searching and storing unknown number of lines based on the string with a condition

Dear friends, Please help me to resolve the problem below,

I have a file with following content:

date of file creation : 12 feb 2007

= name : suresh
= city :mumbai
#this is a blank line
= date : 1st Nov 2005

few lines of some text
this text could be of any number of lines

= code : 10
= time : 10AM
= job : dev

====================
= name : mahesh
= city :Bangalore
#this is a blank line
= date : 1st april 2005

few lines of some text
this text could be of any number of lines

= code : 0
= time : 11AM
= job : test

====================
= name : Girish
= city :Pune
#this is a blank line
= date : 4april 2005

few lines of some text
this text could be of any number of lines

= code : 15
= time : 12AM
= job : support

#end of file

in the above data, it contains 3 records(marked using blue, green and magenta colours).
now the problem is, I need to store all the line of a record which is having the "=code : 10" in a file file_10
and
"=code : 15" in a file file_15.
and
if the "=code : 0" then I need to ignore that record and continure with next record till the end of file.

Please help me.

thanks in advance,
swamymns

hello please try this.

hope this helps you out :slight_smile:

please runthis from a script.

awk ' /^= name*/,/^= job*/ { indx += 1; arr[indx]=$0; } \
/^= code : 10/ { match_found_10="YES"; } \
/^= code : 15/ { match_found_15="YES"; }\
/^= job*/ {if(match_found_10=="YES") { \
print "======================" >"code_10_match_file"; \
for(i=1; i<=indx; i++) \
print arr [i]>"code_10_match_file"; \
print "======================" >"code_10_match_file";\
}\
if(match_found_15=="YES") { \
print "======================" >"code_15_match_file"; \
for(i=1; i<=indx; i++) \
print arr [i]>"code_15_match_file"; \
print "======================" >"code_15_match_file";\
}\
match_found_10="NO"; indx = 0; match_found_15="NO"; }\
' input_file_name

awk 'BEGIN{RS=""}
$0 ~ /code : 10/{   
   print $0 > "file_10"
}' file

Hi Pradee and friends,
I need some modification in the above script.

Hi Guys, thanks for your reply,

awk ' /^= name*/,/^= job*/ { indx += 1; arr[indx]=$0; } \
/^= code : 10/ { match_found_10="YES"; } \
/^= code : 15/ { match_found_15="YES"; }\
/^= job*/ {if(match_found_10=="YES") { \
print "======================" >"code_10_match_file"; \
for(i=1; i<=indx; i++) \
print arr [i]>"code_10_match_file"; \
print "======================" >"code_10_match_file";\
}\
if(match_found_15=="YES") { \
print "======================" >"code_15_match_file"; \
for(i=1; i<=indx; i++) \
print arr [i]>"code_15_match_file"; \
print "======================" >"code_15_match_file";\
}\
match_found_10="NO"; indx = 0; match_found_15="NO"; }\
' input_file_name

above script is ok, but I need this for generic case, i.e., I need to cluster the records having similar "code" into individual files,

for example,
all the records having code=10 need to be stored in a file called code_10 file and
all the records having code=15 need to be stored in a file called code_15 file ...etc.

the code number varied from 0 to 255.
and if the code is 0 then we can ignore those recors.

and if input file contains codes 0,10,15,20, then the script should generate only 3 files(for 10, 15,20) ignoring 0.

Please help me

Thanks in advance
swamymns

Dear friends,

Please help me the above stated problem.

Urgent responses are very much appreciated .

Thanks in advance,
swamymns

Adapted from ghostdog74's solution

awk 'BEGIN{RS=""}
!/code : 0/ {
        match ($0, /code : [0-9]+/, a)
        print $0 > "/tmp/file_" gensub(/code : /, "", 1, a[0])
   }'

Friends,
the soution does not work. please let me know how to put loop in awk command to resolve the issue.
regards.