Wednesday, July 22, 2015

Plot all lines that have certain sum using awk code

To calculate the sum of all fields for each line use NF variable to define a for loop, than sum all fields and keep it in a t variable. If t is not equal to 0, print the whole line with print$0.  Then set back t to 0 for the next line.

awk '{for(i=1;i<=NF;i++) t+=$i; if(t!=0) print$0; t=0}' file.txt > file.txt.filtered


To print all lines that have sum of 100, similarly use code:

awk '{for(i=1;i<=NF;i++) t+=$i; if(t==100) print$0; t=0}' file.txt > file.txt.filtered

No comments:

Post a Comment