Reputation: 38492
Can someone explain why this code doesn't work as expected? I would expect it only to match the first character, and it does with literal characters, but the wildcard (.) and characters classes behave strangely:
I use -o just to demonstrate exactly how things are matching, it doesn't change what matches at all.
$ echo foo | grep -o '^.'
f
o
o
Some more unexpected behavior:
$ echo foobarbazquux | grep -o '^[foarqux]'
f
o
o
$ echo foobarbazquux | grep -o '^.[^u]'
fo
ob
ar
ba
zq
Esentially the beginning-of-line matcher (^) is not behaving as expected in these cases. Is there any way to make it behave more normally?
Upvotes: 3
Views: 388
Reputation: 360742
From my Ubuntu 10.04 box:
marc@panic:~$ echo foo | grep -o '^.'
f
marc@panic:~$ echo foobarbazquux | grep -o '^[foarqux]'
f
marc@panic:~$ echo foobarbazquux | grep -o '^.[^u]'
fo
marc@panic:~$ grep --version
GNU grep 2.5.4
There are a series of environment variables grep will look for to control its behavior/output, so check if any of those are set. Most likely you've got 'GREP_OPTIONS' set.
Upvotes: 0