Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
4  
Suggestion: if you just want to substitute a character for another, use tr(anslate): echo 'Hello (world)' | tr '()' '[]'; it outputs Hello [world]. –  Lekensteyn Feb 8 '13 at 18:42
add comment

1 Answer

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.

share|improve this answer
2  
Square brackets = Mind blown. Thanks –  Steel City Hacker Feb 8 '13 at 18:59
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.