Concatenate text between patterns in individual strings

In any given file, wherever a certain data block exists I need to concatenate the values(text after each "=" sign) from that block. in that block. The block starts and ends with specific pattern, say BEGIN DS and END DS respectively. The block size may vary. A file will have multiple such blocks. So say if there are 6 such blocks then I need 6 strings which have data in those respective blocks. Kindly guide.

Eg. "$file" has content :

 
Some lines of 
text
Begin DS
var1=some
var2=text 2 b concatenated 
End DS 
some more 
data
Begin DS
var1=some more
var2=text
var3=2 concatenate in 
var4=next string
End DS
:
:
so on

OUTPUT :::

 
text 2 b concatenated 
some more text 2 concatenate in next string
:

I know how to use sed for this if the block size was fixed ...

Your example output seems missing a "some" in the 1st line. Anyway:

# awk -F= '/^Begin DS/ {i++} i && /^var[0-9]=/ {a ? a=a " " $NF : a=$NF } /^End DS/ {print a; a=x; i--}' infile
some text 2 b concatenated
some more text 2 concatenate in next string
1 Like

Another one:

awk -F= '/^Begin/{s=x}NF==2{s=s?s OFS $2:$2} /^End/ && s {print s}' file
1 Like

Thanks both of you .. brilliant. Can you kindly explain your solution? It would be really helpful if there is slight change in the format of input file, plus , I am interested. :slight_smile:

Sure.

awk -F= '/^Begin/{s=x}NF==2{s=s?s OFS $2:$2} /^End/ && s {print s}' file

Explanantion:

-F=

Set "=" as field separator

/^Begin/{s=x}

If the line begins with the word "Begin" set variable s to NOTHING (dummy variable x=empty)

NF==2{s=s?s OFS $2:$2}

If there are 2 fields (records with a "=") then if s is not empty then s=s OFS $2 or s=$2
OFS is the output field separator, a built-in variable. The default value is a space.

/^End/ && s {print s}

If The line begins with the word "End" and s is not empty print s

Great. Thanks. So, to concatenate all data(not jst the part after =) in a block (all lines b/w Begin .. and end .. line ) , I tried with

 awk '/^BEGIN/{s=x} {s=s?s OFS $NR:$NR} /^End/ && s {print s}' file 

but it doesnt work ... Please guide again.

Something like this?

awk '/^Begin/{f=1}f;/^End/{f=0}' file

Oh.. it doesnt seem to work at my end ... and kindly explain your new answer if you choose to reply .. Its really nice of you to bear with me.. Thanks in advance ..

We can't guess what's the problem is without any details, we don't have a crystal ball...and it doesn't make any sense to explain every command if you don't know how awk works.

Have a read of an awk tutorial:

Awk - A Tutorial and Introduction - by Bruce Barnett

Below command just prints the file , no lines are appended ..

Regards.

What should be appended?

Please post an example of your input file and the desired output.

I/P Example :

O/P:

Thanks.

awk -F= '/^BEGIN/{f=1;next} /^END/ && s {print s; s=f=x} f{s=s?s "," $0:$0}' file