I have a file with many numbers in it (only numbers and each number is in one line). I want to find out the number of lines in which the number is greater than 100 (or infact anything else). How can I do that?
Counting the number of lines having a number greater than 100
awkgrepsed
Best Answer
Let's consider this test file:
Now, let's count the number of lines with a number greater than 100:
How it works
$1>100{c++}
Every time that the number on the line is greater than 100, the variable
c
is incremented by 1.END{print c+0}
After we have finished reading the file, the variable
c
is printed.By adding
0
toc
, we force awk to treatc
like a number. If there were any lines with numbers>100
, thenc
is already a number. If there were not, thenc
would be an empty (hat tip: iruvar). By adding zero to it, we change the empty string to a0
, giving a more correct output.