Sum numeric columns contained in a plain text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sum numeric columns contained in a plain text file
# 1  
Old 10-04-2013
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)
---------------- ----------------
            1380               46
            1393               59
            2680              134
             740               37
             620               31
            1470               42
            3536              148
            5634              188
            2564              129

How can I sum the numeric columns to get the following result? No need to solve the problem for me. Just point me in the right direction and I'll be more than thankful Smilie.
Code:
20,017           814

I'm in Red Hat Enterprise Linux Server release 5.7 (Tikanga) and using GNU bash, version 3.2.25(1)-release (i386-redhat-linux-gnu).
Thanks in advance!
# 2  
Old 10-04-2013
Try:
Code:
awk 'NR>2{a+=$1;b+=$2}END{print a"\t"b}' file

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 10-04-2013
Why not use the resp. SQL- command?
# 4  
Old 10-04-2013
bartus11's suggestion worked fairly 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;
sql> [my query here];
sql> spool off;
sql> exit

I then return to my command prompt and when I open the file my_query.txt, besides the result of the query I also see the following:
Code:
[Whole SQL query here]
[Result of query here]
[Number of rows returned here]

Obviously I only want to save the query results... I still need to find a way to get rid of the other stuff (the text of the query and the # of rows returned by it).
Any ideas?

Last edited by gacanepa; 10-04-2013 at 09:04 PM..
# 5  
Old 10-04-2013
There might be a SET option for it.

EDIT:
Try
Code:
SET ECHO OFF
SET VERIFY OFF
SET FEEDBACK OFF
SET PAGESIZE 0


Last edited by CarloM; 10-04-2013 at 09:12 PM..
This User Gave Thanks to CarloM For This Post:
# 6  
Old 10-04-2013
[SOLVED] Sum numeric columns contained in a plain text file

Quote:
Originally Posted by CarloM
There might be a SET option for it.

EDIT:
Try
Code:
SET ECHO OFF
SET VERIFY OFF
SET FEEDBACK OFF
SET PAGESIZE 0

Thank you very much!
I was almost there and couldn't understand why the text of the query was still being saved in the text file, until I read that the SET options are only good if you use it in a sql script (separate file) and then source to in the bash script, like so:
File query.sql:
Code:
SET ECHO OFF
SET VERIFY OFF
SET FEEDBACK OFF
SET PAGESIZE 0
SPOOL myfile.txt
SELECT field1, field2 FROM mytable;
SPOOL OFF
exit

Then in the bash script:
Code:
#!/bin/bash
sql @query sql

It worked like a charm!
This User Gave Thanks to gacanepa For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Need Optimization shell/awk script to aggreagte (sum) for all the columns of Huge data file

Optimization shell/awk script to aggregate (sum) for all the columns of Huge data file File delimiter "|" Need to have Sum of all columns, with column number : aggregation (summation) for each column File not having the header Like below - Column 1 "Total Column 2 : "Total ... ...... (2 Replies)
Discussion started by: kartikirans
2 Replies

2. UNIX for Beginners Questions & Answers

Copy columns from one file into another and get sum of column values and row count

I have a file abc.csv, from which I need column 24(PurchaseOrder_TotalCost) to get the sum_of_amounts with date and row count into another file say output.csv abc.csv- UTF-8,,,,,,,,,,,,,,,,,,,,,,,,, ... (6 Replies)
Discussion started by: Tahir_M
6 Replies

3. Shell Programming and Scripting

Evaluate 2 columns, add sum IF two columns satisfy the condition

HI All, I'm embedding SQL query in Script which gives following output: Assignee Group Total ABC Group1 17 PQR Group2 5 PQR Group3 6 XYZ Group1 10 XYZ Group3 5 I have saved the above output in a file. How do i sum up the contents of this output so as to get following output: ... (4 Replies)
Discussion started by: Khushbu
4 Replies

4. Shell Programming and Scripting

Evaluate 2 columns, add sum IF two columns match on two rows

Hi all, I know this sounds suspiciously like a homework course; but, it is not. My goal is to take a file, and match my "ID" column to the "Date" column, if those conditions are true, add the total number of minutes worked and place it in this file, while not printing the original rows that I... (6 Replies)
Discussion started by: mtucker6784
6 Replies

5. Shell Programming and Scripting

Modify one line in a plain text file

Hi everyone, I want to know, if there is a way to modify one line in a text file with unix script, with out re-writing all the file. For example, i have this file: CONFIGURATION_1=XXXX CONFIGURATION_2=YYYY CONFIGURATION_3=ZZZZ supose i have a command or function "modify" that... (7 Replies)
Discussion started by: Xedrox
7 Replies

6. Shell Programming and Scripting

Sum of two columns in a file

Hi, I need to do a sum of two columns in a file where delimiter is |^ input 1|^2|^3|^4|^50|^2|^3|^100 2|^3|^4|^6|^100|^7|^2|^50 3|^4|^2|^3|^50|^6|^3|^50 4|^2|^5|^7|^25|^2|^8|^25 Output required: Sum of 2 columns 5 & 8 which is 450 (11 Replies)
Discussion started by: Jram
11 Replies

7. Shell Programming and Scripting

To perform sum aggregation on numeric fields

Hi all, I have following scenario to perform sum aggregation on certain columns Node Allocated_Space Pool_Name CS_Group Utilized Space -------- ---------------- ---------- --------- -------------- bdw1a_lun01 300 bdw_p0 ... (2 Replies)
Discussion started by: ckwan
2 Replies

8. UNIX for Dummies Questions & Answers

Sum of all columns in all files in one output file

If I have say 4 files like this: File1: 1 3 4 7 7 0 5 7 5 9 1 2 7 4 8 File2: 1 4 6 2 5 7 1 2 3 6 0 3 0 3 8 File3: (5 Replies)
Discussion started by: cosmologist
5 Replies

9. Shell Programming and Scripting

Sum of three columns - in 4N columns file

Hi All, happy new year. I have a file with 4xN columns like 0.0000e+00 0.0000e+00 7.199E+07 7.123E+07 6.976E+07 6.482E+07 5.256E+07 2.523E+07 0.0000e+00 0.0000e+00 8.641E+07 8.550E+07 8.373E+07 7.780E+07 6.309E+07 3.028E+07... (8 Replies)
Discussion started by: f_o_555
8 Replies

10. UNIX for Dummies Questions & Answers

Selectable numeric text menu from file possible?

Is there a way i can take 'lets say the etc/group file' and arange the group names into a selectable numeric menu eg; 1) root:0 2) other:1 3) bin:2 4) sys:3 ..... ..... ..... and so on then GROUP="group name of users selection" i have tried for over an hour searching on... (6 Replies)
Discussion started by: switchkill
6 Replies
Login or Register to Ask a Question