![]() |
|
|
|
|
|||||||
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. Shell Script Page. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Creating an unknown number of arrays | ajgwin | Shell Programming and Scripting | 2 | 05-23-2008 12:49 PM |
| sed - searching for string and storing in variable | melias | Shell Programming and Scripting | 4 | 04-12-2008 11:57 AM |
| How to split the String based on condition? | sankar reddy | Shell Programming and Scripting | 2 | 03-19-2008 03:48 AM |
| printf returning unknown number | m0nk3y | Shell Programming and Scripting | 1 | 08-08-2005 08:31 AM |
| How can I get an if statement to execute based on number of lines in a file? | LordJezo | Shell Programming and Scripting | 6 | 05-14-2004 07:50 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
|||
|
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 |
|
|||
|
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 |
| Thread Tools | |
| Display Modes | |
|
|