Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

addition of fields from multiple files

Status
Not open for further replies.

moreton

Technical User
Jun 3, 2008
5
CA
hello,
I have several hundred files with similar file names (bldgX1_1.csv, bldgX1_2.csv, bldgX2_1.csv etc). Each file has an array of data

For example
bldgX1_1.csv
1, 2, 3
2, 3, 4
3, 4, 5
and bldgX1_2.csv
1, 1, 1
1, 1, 1
1, 1, 1

I am wanting to take both of these files and generate one file bldgX1.csv that has the following output
2, 3, 4
3, 4, 5
4, 5, 6
That is sum each equivalent field position over all of the files that have bldgX1 in common.

I have tried the following code (or variations of) where a.csv and b.csv a test files. with the accompanying output. As you can see there is no summation involved. Have used 6 in my counting integers for trouble shooting but these should be reset to 3 at some point in the case

Code:
$ awk 'BEGIN{FS=OFS=","}
FNR = NR {for(i=1;i<=3;i++) {sum[NR,i] = $i}; next} ; END{for(x=1;x<=6;x++){for(y=1;y<=6;y++) printf "%d, ", sum[x,y]; printf
 "\n" }};  {sum[NR,i] += $i}; END{for(m=1;m<=6;m++){for(n=1;n<=6;n++) printf "%d, ",sum[m,n]; printf "\n"}}' b.csv a.csv
1, 2, 3, 0, 0, 0, 
2, 3, 4, 0, 0, 0, 
3, 4, 5, 0, 0, 0, 
1, 1, 1, 0, 0, 0, 
1, 1, 1, 0, 0, 0, 
1, 1, 1, 0, 0, 0, 
1, 2, 3, 0, 0, 0, 
2, 3, 4, 0, 0, 0, 
3, 4, 5, 0, 0, 0, 
1, 1, 1, 0, 0, 0, 
1, 1, 1, 0, 0, 0, 
1, 1, 1, 0, 0, 0,

Any assistance you can offer would be greatly appreciated. Is it even possible using awk?
Regards
 
As soon as i posted it i worked out the problem (why I didn't see it before who knows).

For those interested here is the code that seems to be working. had to put in an extra equal sign in FNR = NR, and removed the first printing section.

Code:
$ awk 'BEGIN{FS=OFS=","}
FNR == NR {for(i=1;i<=3;i++) {sum[FNR,i] = $i}; next} ; {for(j=1;j<=3;j++) {sum[FNR,j] += $j}}; END{for(m=1;m<=6;m++){for(n=1
;n<=6;n++) printf "%d, ",sum[m,n]; printf "\n"}}' b.csv a.csv
2, 3, 4, 0, 0, 0, 
3, 4, 5, 0, 0, 0, 
4, 5, 6, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0,

If needed the thread can be deleted. Will also save me looking like a fool...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top