You were close! Couple of notes:
- Don't use
\s
. It is a gnu extension, not available everywhere. It's better to use character classes [[:space:]]
, or really just
match a space.
- The
\+
may be misleading - in -E
mode, it matches a literal +
, while without -E
the \+
matches one or more preceding characters. The escaping depends on the mode you are using.
- You don't need to escape everything! When in
"
doublequotes, escape doublequotes "\""
, don't escape singlequotes and commas in doublequotes, "\'\,"
is interpreted as just "',"
.
If you meant only to match spaces with grep -E
:
grep -E "\"'x','a +b +c'\""
This is simple enough without -E
, just \+
instead of +
:
grep "\"'x','a \+b \+c'\""
I like to put things in front of +
inside braces, helps me read:
grep "\"'x','a[ ]\+b[ ]\+c'\""
grep -E "\"'x','a[ ]+b[ ]+c'\""
If you want to match spaces and tabs between a
and b
, you can insert a literal tab character inside [
]
with $'\t'
:
grep "\"'x','a[ "$'\t'"]\+b[ "$'\t'"]\+c'\""
grep -E "\"'x','a[ "$'\t'"]+b[ "$'\t'"]+c'\""
But with grep -P
that would just become:
grep -P "\"'x','a[ \t]+b[ \t]+c'\""
But the best is to forget about \s
and use character classes [[:space:]]
:
grep "\"'x','a[[:space:]]\+b[[:space:]]\+c'\""
grep -E "\"'x','a[[:space:]]+b[[:space:]]+c'\""