LookIntoEast
LookIntoEast

Reputation: 8808

Group the results of grep command

I've been using grep -f to obtain patterns from one file and extract lines from the other. The results are like below:

1   11294199    11294322    40  10  123 0.0813008
1   11294199    11294322    41  6   123 0.0487805
1   11294199    11294322    42  10  123 0.0813008
1   11294199    11294322    43  2   123 0.0162602
1   11293454    11293544    51  1   90  0.0111111
1   11293454    11293544    52  2   90  0.0222222
1   11291356    11291491    54  6   135 0.0444444
1   11291356    11291491    55  8   135 0.0592593
1   11291356    11291491    56  3   135 0.0222222

Now I need to group the results based on the first three columns,and calculate the sum of column 4 for each of the groups:

1   11294199    11294322  (40+41+42+43)
1   11293454    11293544  (51+52)
1   11291356    11291491  (54+55+56)

How can I get such results? Any options in grep to achieve this? thx

Upvotes: 2

Views: 543

Answers (1)

Steve
Steve

Reputation: 54532

You will need awk to do what you want. Try this:

awk '{ array[$1 "\t" $2 "\t" $3] += $4 } END { for (i in array) print i "\t" array[i] }' file.txt

Results:

1   11294199    11294322    166
1   11291356    11291491    165
1   11293454    11293544    103

HTH

Upvotes: 5

Related Questions