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
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.
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
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.