Glam Prestige Journal

Bright entertainment trends with youth appeal.

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"

2

3 Answers

You can use tr -d '()' <infile too.

0

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

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy