I have a text file and I need to remove newlines if they are followed by the string "fox"
the
brown
fox
jumps
will become
the
brown fox
jumps
I would like to do it in SED, but the AWK solution would be useful too.
I have a text file and I need to remove newlines if they are followed by the string "fox"
the
brown
fox
jumps
will become
the
brown fox
jumps
I would like to do it in SED, but the AWK solution would be useful too.
This might work for you (GNU sed):
sed ':a;N;/\nfox/s/\n//;ta;P;D' file
Read two lines into the pattern space and if the second line matches the criteria, remove the newline and repeat. The first line is always printed and then deleted. If the pattern space still has a line in it i.e. the criteria was not matched, another line is appended etc however if the line did meet the criteria the pattern space is empty and two lines will be read in as they would be such as at the beginning of the file.
With Perl:
perl -0pe 's/\nfox/fox/g' file
Output:
the brown fox jumps
This is not a job for sed, it is a job for awk:
$ awk 'NR>1{printf "%s", (/fox/ ? OFS : ORS)} {printf "%s", $0} END{print ""}' file
the
brown fox
jumps
The above replaces the newline (ORS) before fox with a blank char (OFS). Massage to suit...
With GNU awk you can alternatively reduce it to:
$ awk -v RS='^$' -v ORS= '{gsub(/\nfox/," fox")} 1' file
the
brown fox
jumps
or:
$ awk -v RS='\nfox' '{ORS=gensub(/\n/," ",1,RT)} 1' file
the
brown fox
jumps
but that reads the whole file into memory at one time.