Reputation: 395
How can I do an exact match using grep -v
?
For example: the following command
for i in 0 0.3 a; do echo $i | grep -v "0"; done
returns a
. But I want it to return 0.3 a
.
Using
grep -v "0\b"
is not working
Upvotes: 0
Views: 1033
Reputation: 26481
The safest way for single-column entries is using awk. Normally, I would use grep with the -w
flag, but since you want to exactly match an integer that could be part of a float, it is a bit more tricky. The <dot>-character makes it hard to use any of
grep -vw 0
grep -v '\b0\b'
grep -v '\<0\>'
The proposed solution also will only work on perfect lines, what if you have a lost space in front or after your zero. The line will fail. So the safest would be:
single column file:
awk '($1!="0")' file
multi-word file: (adopt the variable FS
to fit your needs)
awk '{for(i=1;i<=NF;++i) if($i == "0") next}1' file
Upvotes: 1
Reputation: 11267
for i in 0 0.3 a; do echo $i | grep -v "^0$"; done
You need to match the start and end of the string with ^
and $
So, we say "match the beginning of a line, the char 0
and then the end of the line.
$ for i in 0 0.3 a; do echo $i | grep -v "^0$"; done
0.3
a
Upvotes: 2