简体   繁体   中英

How to sum from 100 file - bash/awk?

I need to sum up values from 100 files. This is part of my input suma_wiazan_wodorowych_2_1.txt

2536
1928
1830
1774
1732
1673
1620

suma_wiazan_wodorowych_2_101.txt (name for every file is changing by 100, so 1, 101, 201 etc)

2535
1987
1895
1829
1805
1714
1657

So my script should add first row from the first file first row from the second file.... to one hundred 2535+2536+..+..+2621 And against the second row from the first + second row from the second file etc. The length of every file is 5000 rows (so I will have 5000 sums) Do you have any idea?

A one-liner using paste and bc

paste -d + suma_wiazan_wodorowych_2_* | bc

assuming the lines contain only bare numbers without a leading + (negative numbers, that are, numbers with a single leading - , are ok), and the files have equal number of lines.

with awk

$ awk '{sum[FNR]+=$1} END{for(i=1;i<=FNR;i++) print sum[i]}' file*

sum all corresponding values from all input files, print at the end.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM