Glam Prestige Journal

Bright entertainment trends with youth appeal.

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.

3

2 Answers

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?

6

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.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy