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"
3 Answers
You can use tr -d '()' <infile too.
You can use character class [()] as Regex pattern, and replace the pattern with empty string to remove them:
sed 's/[()]//g' So:
% sed 's/[()]//g' <<<"hello my (name) is (user)" hello my name is user g modifier does the operation on all matches, otherwise sed will stop after the first match.
You can save the bracket content in a group and replace by that:
sed 's/(\([^)]*\))/\1/g' This saves everything inside the brackets ([^)]* simply matches everything except )) in group 1 and replaces the whole bracket by it. Doing that globally means to do it for every pair of brackets in the line.
Usage example
$ echo '"hello my (name) is (user)"' | sed 's/(\([^)]*\))/\1/g' "hello my name is user" 3