Sum fields of different files using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sum fields of different files using awk
# 8  
Old 07-23-2013
Quote:
Originally Posted by Yoda
If you want to disregard the first column, just print the sum:
Code:
print A[k]


By default, the order in which a for (i in array) loop scans an array is not defined; it is generally based upon the internal implementation of arrays inside awk.

You might have to use an indexed array to preserve the order.
Oh yeah, thanks Yoda, I missed this one. Pretty obvious. Thanks for your dedication. Smilie
# 9  
Old 07-23-2013
Hello Yoda,

Could you please explain the use of END here as if we are not using END it is giving some thing else result, will be grateful to you if you throw some light on same.
Code:
 
awk '
        # Create an associative array: A for which value is sum of $2 and indexed by $1
        {
                A[$1] += $2
        }

        # End Block
        END {
        # For each element in associative array: A
                for ( k in A )
                        # Print index & value of
                        print k, A[k]
        }
# Path name expansion (aka globbing) will help open & read all files with file name prefixed: file
' file*




Thanks,
R. Singh
# 10  
Old 07-23-2013
BEGIN and END are special awk patterns.

They are usually used for startup and cleanup actions respectively.

A BEGIN rule is executed only once before the first input record is read. Likewise, an END rule is executed once only, after all the input is read.

I recommend reading: GNU Awk User's Guide or AWK Manual
This User Gave Thanks to Yoda For This Post:
# 11  
Old 07-23-2013
Quote:
Originally Posted by rogeriog.em
Thanks Yoda and RudiC, both codes work in the input I provide. But is there a way to do it disregarding the first column values. I mean:
Code:
file 1                file 2
2  5                 4  5
5  6                 5  4
3  5                 8  3

Output:
Code:
file 3
10
10
8

Also my real data is ordered as:
Code:
-48.000   1.2
-47.990   1.5
....
25.000    0.033
25.010    0.023

When I run these codes it seems to sum the values of second column properly but they go out of order. Is there a way to generate them in order or to put them in order again?

Thank you very much.
So you want it based on line no., not on the col1 value? Try
Code:
awk '{RES[FNR]+=$2} max<FNR {max=FNR} END {for (i=1; i<=max; i++) print RES[i]}' file1 file2
10
10
8

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk match two fields in two files

Hi, I have two TEST files t.xyz and a.xyz which have three columns each. a.xyz have more rows than t.xyz. I will like to output rows at which $1 and $2 of t.xyz match $1 and $2 of a.xyz. Total number of output rows should be equal to that of t.xyz. It works fine, but when I apply it to large... (6 Replies)
Discussion started by: geomarine
6 Replies

2. Shell Programming and Scripting

awk to update file with sum of matching fields in another file

In the awk below I am trying to add a penalty to a score to each matching $1 in file2 based on the sum of $3+$4 (variable TL) from file1. Then the $4 value in file1 is divided by TL and multiplied by 100 (this valvue is variable S). Finally, $2 in file2 - S gives the updated $2 result in file2.... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. Shell Programming and Scripting

awk to comppare two files using rwo fields

I use the two awk scripts for comparing file1 and file2. First awk compare $3 column: awk -v OFS="\t" 'NR==FNR{a=$4;next}{$2=$2 "\t"(a?a:"-")}1' file1 file2Second awk compare $2 column: awk -v OFS="\t" 'NR==FNR{a=$4;next}{$2=$2 "\t"(a?a:"-")}1' file1 file2 The only difference ... (12 Replies)
Discussion started by: cmccabe
12 Replies

4. Shell Programming and Scripting

Need to sum up a column value from multiple files into Credit and Debit categories using awk command

i have multiple files with Batch Header, Record detail & Batch trailer data in the files like : BH 20150225950050N8262 RD 20140918000000 99999999 unk Deferred Settlement -13950 BT01 -13950 *Above sample data donot have the spaces coorectly defined. I do have multiple batch trailer... (1 Reply)
Discussion started by: kcdg859
1 Replies

5. Shell Programming and Scripting

Need help in finding sum for values in 2 different fields

Hi there, I have 2 files in following format cat file_1 Storage Group Name: aaaa HBA UID SP Name SPPort ------- ------- ------ 0 21 Storage Group Name: bbbb HBA UID... (2 Replies)
Discussion started by: jpkumar10
2 Replies

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

7. Programming

comparing two fields from two different files in AWK

Hi, I have two files formatted as following: File 1: (user_num_ID , realID) (the NR here is 41671) 1 cust_034_60 2 cust_80_91 3 cust_406_4 .. .. File 2: (realID , clusterNumber) (total NR here is 1000) cust_034_60 2 cust_406_4 3 .. .. (11 Replies)
Discussion started by: amarn
11 Replies

8. Shell Programming and Scripting

Compare fields in 2 files using AWK

Hi unix gurus, I have a urgent requirement, I need to write a AWK script to compare each fields in 2 files using AWK. Basically my output should be like this. file1 row|num1|num2|num3 1|one|two|three 2|one|two|three file2 row|num1|num2|num3 1|one|two|three 2|one|two|four ... (5 Replies)
Discussion started by: rashmisb
5 Replies

9. Shell Programming and Scripting

calculating sum of fields in a file

Hey, I have a file and it has only one field. I need to calculate the sum of each filed as total. For e.g my file is 1 2 3 4 5 I need to calculate the total sum as 15. Please let me know how i can do it? (4 Replies)
Discussion started by: dsravan
4 Replies

10. Shell Programming and Scripting

sort sum fields

HI ALL, i have a problem when i do a sort sum with many fields. Is there a limit for fields? Do you know a solution? thanks in advance. the shell is: # SORT1 SORT1_rcode=777 if ; then echo "USE $DARSEQ/OTPU.FTPEPREC RECORD F,1000 " > $DARPARSRT/TPEKL508.SORT1_$$.srt ... (6 Replies)
Discussion started by: tullo
6 Replies
Login or Register to Ask a Question