I know sed 's/^/"/g'
works for the beginning of a line, and sed 's/$/"/g'
works for the end, but why doesn't sed 's/[^$]/"/g'
work for both?
[^$]
means "any character except the dollar sign". So saying sed 's/[^$]/"/g'
you are replacing all characters with "
, except $
(credits to Ed Morton):
$ echo 'he^llo$you' | sed 's/[^$]/"/g'
""""""$"""
To say: match either ^
or $
, you need to use the ( | )
expression:
sed 's/\(^\|$\)/"/g' file
or, if you have -r
in your sed
:
sed -r 's/(^|$)/"/g' file
$ cat a
hello
bye
$ sed -r 's/(^|$)/"/g' a
"hello"
"bye"
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