Sum numeric columns contained in a plain text file
Hi everyone,
Here are the contents of a plain text file created by a SQL query: Code:
SUM(T.TRNQTY) COUNT(D.TRNSEQ) Code:
20,017 814 Thanks in advance! |
A simple awk one-liner is what you need:
Code:
awk 'NR > 2 {sum1 += $1; sum2 += $2} END{printf "%'\''d %'\''d", sum1, sum2}' file |
Thank you colucix. Your suggestion worked pretty well.
I forgot to mention that this is Oracle Enterprise 11g. I log in to Oracle and at the prompt I write: Code:
sql> spool my_query.txt; Code:
[Whole SQL query here] Any ideas? |
Found the solution here.
|
Same thing occurs in most other databases (ie not just oracle) and I am not sure that it is possible to split the three portions. (SQL query statements, results and count of rows). However, the three portions are separated by a blank line. So starting after the first blank line and summing the two columns till the next blank line should give the result. Also AFAIR there is a 'nohead' or some such clause to to avoid printing the column titles.
PS: I am not sure how data with the error conditions (no records qualify, malformed query, no such database object, rights etc) will display. I mean if you plan to put it in an unattended shell script, then better mail and check the results every time. OK |
You could easily craft your SQL query to generate the sum row for you.
If your result set is as simple as your example and you are not overly concerned about performance, then a simple union would do it. I am not an Oracle guru, but I am sure it also has built-ins for doing that simply too. ANSI SQL includes "WITH ROLLUP" as a GROUP BY modifier that will sum the grouped columns into a total row, and Oracle does support it. |
Thank you colucix, astrogeek and AnanthaP!
|
All times are GMT -5. The time now is 11:39 PM. |