0

I am querying a database (SQL Server) using isql in a bash script. For example:

RESULT="$(${ISQL} -Q -U ${DB_USER} -S ${DB_SERVER} -D ${DB_NAME} << __END
SELECT order_id % 5 as mod5, count(*) as count
FROM orders
GROUP BY order_id % 5
ORDER BY order_id % 5
GO
__END
)"

My output:

mod5 count -------------------- ----------- 0 17640 1 17640 2 17638 3 17637 4 17638

How can I get the output to have new lines in it? For example:

mod5 count
-------------------- -----------
0 18118
1 18118
2 18116
3 18116
4 18117

I could do something hacky like also selecting '!!!' and then using sed to replace '!!!' with a new line, but I still don't know what do to about the first two lines (headers and dashes). In this case I know that I am going to have two fields of output so I could just count the tokens somehow and insert newlines, but what if I don't know how many columns will be returned in a query like "select * from orders"?

I can think of various solutions but they all seem incredibly hacky - is there a standard way to deal with output like this?

Jer
  • 4,958
  • 6
  • 28
  • 39

1 Answers1

0

No idea about isql or sql-server. but for your problem, this works:

.cmd ..gives..output|xargs -n2

with your data:

kent$  echo "mod5 count -------------------- ----------- 0 17640 1 17640 2 17638 3 17637 4 17638"|xargs -n2
mod5 count
-------------------- -----------
0 17640
1 17640
2 17638
3 17637
4 17638
Kent
  • 173,042
  • 30
  • 210
  • 270