I am grepping through some files look for the string ‘host’ (with the single quotes). I want to capture the files that have the string without a comment (#).
The test case in the first example works just fine, but the 2nd case below does not. The 2nd case is a test file that I created, and inserted leading spaces in. So I know that there are no control characters, just whitespace.
Both servers are fairly recent versions of Linux.
What could account for the 2nd example not capturing the text? I know I can just grep for host, and then filter with grep -v the comments, but it bugs me that I don’t understand this.
/home/user2> $ cat set.txt
'host'
/home/user2> $ grep -E "^\s+'host'" set.txt
'host'
Grep on the other Linux server does not capture the desired data:
[user1@wweb1 ~]$ cat set.txt
'host'
[user1@wweb1 ~]$ grep -E "^\s+'host'" set.txt
[user1@wweb1 ~]$
Depending on
grepversion and type,\smight not work. On my system with grep 2.12, there is no mention of\s, though it does work anyway. Other versions may not have this enabled.From the man page:
Additionally,
man grep | grep "\\\\s"returns nothing at all. And that’s it: no mention of\s.Instead, you could use
[:space:]as so:So check your grep versions, and go for the syntax that works in all of them. This mailing list post says
\sdid not work in grep 2.5.1, but did work in 2.6.3, so of you have a pre-2.6.3 version, it might not work with\s.