Multiple regex in sed

I am using the following sed script to remove new lines ( \r\n and \n ), except from lines starting with > :

sed -i ':a /^>/!N;s/\r\n\([^>]\)/\1/;s/\n\([^>]\)/\1/;ta' 

Is there a way to include both \r\n and \n in one regex to avoid the second substitute script ( s/\n\([^>]\)/\1/ )?

Give it a try as \r?\n

This did not work:

sed ':a /^>/!N;s/\r\n?\n\([^>]\)/\1/;ta'

Or this:

sed ':a /^>/!N;s/\r?\n\([^>]\)/\1/;ta'

Try:

GNU sed:

sed ':a /^>/!N;s/\r\?\n\([^>]\)/\1/;ta' file
sed ':a /^>/!N;s/\r\{0,1\}\n\([^>]\)/\1/;ta' file
sed -r ':a 1!N;s/\r?\n([^>])/\1/;ta' file

---
Note:
The above approaches will not work if there is a carriage return on the last line
Instead try this:

sed -r ':a 1!N;s/\r$|\r?\n([^>])/\1/g;ta' file
1 Like

Thanks a TON! That worked like a charm