开发者

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

Using GNU sed, I n开发者_JAVA技巧eed 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)


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

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


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.


Here's yet another sed approach that keeps the complete input in the hold buffer:

# see http://austinmatzko.com/2008/04/26/sed-multi-line-search-and-replace/
echo '
()
(do not delete this)
()
(click here to
enter some text)
()
' | 
sed -n '1h;1!H;${;g;s/(\([^)]*\))/\1/g;p;}'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜