egrep search for whitespace egrep search for whitespace unix unix

egrep search for whitespace


I see the same issue on SunOS 5.10. /usr/bin/egrep does not support extended regular expressions.

Try using /usr/xpg4/bin/egrep:

$ echo 'this line has whitespacethislinedoesnthave' | /usr/xpg4/bin/egrep '[[:space:]]'this line has whitespace

Another option might be to just use perl:

$ echo 'this line has whitespacethislinedoesnthave' | perl -ne 'chomp;print "$_\n" if /[[:space:]]/'this line has whitespace


If you're using 'degraded' versions of grep (I quote the term because most UNIX'es I work on still use the original REs, not those fancy ones with "\s" or "[[:space:]]" :-), you can just revert to the lowest form of RE.

For example, if :space: is defined as spaces and tabs, just use:

egrep '[ ^I]' file

That ^I is an actual tab character, not the two characters ^ and I.

This is assuming :space: is defined as tabs and spaces, otherwise adjust the choices within the [] characters.

The advantage of using degraded REs is that they should work on all platforms (at least for ASCII; Unicode or non-English languages may have different rules but I rarely find a need).


If you are using bash, then syntax to put a tab in a line is

$'foo\tbar'

I was recently working with sed to do some fixups on a tab-delimited file. Part of the file was:

sed -E -e $'s/\t--QUOTE--/\t"/g'

That argument is parsed by bash, and sed sees a regex with literal tabs.