I want to use with GNU sed to replace \n by \r when each line (comming from a pipe) begins with a specified pattern but my sed command does not seem to work :
$ cat mpv_all.log | sed -z '/^AV:/s/\n/\r/g'
Can you please help me ?
The issue is that -z
causes the whole file to be read in at once. Consequently, ^AV:
only matches if the file starts with AV:
. You probably want to match AV:
on each line. In that case, try:
sed -Ez ':a; s/((^|[\n\r])AV:[^\n\r]*)\n/\1\r/g ;ta' mpv_all.log
How it works:
-E
turns on Extended Regular Expressions.
:a
defines label a
.
s/((^|[\n\r])AV:[^\n\r]*)\n/\1\r/g
replaced \n
with \r
at the end of any line that starts with AV:
.
ta
tells sed to go back to label a
if the previous substitute command resulted in any changes. This is necessary because patterns could overlap and the g
modifier to the s
command does not do overlaps.