I have a file called test.txt
with contents like below:
Si 28.086 Si.bhs
As 74.90000 As.pz-bhs.UPF
Here is some of my running of grep
I just can't understand, why grep bhs[^\.] test.txt
won't grep the first line? Could someone please explain? Doesn't [^\.]
represent any character other than dot?
echo \.
andecho [^\.]
. Quote your regexes. – muru Mar 06 '17 at 02:18bhs
on the first line? "followed by not dot" is not the same as "not followed by dot" – steeldriver Mar 06 '17 at 02:21grep bhs[^\\.] test.txt
norgrep 'bhs[^\\.] ' test.txt
works – user15964 Mar 06 '17 at 02:26grep 'bhs$'
or (if yourgrep
supports PCRE mode) usegrep -P 'bhs(?!\.)'
– steeldriver Mar 06 '17 at 02:26bhs[^\.]
matches the first bhs – user15964 Mar 06 '17 at 02:28grep -P 'bhs[^\\.]' test.txt
doesn't work – user15964 Mar 06 '17 at 02:30grep
searches within lines. The newline character (\n
) is the terminator of the line, so[^.]
won't match it. Note that[^\.]
is actually "any character except backslash or dot", as characters are not considered special within character classes – Fox Mar 06 '17 at 02:33[^\.]
also contains backslash? Isn't\.
the escaping of dot? – user15964 Mar 06 '17 at 02:37printf 'bhs\\xyz\n' | grep 'bhs[^\.]'
. If your regexp is left unquoted, then your shell eats the backslash beforegrep
gets it – Fox Mar 06 '17 at 02:40grep
andgrep -P
treat[^\.]
differently – user15964 Mar 06 '17 at 02:59grep 'bhs\.'
andgrep 'bhs[^.]'
– muru Mar 06 '17 at 05:27