0

Why does the first grep statement below fail to return results, but the modified grep statement below that works? I have tried egrep as well with same results.

cat test
ALL: 192.168.0.0/255.255.0.0, 10.0.0.0/255.0.0.0

grep '^[\s]*ALL[\s]*:[\s]*192.168.0.0/255.255.0.0[\s]*' test
No results

grep '^[\s]*ALL[\s]*: 192.168.0.0/255.255.0.0[\s]*' test
ALL: 192.168.0.0/255.255.0.0, 10.0.0.0/255.0.0.0

Also , when I put a $ at the end, both fail.

grep '^[\s]*ALL[\s]*:[\s]*192.168.0.0/255.255.0.0[\s]*$' test
No results

grep '^[\s]*ALL[\s]*: 192.168.0.0/255.255.0.0[\s]*$' test
No results
Brian Mc
  • 141
  • 8
  • 2
    `\s` is PCRE syntax. It isn't guaranteed to be available in BRE or ERE, and is purely a local extension there. – Charles Duffy Aug 09 '18 at 13:59
  • 2
    Use `[[:space:]]` instead to have something that's guaranteed to work without depending on your OS folding in PCRE extensions to be available in BRE or ERE. – Charles Duffy Aug 09 '18 at 13:59
  • 3
    ...note that `grep` is not part of bash, but is an external tool provided by your OS vendor; we don't know which grep version you have, and that's critical to the question. – Charles Duffy Aug 09 '18 at 14:00
  • 1
    @anubhava, ...*should* work if you have vendor extensions; `\s` isn't guaranteed in ERE either. http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap09.html sets out both standards. See the linked question (https://stackoverflow.com/questions/4233159/grep-regex-whitespace-behavior), which goes into the version boundary in which GNU `grep` started supporting `\s` without `-P`. – Charles Duffy Aug 09 '18 at 14:03
  • 1
    As for putting $ at the end of the line, that's because $ means "the string ends here", which it clearly doesn't. – Mad Physicist Aug 09 '18 at 14:07

1 Answers1

2

grep is guaranteed to implement BRE -- POSIX basic regular expressions. \s is not meaningful in BRE. (Some OS vendors extend the standard, some don't).

Use [[:space:]] instead to have something that works everywhere.

Adding $ to the end of your expression makes it fail because it matches the end of the line. Your line has an extra , 10.0.0.0/255.0.0.0 after the matching portion, so of course that doesn't match $. You could say .*$, but that would be redundant unless you had the -o/--only-matching flag enabled.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • Thanks Charles, when I use [[:space:]] that works, but why does the [\s]* work in the first two cases, but fails in the third instance? Also, can you explain why the $ at the end is not working? – Brian Mc Aug 09 '18 at 14:07
  • 1
    @BrianMc, `*` matches "zero or more", and if you have none of something, it doesn't matter what the something is. As for the `$`, it only matches if the input line ends at that point, which isn't true for your given inputs -- presumably there's trailing whitespace, and as discussed in this answer, `\s` isn't guaranteed to match whitespace in `grep`. – Charles Duffy Aug 09 '18 at 14:08
  • Charles, make perfect sense. Thanks for quick response! Have a great day. – Brian Mc Aug 09 '18 at 14:38