[plug] Grep not following my regex
Mark O'Shea
mark at musicalstoat.co.uk
Thu Sep 17 10:44:05 WST 2009
On Thu, Sep 17, 2009 at 10:10:33AM +0800, Tim wrote:
> On my laptop. both work.
> $ grep -V
> GNU grep 2.5.3
>
> $ egrep -o '[0-9.]*'
> random 4343.34343
> 4343.34343
>
> $ egrep -o '[0-9.]+'
> random 4343.34343
> 4343.34343
>
> On the Fedora machine, the first doesn't.
>
> $ grep -V
> grep (GNU grep) 2.5.1
>
> $ egrep -o '[0-9.]*'
> random 4343.34343
>
> $ egrep -o '[0-9.]+'
> random 4343.34343
> 4343.34343
Okay, consider this (on 2.5.1):
sh-3.1$ egrep -oc '[0-9.]*'
random 4343.34343
1
So there was a single match there but it didn't print anything out (or
wouldn't if I hadn't have been counting matches). Why not? Well have a
look at what you are asking for it to match. Zero or more numbers or
periods. Zero is the first match it could come to (depending on how it
processes the line) and as -o only asks it to print out a match it has
decided to print nothing.
Doesn't seem very useful, but it has done what you asked. Now it does
seem that the behaviour may have changed for 2.5.3 (or in libc or it's a
locale issue, which has affected matches in grep before), but the second
form of the regexp is the only one of the two which is guaranteed to
only match what you want. Be respectful of regular expressions. They
can expose any lack of clear thought that you may have while creating
them. They've certainly spent years doing that to me ;-)
Regards,
--
Mark
More information about the plug
mailing list