Deleting parentheses in shell script
Clash Royale CLAN TAG#URR8PPP
up vote
3
down vote
favorite
I would like to create mechanism to delete parentheses, or parentheses and the text between them. For example:
before:
text0 text1 text2 (text3 text4)
after:
text0 text1 text2 text3 text4
or:
text0 text1 text2
I would like to test both options and other type of brackets but I'm not sure what tool should I use, Awk or Sed or maybe something else? I would be grateful for any advice.
shell-script text-processing awk sed
add a comment |Â
up vote
3
down vote
favorite
I would like to create mechanism to delete parentheses, or parentheses and the text between them. For example:
before:
text0 text1 text2 (text3 text4)
after:
text0 text1 text2 text3 text4
or:
text0 text1 text2
I would like to test both options and other type of brackets but I'm not sure what tool should I use, Awk or Sed or maybe something else? I would be grateful for any advice.
shell-script text-processing awk sed
Can brackets be nested?
â Weijun Zhou
Jan 30 at 20:00
Good question. Let's say yes but only brackets that are deeply nested - file may be json and curly braces will be preseted in every case.
â SeSa
Jan 30 at 20:14
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I would like to create mechanism to delete parentheses, or parentheses and the text between them. For example:
before:
text0 text1 text2 (text3 text4)
after:
text0 text1 text2 text3 text4
or:
text0 text1 text2
I would like to test both options and other type of brackets but I'm not sure what tool should I use, Awk or Sed or maybe something else? I would be grateful for any advice.
shell-script text-processing awk sed
I would like to create mechanism to delete parentheses, or parentheses and the text between them. For example:
before:
text0 text1 text2 (text3 text4)
after:
text0 text1 text2 text3 text4
or:
text0 text1 text2
I would like to test both options and other type of brackets but I'm not sure what tool should I use, Awk or Sed or maybe something else? I would be grateful for any advice.
shell-script text-processing awk sed
edited Feb 2 at 11:47
Vlastimil
6,4011146119
6,4011146119
asked Jan 30 at 19:53
SeSa
284
284
Can brackets be nested?
â Weijun Zhou
Jan 30 at 20:00
Good question. Let's say yes but only brackets that are deeply nested - file may be json and curly braces will be preseted in every case.
â SeSa
Jan 30 at 20:14
add a comment |Â
Can brackets be nested?
â Weijun Zhou
Jan 30 at 20:00
Good question. Let's say yes but only brackets that are deeply nested - file may be json and curly braces will be preseted in every case.
â SeSa
Jan 30 at 20:14
Can brackets be nested?
â Weijun Zhou
Jan 30 at 20:00
Can brackets be nested?
â Weijun Zhou
Jan 30 at 20:00
Good question. Let's say yes but only brackets that are deeply nested - file may be json and curly braces will be preseted in every case.
â SeSa
Jan 30 at 20:14
Good question. Let's say yes but only brackets that are deeply nested - file may be json and curly braces will be preseted in every case.
â SeSa
Jan 30 at 20:14
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
4
down vote
accepted
The other half of your question,
deleting just the parentheses (but not the text between them):
echo 'text0 text1 text2 (text3 text4)' |
sed 's/[()]//g'
Output:
text0 text1 text2 text3 text4
add a comment |Â
up vote
5
down vote
To keep the text, this is enough:
tr -d '()'
To delete the text and parentheses:
sed 's/([^)]*)//g;s/ / /g'
If the text contains nested parenthesis like :
echo 'text0 (text1 (textA )) text2 (text3 text4) test5' |
sed -e :A -e 's/([^()]*)//;tA' -e 's/ / /g'
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
The other half of your question,
deleting just the parentheses (but not the text between them):
echo 'text0 text1 text2 (text3 text4)' |
sed 's/[()]//g'
Output:
text0 text1 text2 text3 text4
add a comment |Â
up vote
4
down vote
accepted
The other half of your question,
deleting just the parentheses (but not the text between them):
echo 'text0 text1 text2 (text3 text4)' |
sed 's/[()]//g'
Output:
text0 text1 text2 text3 text4
add a comment |Â
up vote
4
down vote
accepted
up vote
4
down vote
accepted
The other half of your question,
deleting just the parentheses (but not the text between them):
echo 'text0 text1 text2 (text3 text4)' |
sed 's/[()]//g'
Output:
text0 text1 text2 text3 text4
The other half of your question,
deleting just the parentheses (but not the text between them):
echo 'text0 text1 text2 (text3 text4)' |
sed 's/[()]//g'
Output:
text0 text1 text2 text3 text4
edited Feb 2 at 10:15
Henk Langeveld
547213
547213
answered Jan 31 at 1:23
G-Man
11.5k82657
11.5k82657
add a comment |Â
add a comment |Â
up vote
5
down vote
To keep the text, this is enough:
tr -d '()'
To delete the text and parentheses:
sed 's/([^)]*)//g;s/ / /g'
If the text contains nested parenthesis like :
echo 'text0 (text1 (textA )) text2 (text3 text4) test5' |
sed -e :A -e 's/([^()]*)//;tA' -e 's/ / /g'
add a comment |Â
up vote
5
down vote
To keep the text, this is enough:
tr -d '()'
To delete the text and parentheses:
sed 's/([^)]*)//g;s/ / /g'
If the text contains nested parenthesis like :
echo 'text0 (text1 (textA )) text2 (text3 text4) test5' |
sed -e :A -e 's/([^()]*)//;tA' -e 's/ / /g'
add a comment |Â
up vote
5
down vote
up vote
5
down vote
To keep the text, this is enough:
tr -d '()'
To delete the text and parentheses:
sed 's/([^)]*)//g;s/ / /g'
If the text contains nested parenthesis like :
echo 'text0 (text1 (textA )) text2 (text3 text4) test5' |
sed -e :A -e 's/([^()]*)//;tA' -e 's/ / /g'
To keep the text, this is enough:
tr -d '()'
To delete the text and parentheses:
sed 's/([^)]*)//g;s/ / /g'
If the text contains nested parenthesis like :
echo 'text0 (text1 (textA )) text2 (text3 text4) test5' |
sed -e :A -e 's/([^()]*)//;tA' -e 's/ / /g'
edited Feb 2 at 13:16
Stéphane Chazelas
281k53516847
281k53516847
answered Jan 31 at 20:59
ctac_
1,016116
1,016116
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f420778%2fdeleting-parentheses-in-shell-script%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Can brackets be nested?
â Weijun Zhou
Jan 30 at 20:00
Good question. Let's say yes but only brackets that are deeply nested - file may be json and curly braces will be preseted in every case.
â SeSa
Jan 30 at 20:14