Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sed remove trailing space after bracket

I am trying to remove trailing space after brackets in an HTML file using sed (part of a shell script on CentOS):

from this:

<p>Some text (
<em>Text which should not break to a new line</em>). More text.</p>

to this:

<p>Some text (<em>Text which should not break to a new line</em>). More text.</p>

I can easily do it in Sublime Text with \(\s REGEX and replacing it with a bracket but this does not work in sed.

I have tried:

sed 's/[(]\s*$/(/'
sed 's/[(]\s*$\n/(/'

and many other things, but none of them work.

Any ideas?

like image 590
Coroner Avatar asked Apr 21 '26 06:04

Coroner


1 Answers

Try:

sed ':a;/($/{N;s/\n//;ba}' file

If the line ends with (, appends the next line (N) to the pattern space and then substitutes the newline character \n with nothing, thereby joining the lines. This is done in a loop (ba jumps back to label a).

like image 126
dogbane Avatar answered Apr 23 '26 20:04

dogbane