Sum of two columns in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sum of two columns in a file
# 8  
Old 08-13-2012
Quote:
Originally Posted by Don Cragun
Also in an ERE ^ is an anchor (even when it is not the first character in the ERE) except when it appears in a bracket expression ...
That's incorrect. In a POSIX ERE, ^ is only an anchor when it is the first character in an expression.

\|^ should be portable, but, sadly, some implementations botch ^ handling. Some (perhaps all?) mawk implementations definitely do not handle this correctly.

Regards,
Alister
# 9  
Old 08-13-2012
Quote:
Originally Posted by alister
That's incorrect. In a POSIX ERE, ^ is only an anchor when it is the first character in an expression.

\|^ should be portable, but, sadly, some implementations botch ^ handling. Some (perhaps all?) mawk implementations definitely do not handle this correctly.

Regards,
Alister
No. What you say is true for a BRE; not for an ERE. Quoting from IEEE Std. 1003.1-2008, P190, L6174-6183:
Quote:
9.4.9 ERE Expression Anchoring
An ERE can be limited to matching strings that begin or end a line; this
is called ‘‘anchoring''. The <circumflex> and <dollar-sign> special
characters shall be considered ERE anchors when used anywhere outside
a bracket expression. This shall have the following effects:
1. A <circumflex> ('ˆ') outside a bracket expression shall anchor the
expression or subexpression it begins to the beginning of a string; such
an expression or subexpression can match only a sequence starting at
the first character of a string. For example, the EREs "ˆab" and "(ˆab)"
match "ab" in the string "abcdef", but fail to match in the string
"cdefab", and the ERE "aˆb" is valid, but can never match because the
'a' prevents the expression "ˆb" from matching starting at the first
character.
2. ... ... ...
These 2 Users Gave Thanks to Don Cragun For This Post:
# 10  
Old 08-13-2012
Quote:
Originally Posted by Don Cragun
No. What you say is true for a BRE; not for an ERE. Quoting from IEEE Std. 1003.1-2008, P190, L6174-6183:
I stand corrected. Thank you.

Also, apologies to mawk devs Smilie

Regards,
Alister
# 11  
Old 08-17-2012
Hi,

Thanks for your response..

Now my file is having around 300 columns and awk is giving an error message "awk cannot have more than 199 fields" while summing up the columns 80 and 138.

Please suggest

Thanks, Jram

Last edited by Jram; 08-17-2012 at 07:51 AM.. Reason: highlighted the error message
# 12  
Old 08-17-2012
Quote:
Originally Posted by Jram
Hi,

Thanks for your response..

Now my file is having around 300 columns and awk is giving an error message "awk cannot have more than 199 fields" while summing up the columns 80 and 138.

Please suggest

Thanks, Jram
What system are you using?

I see nothing in the POSIX standard that would allow this limitation.
Input files for awk are text files. Text files can't contain null bytes. If there are any characters in a text file, the last character in the file must be a <newline> character and no line in a text file (including its trailing <newline> character can be more than {LINE_MAX} bytes long. The minimum value for {LINE_MAX} on conforming implementations is 2,048, so in theory, I believe the standard requires awk implementations to support at least 2,049 fields (more if the implementation's value of {LINE_MAX} is larger than the minimum).

Assuming your input line are not longer than {LINE_MAX} bytes long, the following should work around your awk's limit on the number of fields:
Code:
awk -F"\n" '# Call getfield(N) to extract field N from $0 with "|^"
# as the field separator.  f1toN is a local temp variable.
function getfield(n,f1toN) {
        if(match($0,"(\([^^]*\)[.|.][.^.]){"n"}") > 0) {
                f1toN=substr($0,RSTART,RLENGTH-2)
                match(f1toN,"(\([^^]*\)[.|.][.^.]){1,"n-1"}")
                return substr(f1toN,RLENGTH+1)
        }
        return ""
}
        {sum+=(getfield(80)+getfield(138))}
END     {print "Sum of 2 columns 80 & 138 which is",sum
}' in

Setting FS to <newline> means each input line will be treated as a single field. The getfield(n) function will extract field n from that line (assuming the field delimiter is still "|^") by storing the 1st n fields in f1toN and then extracting just the nth field from the end of that. (This could be done with a single call to substr() by calculating the start and end points of the desired field using the RSTART and RLENGTH from the two calls to match(). But I'll leave that as an exercise for the reader.) Note that the "n" and "n-1", respectively, in the two calls to match() are not inside a "..." string!

Last edited by Don Cragun; 08-17-2012 at 02:53 PM..
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. UNIX for Beginners Questions & Answers

Group by columns and add sum in new columns

Dear Experts, I have input file which is comma separated, has 4 columns like below, BRAND,COUNTRY,MODEL,COUNT NIKE,USA,DUMMY,5 NIKE,USA,ORIGINAL,10 PUMA,FRANCE,DUMMY,20 PUMA,FRANCE,ORIGINAL,15 ADIDAS,ITALY,DUMMY,50 ADIDAS,ITALY,ORIGINAL,50 SPIKE,CHINA,DUMMY,1O And expected output add... (2 Replies)
Discussion started by: ricky1991
2 Replies

4. 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

5. Shell Programming and Scripting

Get the SUM of TWO columns SEPARATELY by doing GROUP BY on other columns

My File looks like: "|" -> Field separator A|B|C|100|1000 D|E|F|1|2 G|H|I|0|7 D|E|F|1|2 A|B|C|10|10000 G|H|I|0|7 A|B|C|1|100 D|E|F|1|2 I need to do a SUM on Col. 5 and Col.6 by grouping on Col 1,2 & 3 My expected output is: A|B|C|111|11100 (2 Replies)
Discussion started by: machomaddy
2 Replies

6. Shell Programming and Scripting

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: SUM(T.TRNQTY) COUNT(D.TRNSEQ) ---------------- ---------------- 1380 46 1393 59 2680 134 740 37 ... (5 Replies)
Discussion started by: gacanepa
5 Replies

7. 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

8. Shell Programming and Scripting

Sum columns

Hi All, I'm new to this forum. So please be patience with me! :) I have a file that looks like this (all rows have the same number of columns): 19 20 30 15 17 38 51 60 74 85 96 07 .... 10 20 44 59 39 88 13 77 30 10 11 12 .... . . . I want to sum the value of first field to all the... (2 Replies)
Discussion started by: Aderson Nascime
2 Replies

9. 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

10. 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
Login or Register to Ask a Question