I want to search-replace something containing whitespace on a bash command line, and I assumed sed would be the easiest way to go.
Using [ \t]
denoting either tab or space, to match the whitespace, works as intended:
echo "abc xyz" | sed "s/[ \t]xyz/123/"
abc123
But using \s
instead of [ \t]
does not, to my surprise:
echo "abc xyz" | sed "s/\sxyz/123/"
abc xyz
I'm fairly new to bash so I might be missing something trivial, but no matter what I do, I can't get this to work. Using \\s
instead of \s
, using single quotes ('
instead of "
), putting the whitespace marker inside square brackets (like [\s]
or even [\\s]
), nothing seems to help..?
(edit) in case it differs from one sed / bash version to another: I'm working on OS X here.
Additionally, I noticed that when I add a +
after the [ \t]
whitespace part, to optionally grab multiple space/tab characters if present, it doesn't work anymore either...??
echo "abc xyz" | sed "s/[ \t]+xyz/123/"
abc xyz
(and again, also tried with \+
instead of +
, and single quotes instead of double quotes, nothing helps)
sed
. Could you provide the output ofsed --version
? – Pompeiiman sed
at a command prompt. – Trovillionsed --version
saysillegal option -- -
so I guess it's not GNU :) – Myxomycete