Dealing with whitespace in grep

I'm trying to figure out how to deal with whitespace in grep. How do I tell grep to find strings containing whitespace or tabs? The manual tells me nothing. \s seems to work for whitespace, and \S seems to work for non-whitespace, but it includes all whitespace characters (spaces AND tabs) and it doesn't work if I put it in brackets, treating the backslash and the \s as separate characters.


Solution 1:

Are you sure about that?

$ printf "a \tb\na b\na\tb" | grep '.\s*.'
a       b
a b
a       b

$ grep -V
grep (GNU grep) 2.14
Copyright (C) 2012 Free Software Foundation, Inc.

I.e., as shown, \s has matched both spaces and tabs -- I included the 'a' and 'b' just to highlight it.

What do you get?

Solution 2:

In my experience grep works best with POSIX character classes - look up [[:space:]] for instance. I use grep extensively in some programs for user input validation and have never had a problem if I stuck to POSIX classes.

However, as commenters have noted, your question is not entirely clear.