I have to replace (
with some character in my file, and I can't do it. \(
is used for grouping in sed and when I used \\(
with sed, it treated it as \(
character not as just (
. It seems like a tricky case to me.
Asked
Active
Viewed 6.9k times
1 Answers
77
If you use sed without -r
(extended regular expressions) it will work as it uses \(\)
rather than ()
for grouping:
sed -e 's/(/X/g'
For example
$ echo "foo (bar) (baz)" | sed -e 's/(/X/g'
foo Xbar) Xbaz)
If you want to use -r
you can put (
inside []
, e.g.
sed -re 's/[(]/X/g'
As Lekensteyn remarks using tr
might be more appropriate as you are merely replacing and you do not need the full power of regular expressions.
-
7
-
I used this to replace php's deprecated "split" with "explode" function, making sure "split" is preceded by a space and followed by a "(".
grep -lRZ " split(" . | xargs -0 sed -i -e 's/ split(/ explode(/g'
– Buttle Butkus Jun 04 '16 at 02:01
tr
(anslate):echo 'Hello (world)' | tr '()' '[]'
; it outputsHello [world]
. – Lekensteyn Feb 08 '13 at 18:42