![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Small Search script | appu1987 | Shell Programming and Scripting | 2 | 06-03-2008 11:14 PM |
| script to search a file | dr46014 | Shell Programming and Scripting | 1 | 02-28-2008 06:55 AM |
| Help with search script | bsandeep_80 | UNIX for Advanced & Expert Users | 9 | 09-06-2007 09:25 AM |
| Search File Script | I4ce | Shell Programming and Scripting | 7 | 03-28-2006 03:12 PM |
| script to search all the directories | abk | Shell Programming and Scripting | 3 | 07-10-2002 01:19 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
Search script
I need to create a script called 'find_data' that will search a file by passing two parameters to it
. I'm pretty good with scripting but haven't really gotten into sed and awk that much (and no perl whatsoever). I'm really at a loss and need to get something working in the next couple days (yes, we still do work on weekends).The script should work like this. find_data <parameter 1> <parameter 2> The first parameter would be the data before the colon (':'). The second parameter would be the data after the colon (':') If both are found the data between the preceeding BEGINJOB and ENDJOB (inclusive) should be displayed to stdout. Examples using the data: 1 find_data start 01:15 would find, and display, the first set of data. 2 find_data days tu would find, and display, the last set of data. 3 find_data days all would find, and display, the 2nd, 3rd and 4th sets of data. I really appreciate your help with this. It's a great site. Sample data (only five shown but there are several hundred): BEGINJOB--------------------------- insert_job: cir_cdezp07b job_type: c command: /apps/usr/circ/bin/cdezp07b.cmd days_of_week: all start_times: "01:15" description: "Mass Zip Code Update" ENDJOB BEGINJOB--------------------------- insert_job: cir_ccsds03b job_type: c command: /apps/usr/circ/bin/ccsds03b.cmd days_of_week: all start_times: "02:00" description: "Deletes Info. from Dispatch_table" ENDJOB BEGINJOB--------------------------- insert_job: cir_ccsda78r job_type: c command: /apps/usr/circ/bin/ccsda78r.cmd REP days_of_week: all start_times: "03:00" description: "Daily Reports" ENDJOB BEGINJOB--------------------------- insert_job: cir_ccsda95i job_type: c command: /apps/usr/circ/bin/ccsda95i.cmd 6 1000 0 25 days_of_week: all start_times: "04:00" condition: s (cir_ccbdd01b_daily) & s (cir_ccbdd01b_sat) & s (cir_ccbdd01b_sun) description: "Archive job, runs M-SU" ENDJOB BEGINJOB--------------------------- insert_job: cir_cdtsm01i job_type: c command: /apps/usr/circ/bin/cdtsm01i.cmd days_of_week: tu,we,th,fr,sa start_times: "04:00" description: "Templated Solo Mail - aoets20b.tx to aoets20b_txt" ENDJOB |
|
||||
|
You could split the file into job arrays Code:
open (FILE,"/path/to/file");
@LINES=<FILE>;
close(FILE);
$count = 0;
foreach $_ (@LINES) {
$curarray = "array".$count;
if ($_ =~ /ENDJOB/) {$count++}
push (@$curarray,$_);
}
then just check each array for what you are looking for and print it out if it matches. |
|
|||||
|
Code:
awk 'BEGIN{RS="(BEGIN|END)JOB-*"}$0~(x ".*:.*" y)' x=days y=tu file1
...gives... Code:
insert_job: cir_cdtsm01i job_type: c command: /apps/usr/circ/bin/cdtsm01i.cmd days_of_week: tu,we,th,fr,sa start_times: "04:00" description: "Templated Solo Mail - aoets20b.tx to aoets20b_txt" |
|
|||||
|
Code:
awk -v Before="$1" -v After="$2" '
BEGIN {
FS = ":";
Before_Pattern = ".*" Before ".*";
After_Pattern = ":.*" After ".*";
}
/^BEGINJOB/,/^ENDJOB/ {
Memo[++Line_Count] = $0;
if ($1 ~ Before_Pattern) debug "Before match";
if ($1 ~ After_Pattern) debug "After match";
if ($1 ~ Before_Pattern && $0 ~ After_Pattern)
Print_Datas = 1;
}
/^ENDJOB/ {
if (Print_Datas) {
for (i=1; i<=Line_Count; i++)
print Memo[i];
}
Line_Count = 0;
Print_Datas = 0;
}
' input.txt
Code:
$ find_data days tu BEGINJOB--------------------------- insert_job: cir_cdtsm01i job_type: c command: /apps/usr/circ/bin/cdtsm01i.cmd days_of_week: tu,we,th,fr,sa start_times: "04:00" description: "Templated Solo Mail - aoets20b.tx to aoets20b_txt" ENDJOB |
|
||||
|
Getting errors
Thanks aigles but I'm getting errors when I execute this. Here's the script. And the errors I'm getting (btw, I'm running this on a Solaris server).
Script: #! /bin/ksh # find_script awk -v Before="$1" -v After="$2" ' BEGIN { FS = ":"; Before_Pattern = ".*" Before ".*"; After_Pattern = ":.*" After ".*"; } /^BEGINJOB/,/^ENDJOB/ { Memo[++Line_Count] = $0; if ($1 ~ Before_Pattern) debug "Before match"; if ($1 ~ After_Pattern) debug "After match"; if ($1 ~ Before_Pattern && $0 ~ After_Pattern) Print_Datas = 1; } /^ENDJOB/ { if (Print_Datas) { for (i=1; i<=Line_Count; i++) print Memo[i]; } Line_Count = 0; Print_Datas = 0; } ' input.txt exit 0 Errors: % find_script days tu awk: syntax error near line 1 awk: bailing out near line 1 % If I change this: awk -f circjobs1 -v Before="$1" -v After="$2" ' the errors are: % find_script days tu awk: syntax error near line 3 awk: bailing out near line 3 awk: newline in string near line 317 awk: newline in string near line 3844 awk: newline in string near line 4758 awk: newline in string near line 5411 awk: newline in string near line 5816 awk: newline in string near line 9824 awk: newline in string near line 10337 awk: newline in string near line 10517 awk: newline in string near line 11808 % Last edited by BCarlson; 02-03-2006 at 09:21 AM.. |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|