You can match a space character with just the space character; [^ ] matches anything but a space character.
To match any character except a list of excluded characters, put the excluded charaters between [^ and ] . The caret ^ must immediately follow the [ or else it stands for just itself. The character '. ' (period) is a metacharacter (it sometimes has a special meaning).
The \S metacharacter matches non-whitespace characters. Whitespace characters can be: A space character.
Non-whitespace character: \S.
You can use a character class:
/[^\s\\]/
matches anything that is not a whitespace character nor a \
. Here's another example:
[abc]
means "match a
, b
or c
"; [^abc]
means "match any character except a
, b
or c
".
You can use a lookahead:
/(?=\S)[^\\]/
This worked for me using sed [Edit: comment below points out sed doesn't support \s]
[^ ]
while
[^\s]
didn't
# Delete everything except space and 'g'
echo "ghai ghai" | sed "s/[^\sg]//g"
gg
echo "ghai ghai" | sed "s/[^ g]//g"
g g
On my system: CentOS 5
I can use \s
outside of collections but have to use [:space:]
inside of collections. In fact I can use [:space:]
only inside collections. So to match a single space using this I have to use [[:space:]]
Which is really strange.
echo a b cX | sed -r "s/(a\sb[[:space:]]c[^[:space:]])/Result: \1/"
Result: a b cX
\s
[[:space:]]
[^[:space:]]
These two will not work:
a[:space:]b instead use a\sb or a[[:space:]]b
a[^\s]b instead use a[^[:space:]]b
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With