I am having trouble searching for lines with . in complex (chained) grep commands.
Is this my error or a bug?
grep '\.' filename | grep 1 # works correctly (finds lines with '.' and '1')
grep 1 filename | grep '\.' # fails: ignores 2nd grep, returns lines with or without '.'
It is not the pipe that causes this; it is really something about grep.
This frankly seems implausible, yet it is true:
cat filename | grep 1 | grep '\.' # works correctly (and provides an easy work-around for the bug)
Adding more protection does not help:
grep 1 filename | grep '\\.' # fails
If there is a something behind or in front of the wildcard, it works:
grep 1 filename | grep 'a\.' # works
grep 1 filename | grep '\.a' # works
But not if the something is a range:
grep 1 filename | grep '[0-9]\.' # fails
grep 1 filename | grep '\.[0-9]' # fails
I can’t believe this is really a bug – I must be missing something, right?
Thanks!
uname -a
Linux conception 2.6.32-33-generic #70-Ubuntu SMP Thu Jul 7 21:09:46 UTC 2011 i686 GNU/Linux
First of all, “chained” grep command is something like “mauvais ton”. Most of times
grep | grepcan be replaced with onegrepwith complicated regular expression.Grepping lines with
.and1in it.-Pmeans perl regex"(\..*1|1.*\.)"is (., some symbols and1) or (1, some symbols and.)So, if you not pretty sure about wildcards usage, regular expressions could be your useful, safe, and powerful tool.
First examples
grep '\.' filename | grep 1,grep 1 filename | grep '\.',cat filename | grep 1 | grep '\.'returnes right answerSecond example
grep 1 filename | grep '\\.'failed because you’re grepping\with any symbol after it.