Issue
I need to find whether any line of my file has a value of 0 in the fifth column. The values in this column are all numbers:
cat /home/vsrchops/Usrlogin_1.csv | awk '{print $5}'
8
8
1
4
2
8
23
61
473
1465
1771
2200
I tried this command:
cat /home/vsrchops/Usrlogin_1.csv | awk '{print $5}' | grep 0
But it matches all lines containing 0, rather than those that are exactly 0:
2200
How do I find values that are exactly zero?
Solution
Firstly, the cat
command is unnecessary, since we can use <
to redirect AWK's input to read from the file.
We could modify the grep
command to match the start and end of line like this:
awk '{print $5}' </home/vsrchops/Usrlogin_1.csv | grep '^0$'
Here, ^
matches the start of a line and $
matches the end of line.
Or we can use grep
options to match the whole line:
awk '{print $5}' </home/vsrchops/Usrlogin_1.csv | grep -Fx 0
Either of these will match only those lines that are exactly 0.
Alternatively, we could avoid creating the grep
process, and do the matching entirely within AWK:
awk '$5 ~ /^0$/ {print $5}' </home/vsrchops/Usrlogin_1.csv
That's probably the simplest and most efficient solution.
Answered By - Øystein Baarnes Answer Checked By - Marie Seifert (WPSolving Admin)