apply sed command only if no dot before

Viewed 18

My sed command does not work as expected.

sed -E ':a;N;$!ba;s/[^\.](\[[0-9]]) \n\n/\1 /g'

I want this :

blabla[3]

foofoo

barbar.[4]

blabla

To become :

blabla[3] foofoo

barbar.[4]

blabla

That is, I just want the new lines to be deleted when there is no dot before "[".

But my sed command does not take into account my [^\.].

Without [^\.], I get :

blabla[3] foofoo

barbar.[4] blabla
1 Answers

You may use this sed (should work with non-gnu sed also):

sed -E -e ':a' -e 'N;$!ba' -e 's/((^|[^.])\[[0-9]+]) *\n\n/\1 /g' file
blabla[3] foofoo

barbar.[4]

blabla

Code Demo

Note that we are matching (^|[^.]) to allow [<digits>] to appear at the line start as well. It is also important to keep this part in capture group so that we don't miss out on this char in replacement.

Related