Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sed or perl Remove outer parentheses only if first inner word matches

Tags:

regex

bash

sed

perl

Using GNU sed, I need to remove parenthetical phrases like (click here ....) including the parens. The text following click here varies, but I need to remove the whole outer parentheses.

I've tried many variations on the following, but I can't seem to hit the right one:

sed -e 's/\((click here.*[^)]*\)//'

EDIT Foolishly I didn't notice that there's often actually a linebreak in the middle of the parenthetical string, so sed probably isn't going to work. Example:

(click here to
enter some text)
like image 468
Michael Berkowski Avatar asked Dec 20 '25 22:12

Michael Berkowski


2 Answers

If there aren't nested parens, maybe you can try something like:

sed -e 's/(click here [^)]*)//'
like image 63
sidyll Avatar answered Dec 22 '25 15:12

sidyll


With perl you can run :

perl -00 -pe "s/\(click here [^)]*\)//g" inputfile > outputfile

It will read the inputfile in a string then replace all occurrences of (click here anychar but '(' ) then output all in the outputfile.

like image 26
Toto Avatar answered Dec 22 '25 16:12

Toto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!