You are not logged in.

#1 2013-11-01 15:40:47

frabjous
Member
Registered: 2010-07-13
Posts: 367

grep: '\s*foo' vs. '[\s]*foo'

I have quite a few scripts in which I use grep with the combination \s* to represent any number of whitespace characters.

As of the recent update to 2.15 these scripts have all stopped working. However [\s]* seems to work:

$ echo ' foo' | grep '\s*foo'
$ echo ' foo' | grep '[\s]*foo'
 foo

Is the first syntax wrong, and if so, why? Have I been grepping wrong for years? Should I change all my scripts, or is this a bug with grep 2.15?

Last edited by frabjous (2013-11-01 15:41:27)

Offline

#2 2013-11-01 15:50:11

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: grep: '\s*foo' vs. '[\s]*foo'

There's https://bugs.archlinux.org/task/37556 , so maybe some things did change in grep 2.15.


Edit: egrep works:

$ echo ' foo' | egrep '\s*foo'
 foo

Last edited by karol (2013-11-01 15:56:56)

Offline

#3 2013-11-02 00:16:37

Trent
Member
From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 990

Re: grep: '\s*foo' vs. '[\s]*foo'

Yeah, I'm pretty sure this is a bug. Bleeding edge and all that. https://www.gnu.org/software/grep/manual/grep.html makes no mention of such a removal. [[:space:]] still works too.

I didn't know regular grep did such things; I never bothered to learn the difference and so always use egrep when doing anything more complex than ^ and $ anchors.

Offline

#4 2013-11-04 15:35:35

frabjous
Member
Registered: 2010-07-13
Posts: 367

Re: grep: '\s*foo' vs. '[\s]*foo'

Thanks for the suggestions. I'm a bit hesitant to switch to egrep, since I'm not that familiar with what the differences are, and of course my actual regex's are much more complicated than the toy example above.

I do think it is a bug. I'd file a bug report, but there doesn't seem to be a way to do so unless you're on the dev mailing list, which is rather annoying.

Offline

#5 2014-09-18 01:10:41

nuf0xx
Member
Registered: 2014-02-02
Posts: 7

Re: grep: '\s*foo' vs. '[\s]*foo'

--------------------------------------




       Grep  searches the named input FILEs (or standard input if no files are
       named, or the file name - is given) for lines containing a match to the
       given PATTERN.  By default, grep prints the matching lines.

       In addition, two variant programs egrep and fgrep are available.     Egrep
       is the same as grep -E.    Fgrep is the same as grep -F.


       -E, --extended-regexp
          Interpret PATTERN as an extended regular expression (see below).

       -F, --fixed-strings
          Interpret PATTERN as a list of fixed strings, separated by  new-
          lines, any of which is to be matched.



*apologies

Last edited by jasonwryan (2014-09-18 01:28:32)

Offline

#6 2014-09-18 01:29:37

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,426
Website

Re: grep: '\s*foo' vs. '[\s]*foo'

@nuf0xx Please use code tags when pasting to the boards https://wiki.archlinux.org/index.php/Fo … s_and_Code


And there is no need to paste that ascii art thing, it adds nothing to the thread.


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#7 2014-09-18 12:47:23

frabjous
Member
Registered: 2010-07-13
Posts: 367

Re: grep: '\s*foo' vs. '[\s]*foo'

Please note this thread is almost a year old. The bug I reported on no longer exists in the current version of grep, and so the thread is now irrelevant. nuf0xx's copy and paste job would not have been relevant anyway.

Offline

Board footer

Powered by FluxBB