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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting searching and storing unknown number of lines based on the string with a condition
# 1  
Old 05-01-2008
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
# 2  
Old 05-01-2008
hello please try this.

hope this helps you out :-)

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
# 3  
Old 05-01-2008
Code:
awk 'BEGIN{RS=""}
$0 ~ /code : 10/{   
   print $0 > "file_10"
}' file

# 4  
Old 05-04-2008
Hi Pradee and friends,
I need some modification in the above script.

Last edited by swamymns; 05-06-2008 at 02:04 AM..
# 5  
Old 05-06-2008
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
# 6  
Old 05-09-2008
Dear friends,

Please help me the above stated problem.

Urgent responses are very much appreciated .

Thanks in advance,
swamymns
# 7  
Old 05-09-2008
Adapted from ghostdog74's solution

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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to reformat lines based on condition

The awk below uses the tab-delimeted fileand reformats each line based on one of three conditions (rules). The 3 rules are for deletion (lines in blue), snv (line in red), and insertion (lines in green). I have included all possible combinations of lines from my actual data, which is very large.... (0 Replies)
Discussion started by: cmccabe
0 Replies

2. Shell Programming and Scripting

Print lines based on line number and specified condition

Hi, I have a file like below. 1,2,3,4,5,6,7,8,9I would like to print or copied to a file based of line count in perl If I gave a condition 1 to 3 then it should iterate over above file and print 1 to 3 and then again 1 to 3 etc. output should be 1,2,3 4,5,6 7,8,9 (10 Replies)
Discussion started by: Anjan1
10 Replies

3. Shell Programming and Scripting

Delete lines from file based on condition

I want to keep last 2 days data from a file and want to delete others data from the file. Please help me. Sample Input # cat messages-2 Apr 15 11:25:03 test1 kernel: imklog 4.6.2, log source = /proc/kmsg started. Apr 15 11:25:03 test1 rsyslogd: (re)start Apr 16 19:42:03 test1 kernel:... (2 Replies)
Discussion started by: makauser
2 Replies

4. Shell Programming and Scripting

Print certain lines based on condition

Hi All, I have following listing Filesystem GB blocks Free Used Iused Iused Mounted on /dev/hd2 4.00 0.31 93 63080 43 /usr Filesystem GB blocks Free Used Iused Iused Mounted on Filesystem GB blocks Free Used Iused Iused... (11 Replies)
Discussion started by: ckwan
11 Replies

5. Shell Programming and Scripting

Arithmetic (number-based) if condition

Hi Folks I'm looking for help with if statement. I'm reading the file with header (starts with 0 on position 1 in the line) and data (starts with 1 on position 1 in the line). I have to check if the number from header (should be number of data rows) equal actual count of the data rows. ... (4 Replies)
Discussion started by: viallos
4 Replies

6. Shell Programming and Scripting

Merge two non-consecutive lines based on line number or string

This is a variation of an earlier post found here: unixcom/shell-programming-scripting/159821-merge-two-non-consecutive-lines.html User Bartus11 was kind enough to solve that example. Previously, I needed help combining two lines that are non-consecutive in a file. Now I need to do the... (7 Replies)
Discussion started by: munkee
7 Replies

7. Shell Programming and Scripting

Remove lines from XML based on condition

Hi, I need to remove some lines from an XML file is the value within a tag is empty. Imagine this scenario, <acd><acdID>2</acdID><logon></logon></acd> <acd><acdID></acdID><logon></logon></acd> <acd><acdID></acdID><logon></logon></acd> <acd><acdID></acdID><logon></logon></acd> I... (3 Replies)
Discussion started by: giles.cardew
3 Replies

8. Shell Programming and Scripting

awk to print lines based on string match on another line and condition

Hi folks, I have a text file that I need to parse, and I cant figure it out. The source is a report breaking down softwares from various companies with some basic info about them (see source snippet below). Ultimately what I want is an excel sheet with only Adobe and Microsoft software name and... (5 Replies)
Discussion started by: rowie718
5 Replies

9. Shell Programming and Scripting

sed - searching for string and storing in variable

Hi I'm trying to find a way to search a text file for a specific string. I have a file which contains i.p. addresses and port numbers in the following format: 'ip="www.xxx.yyy.zzz"' 'port="xx""' I want to print only the parts between the double quotes for use in seperate variables,... (4 Replies)
Discussion started by: melias
4 Replies

10. Shell Programming and Scripting

How to split the String based on condition?

hi , I have a String str="/opt/ibm/lotus/ibw/latest" or ="/opt/lotus/ibw/latest" this value is dynamic..I want to split this string into 2 strings 1. /opt/ibm/lotus(/opt/lotus) this string must ends with "lotus" 2./ibw/latest can any body help me on this? Regards, sankar (2 Replies)
Discussion started by: sankar reddy
2 Replies
Login or Register to Ask a Question