Monday 15 August 2011

linux - Summing by common strings in different files (bash) -



linux - Summing by common strings in different files (bash) -

i have file1 amount of times user shows in files, this:

4 userc 2 usera 1 userb

and have file2 users , other info like:

userc, degree2 usera, degree1 userb, degree2

and want output shows amount of times users show each degree:

5 degree2 2 degree1

i agree associative arrays option, long utilize late version of bash. otherwise, prepare it:

cat file1 | while read score name if f2=`egrep "^$name," file2` hit=`echo "$f2" | awk -f, '{ print $2 }'` echo "$score $hit" fi done

be careful characters, in particular backticks after f2= , hit= correct!

linux string bash sum

No comments:

Post a Comment