count lines with different conditions using awk

1.6k views Asked by At

I need to get the number of lines with 2 different conditions for 1 text file. The first condition is that values of the third column are smaller than 10 so I can do it by the following script:

awk '$3<=10' DATA_File | wc -l

The second condition is just to get a total number of lines in the same file this I can get by:

awk 'END { print FNR}' DATA_File

or

awk '$3' DATA_File | wc -l

However, what I don't know is how to merge these to commands in a single string so I can get the result saved in a separate file with one string separated by either "tab" or "space" consisting of "number of string with <10", "total number of strings", "their ratio/ or percentage"

for instance the file is:

wer fre 11
grt o34 5
45f 123 45

the output I need is:

2 3 0.66/ or 66%

I could write a small script on python which would do it but due to a number of reasons bash would be much more convenient.

1

There are 1 answers

9
fedorqui On BEST ANSWER

You can for example say:

$ awk '$3<=10 {min10++} END {print min10, FNR, (FNR?min10/FNR:0)}' file
1 3 0.333333

Or print and output to a file like print ... > "new_file".

You can also use printf to provide a better format:

$ awk '$3<=10 {min10++} END {printf "%d %d %.2f%\n", min10, FNR, (FNR?min10/FNR:0)}' file
1 3 0.33%

The (FNR?min10/FNR:0) trick is courtesy of Ed Morton and is used to prevent diving by zero.