Search between two strings for multiple occurances

i search between two strings viz <app-deployment> & </app-deployment> and save the contents in a new file using the code snippet below.

sed -n "/<app-deployment/,/<\/app-deployment>/p" deploy.tmp >found1.tmp

But if the search string apprears more than once in the file then how can i store the text between each occurance in a seperate file ?

Thus, i need the below to go in found1.tmp

and the below to go in found2.tmp

Hello mohtashims,

Could you please try following and let me know if this helps.

awk '/<app-deployment>/{A=1;++i} A{;print >> "found"i".tmp"}'  Input_file

Output files will be as follows.

cat found2.tmp
<app-deployment>
<name>Security</name>
<target>CS2</target>
<module-type>ear</module-type>
</app-deployment>
  
cat found1.tmp
<app-deployment>
<name>cert</name>
<target>CS1</target>
<module-type>war</module-type>
</app-deployment>
 

Thanks,
R. Singh

I dont think u r searching between two strings but looking for only one string. Note the contents of deploy.tmp may change.

Hello mohtashims,

I was editing that post, following is the code where I am looking for both the strings, let me know how it goes then.

awk '/<app-deployment>/{A=1;++i} /<\/app-deployment>/{print >> "found"i".tmp";A=0} A{;print >> "found"i".tmp"}'  Input_file

Thanks,
R. Singh

1 Like