add number in lines line by line in different files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting add number in lines line by line in different files
# 1  
Old 06-29-2010
add number in lines line by line in different files

I have a set of log files that are in the following format

Code:
======= set_1 ========
counter : 315
counter2: 204597
counter3: 290582
======= set_2 ========
counter : 315
counter2: 204597
counter3: 290582
======= set_3 ========
counter : 315
counter2: 204597
counter3: 290582

Is there a good way (pure shell, sed, awk, etc.) to "add" the log files so that I will get a summary where the format is the same, but the numbers are the sum of the numbers in each line?

I'm waiting for some shell magic Smilie Otherwise I just have to write a c/c++ program or something...
# 2  
Old 06-29-2010
Some thing like this:

Code:
 
awk -F":" ' !/set/ { a[$1]=a[$1]+$2 } END { for ( i in a ) print i":"a[i]}' logfile

Assuming logfile is a cat of logfiles*

To be specific

Code:
cat log_files* > file | awk -F":" ' !/set/ { a[$1]=a[$1]+$2 } END { for ( i in a ) print i":"a[i]}' file


Last edited by panyam; 06-29-2010 at 01:36 PM.. Reason: added one more statement "cat"
This User Gave Thanks to panyam For This Post:
# 3  
Old 06-29-2010
Nice elegant answer, but the array in awk does not keep the insertion order... so the resulting file does not follow the original order in the log files. Is is a way to keep track of the order as well?
# 4  
Old 06-29-2010
Quote:
Originally Posted by grandguest
Nice elegant answer, but the array in awk does not keep the insertion order... so the resulting file does not follow the original order in the log files. Is is a way to keep track of the order as well?
If you have gawk:
Code:
WHINY_USERS=1 gawk -F: ' !/set/{ a[$1]+=$2 } END{ for(i in a) print i":"a[i] }' log_files*


---------- Post updated at 19:57 ---------- Previous update was at 19:16 ----------

If you don't have gawk you can try this:
Code:
awk -F: ' 
!/set/ {
  if(!a[$1]){b[c++]=$1} 
  a[$1]+=$2 
}
END{
  for(i=0;i<c;i++) 
    print b[i] ":" a[b[i]]
}' log_files*

Use nawk or /usr/xpg4/bin/awk on Solaris if you get errors.
# 5  
Old 06-29-2010
The gawk solution does not work, but the second solution works great. Thanks!
# 6  
Old 06-29-2010
Quote:
Originally Posted by Franklin52
Code:
  if(!a[$1]){b[c++]=$1}

If it is possible for the value of a[$1] to be 0 (e.g., $0=="counter : 0"), then "!a[$1]" would create a second entry in b for $1 the next time a "counter :..." line is encountered, which would affect the number of times the line keyed by $1 appears in the output (though the sum would be correct). Perhaps, "$1 in a" would be better.

Regards,
Alister
# 7  
Old 06-29-2010
I discovered another problem: I really want a line by line sum, and there is a problem if one of my counters' name is duplicated. So I end up with the following script (not a slick one-liner, but gets the job done):

Code:
#!/bin/zsh
num_of_lines=`grep -n -i '^=*' $1 | sed 's/:.*$//'`

for linenum in `seq $num_of_lines -1 1`; do rm file2; for i in $*; do tail -n $\
linenum $i | head -n 1 >>! file2; done && awk -F":" '{b=$1; a=a+$2} END {printf\
 "%s: %12.0f\n",b,a}' file2; done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Get an output of lines in pattern 1st line then 10th line then 11th line then 20th line and so on.

Input file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 (6 Replies)
Discussion started by: Sagar Singh
6 Replies

2. Shell Programming and Scripting

How to add line to previous line if not a number?

Hi, I am trying to compare 2 lists. However, one of these lists has to be taken from a.pdf file. When I copy the test into a .txt document there are formatting errors which I need to correct. The document is long (~10,000 lines) so I need to script the re-formatting. Currently my file looks... (9 Replies)
Discussion started by: carlr
9 Replies

3. Shell Programming and Scripting

Need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line...

Hello, I need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line... An example of entries in the file would be: SRVXPAPI001 ERRO JUN24 07:28:34 1775 REASON= 0000, PROCID= #E506 #1065: TPCIPPR, INDEX= 003F ... (8 Replies)
Discussion started by: Ferocci
8 Replies

4. Shell Programming and Scripting

Choose a line-number to add line

Hello guys, I'm making a script to add visudo with this script. Do you guys know if it's possible to add words to a line-number you want to. Something like this: echo "Adding words to line-number 16" >> /etc/sudoers # (options to add to line-number-16)? Thanks! (3 Replies)
Discussion started by: dannyvdberg
3 Replies

5. Shell Programming and Scripting

sed command to replace a line at a specific line number with some other line

my requirement is, consider a file output cat output blah sdjfhjkd jsdfhjksdh sdfs 23423 sdfsdf sdf"sdfsdf"sdfsdf"""""dsf hellow there this doesnt look good et cetc etc etcetera i want to replace a line of line number 4 ("this doesnt look good") with some other line ... (3 Replies)
Discussion started by: vivek d r
3 Replies

6. Shell Programming and Scripting

Read line with particular number of lines

Hi all, I have a file sample.txt abc asd adf daf adw add adv wdf I want to control the number of lines to read Like if i give input as ./script_name 2 5 required output asd adf daf (2 Replies)
Discussion started by: krux_rap
2 Replies

7. Shell Programming and Scripting

How to calculate mean in AWK? line by line several files, thousands of lines

I'm kinda stuck on this one, I have 7 files with 30.000 lines/file like this 050 0.023 0.504336 050 0.024 0.529521 050 0.025 0.538908 050 0.026 0.537035 I want to find the mean line by line of the third column from the files named like this: Stat-f-1.dat .... Stat-f-7.dat Stat-s-1.dat... (8 Replies)
Discussion started by: AriasFco
8 Replies

8. UNIX for Dummies Questions & Answers

How to read contents of a file from a given line number upto line number again specified by user

Hello Everyone. I am trying to display contains of a file from a specific line to a specific line(let say, from line number 3 to line number 5). For this I got the shell script as shown below: if ; then if ; then tail +$1 $3 | head -n $2 else ... (5 Replies)
Discussion started by: grc
5 Replies

9. Shell Programming and Scripting

Appending line number to each line and getting total number of lines

Hello, I need help in appending the line number of each line to the file and also to get the total number of lines. Can somebody please help me. I have a file say: abc def ccc ddd ffff The output should be: Instance1=abc Instance2=def Instance3=ccc Instance4=ddd Instance5=ffff ... (2 Replies)
Discussion started by: chiru_h
2 Replies

10. UNIX for Dummies Questions & Answers

display lines after a particular line number

I have a file that has 1k lines and i want to print all the lines after 900th line. an 2)I want to move files f1 ,f2,f3,f4 to p1,p2,p3,p4 Please give me the commands. Thanx in adv. (6 Replies)
Discussion started by: rajashekar.y
6 Replies
Login or Register to Ask a Question