2

I was working on the Linux box A and I run this:

grep '^\S*\s-' access_log

That displayed some lines, as expected.

Then I moved to the machine B and I launched exactly the same command. But this time it didn't work.
I had to launch this in order to get done what I needed:

grep '^[^ ]* -' access_log  

Before succeeding, I tried all of these but with no luck:

grep '^\S* -' access_log  
grep '^\S*\s-' access_log      
grep -e '^\S* -' access_log   
grep -E '^\S* ' access_log    

It looks like machine B doesn't understand the metacharacters \S and \s.

Both of the boxes were running: grep 2.5.1 and bash 3.2.25

How is that possible?

Cheers, Dan

gblazex
  • 49,155
  • 12
  • 98
  • 91
Daniele
  • 99
  • 3
  • https://stackoverflow.com/questions/18514135/bash-regular-expression-cant-seem-to-match-any-of-s-s-d-d-w-w-etc is about Bash's built-in regular expressions, but basically applies to traditional regex tools like `grep`, too. – tripleee Oct 03 '21 at 19:31

1 Answers1

1

Judging from the grep man page. It seems that if you can use things like \s you are using Perl regular expressions. Which are used when the -P option is passed to grep. So it may be that that option is set automatically on machine A and not on machine B. The reason for that may be some alias, or the option is set in GREP_OPTIONS.

heijp06
  • 11,558
  • 1
  • 40
  • 60