Sed/awk join lines once pattern found

Hi all

OS - RHEL6.4

I have input file -f1.txt

 [=129Dytt.....===..
  
 Sometxt
 \Start\now\fine step
 watch this space for tools 
 ends here 
  
 =--2323UT;;;====.......
  
 \Start\then\go language again
 cubernates space 
 sign in done 
 line again
 do it now
  
 

I need to search line which starts with

\Start

and read next line till it gets blank line and join them all. I need to trim any trailing spaces for each line.So output.txt should be..

 \Start\now\fine stepwatch this space for toolsends here 
 \Start\then\go language againcubernates spacesign in doneline againdo itnow
 

I tried ..

 sed -n '^//Start/{
 s/[ \t]*$// # trim trailing spaces
 N # Move to next line
 s/\n//p #join line and print them
 

But it goes to next line not beyond. Not sure how to keep lines in buffer till blank line comes.

Tried AWK

sed -i 's/[ \t]*$//g' f1.txt ; gawk '/^\\Start/{gsub("[\n]","",$0);print $0}RS=""' f1.txt
 

This gives me only last part

\Start\then\go language againcubernates spacesign in doneline againdo itnow
 

Can someone help me out here please?

Thanks

You can try this, it is probably a lot more complicated that in needs to be but it seems to work. It trims the leading spaces before \Start as well so if that is undesired then it can be changed.

awk '/\\Start/{f=1};!NF{if (f) print A[f];split("",A);f=0};{gsub(/^ /,"");A[f]=A[f]?A[f] $0:$0};END{if (f) print A[f]}'
1 Like

Try:

awk '/\\Start/{f=1} !NF{if(f)print s; s=x; f=0} f{s=s $0} END{if(f) print s}'  file
 \Start\now\fine step watch this space for tools  ends here 
 \Start\then\go language again cubernates space  sign in done  line again do it now

--
Note: the input file has leading spaces on every line
You can get rid of those, like this

awk 'NF{$1=$1} /^\\Start/{f=1} !NF{if(f)print s; s=x; f=0} f{s=s $0} END{if(f) print s}'

and then it will be more like you output sample:

\Start\now\fine stepwatch this space for toolsends here
\Start\then\go language againcubernates spacesign in doneline againdo it now
1 Like

Thanks both

Scrutinizer could you please help me to understand few things ..

  1. What this code is doing
!NF{if(f)print s; s=x; f=0}

. I understand here we are processing blank lines and if f is true(1) then print the string in buffer and reset the s. so here s=x is same as s=""?

  1. AS i know we can in sed or awk we can grab the pattern like ..
awk '/Start/,/^$/ {action}

here we get set of lines between start and blank line. Can we not join them some way ...This is just clear my concept.

Hi krsnadasa,

  1. Yes that is exactly right.
  2. Yes that could be done, but then you need to test inside as well which is the case, so here I think it is not that practical. I did not use /^$/ since that would not work with your sample, seeing as its "empty lines" contain spaces. So I used !NF instead .

thanks much

Solved AWK/Sed Join 

sed only:

sed -rn 's/^ *| *$//; / *\\Start/,/^$/ {H; /^$/ {g;s/\n//g;p;s/.*//;h; };}; $ {g;s/\n//g;p;}' file
\Start\now\fine stepwatch this space for tools ends here 
\Start\then\go language againcubernates space sign in done line againdo it now
1 Like

Thanks Rudi.. this is what i was looking for. To understand your code I have gone through Sed documentation and it clears many of my doubts that how sed processes data line by line and only Pattern buffer is used to manipulate while HOLD buffer is used just keep data for future reference...Thanks again