Given a line like this: "hello my (name) is (user)
, how can I remove all '()'
using sed?
What I'm currently doing is highlighting the line using visual block, and then :s/(//g
and again for )
. Is there a way to remove both (, )
in one sed command?
My end goal is "hello my name is user"
You can use character class
[()]
as Regex pattern, and replace the pattern with empty string to remove them:So:
g
modifier does the operation on all matches, otherwisesed
will stop after the first match.You can use
tr -d '()' <infile
too.You can save the bracket content in a group and replace by that:
This saves everything inside the brackets (
[^)]*
simply matches everything except)
) in group 1 and replaces the whole bracket by it. Doing thatg
lobally means to do it for every pair of brackets in the line.Usage example