Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm trying to follow a section in The GNU Grep Manual but my expression isn't working. Is it my grep version? Red Hat 5.5.

$ rm file
$ echo 'hello world' > file
$ grep 'hello world' file
hello world
$ grep 'hello\sworld' file
$

Returns nothing. grep-2.5.1-55.el5

share|improve this question
1  
Works with grep (GNU grep) 2.14 –  Hauke Laging Feb 21 at 0:02
1  
Does grep 'hello[[:space:]]world' file work? –  Ketan Feb 21 at 0:13
    
[:space:] and [:blank:] work. But why doesn't \s work? –  Felipe Alvarez Feb 21 at 1:25

3 Answers 3

up vote 2 down vote accepted

The special handling of \s was added in grep 2.6.3. Commit message

Whenever possible, use the info docs that are on your system. Just run info grep.

To get this behavior, try adding the -P flag to make it work in Perl regex mode.

share|improve this answer
    
I knew it must be the version number. I'll need to make do with this for now. Thanks to all contributions. –  Felipe Alvarez Feb 21 at 2:40

I could reproduce your issue with GNU grep v2.5.1.

However, when I use -i switch it worked:

grep -i 'hello\sworld' file

From the man page:

-i, --ignore-case Ignore case distinctions in both the PATTERN and the input files.

It might be a bug in grep.

share|improve this answer
    
You are right. -i causes it to return the result I want. Curiously though, without -i, both \S and \s both fail to return expected results. –  Felipe Alvarez Feb 21 at 2:42

You should use "" instead of '' :

grep "hello\sworld" file

When you use '' grep discardes scape codes.

share|improve this answer
    
double quotes returns the same result (nothing) –  Felipe Alvarez Feb 21 at 1:24
    
This is not true, the string is exactly the same in both types of quotes in this instance. –  Chris Down Feb 21 at 2:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.