Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sed substitution including newlines

Tags:

regex

sed

awk

I want to change a text file so that any line beginning with "Length:" is appended to the previous line.

I'm aware that sed '/\nLength:/ Length:/' isn't going to work because sed is line based.

Googling for "How to match newlines in sed" did turn up a complex sed method for joining a pattern to the next line but I couldn't figure out how to adapt it.

Help would be appreciated.

like image 690
Dave Rove Avatar asked Sep 27 '22 08:09

Dave Rove


1 Answers

In awk you can use something like:

awk '/^/&&!/^Length/{printf "\n"}{printf "%s",$0}' infile

Will only print \n when line start ^ is matched. Exception: Length is found at that beginnig.

like image 158
Juan Diego Godoy Robles Avatar answered Oct 03 '22 21:10

Juan Diego Godoy Robles