Merge multiple lines to one line when line starts with and ends with

example:

comment Now_TB.table column errac is for error messages
1 - first 
2 - second
3 -third ; 

in this example I need to be able to grab the comment as first word and ; as the last word and it might span a few lines. I need it to be put all in one line without line breaks so I can grep for comment and pull the whole line out.

comment Now_TB.table column errac is for error messages 1 - first 2 - second 3 -third ;

also there will be a lot of these in the file and other items between these... but from comment to ; all of that goes in one line.

thank you

What have you tried so far? Where are you stuck?

awk '/comment/ {F=1} F {P=P$0} !F; F&&/;/ {print P;F=0}'

sed '/^comment/,/;$/{/^[^0-9]/N;s/\n//;}' 

these dont grab more than two lines and they dont work well with other items in the file. but they are close...

1 Like
awk ' { if($0 ~ /^comment/ && NR!=1) printf "\n%s", $0; else printf "%s", $0; } END { printf "\n"; } ' infile

Try:

awk '/^comment/{ORS=FS} /;/{ORS=RS}1' file
1 Like