Simple aggregation using linux scripts

Say I have a text file with lines like this:

foo 10 bar 15 bar 5 foo 30 ... 

What is the easiest way to create the following output:

 foo 40 bar 20 

?

+4
source share
4 answers

This will do:

 awk '{arr[$1]+=$2;} END { for (i in arr) print i, arr[i]}' file 

For more information, read the Awk associative arrays.

+9
source

Use this awk script:

 awk '{sums[$1] += $2} END {for (a in sums) print a, sums[a]}' infile 

OUTPUT:

 foo 40 bar 20 

Use this awk tutorial on using associative arrays :

+5
source

If you are interested in perl:

 perl -F -lane '$X{$F[0]}=$X{$F[0]}+$F[1];if(eof){foreach (keys %X){print $_." ".$X{$_}}}' your_file 
0
source

Here is one way with sorting, GNU sed and bc:

 sort infile | sed -r ':a; N; s/([^ ]+) +([^\n]+)\n\1/\1 \2 +/; ta; P; D' | sed -r 'h; s/[^ ]+/echo/; s/$/ | bc/e; G; s/([^\n]+)\n([^ ]+).*/\2 \1/' 

Output:

 bar 20 foo 40 

The first sed joins adjacent lines with the same key adding + between numbers, the second passes the sums to bc.

0
source

Source: https://habr.com/ru/post/1444447/


All Articles