Search script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search script
# 1  
Old 02-02-2006
Search script

I need to create a script called 'find_data' that will search a file by passing two parameters to it Smilie . 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
# 2  
Old 02-02-2006
frankly you lost me, I am sorry if I am not able to understand this, but I dont understand the above scenario.
The output is a part of file ? (if yes, is this file same as input file ?)
what is parameter and what is data, where is data, in input file ?
you are finding a file, or finding for something in a file ?
# 3  
Old 02-02-2006
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.
# 4  
Old 02-02-2006
That code will give you:
@array0
@array1
and so on, and each array will be the jobs from the BEGIN to the END lines.

$array2[3] would be
days_of_week: all
for example
# 5  
Old 02-03-2006
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"

# 6  
Old 02-03-2006
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

# 7  
Old 02-03-2006
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..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Timebased search script

Hi there first of all, sry for my bad english and possible mistakes .-) I write scripts for my server to get my live easier. So my next one will be a script, where I can find special files in a folder. I put with a script of mine files in a folder on the server with following name... (5 Replies)
Discussion started by: bigsanch
5 Replies

2. UNIX for Dummies Questions & Answers

Help with ip search script

Hi Obviously first time poster here. I have been set the task of creating a network diagram for each of our clients in the business i work for. After identifying ip adresses of firewalls name servers and the like im got to the point where i found it impracticle to go through 254 ip address and... (6 Replies)
Discussion started by: jeked
6 Replies

3. Shell Programming and Scripting

Please help in creating search script

Hi all, Hope everyone is doing fine. I need a help in creating search script. I have file which contains job names and job recs. I need to select specific job name and next line which is job rec job (BLREEXTT) { Record B${cycle}${month}${year}000075; follows BLCYRRTCHKT; ... (5 Replies)
Discussion started by: taksistalo
5 Replies

4. Shell Programming and Scripting

Search and Copy Script

Dear All, I would like to create a Unix script which basically searches for files which are more than 2 days old and copy only the new files to the destination. What i mean is if the destination may have most of the files, it may not have only last 2 to 3 days file. I am able to create the... (6 Replies)
Discussion started by: rrb2009
6 Replies

5. Shell Programming and Scripting

Log Search Script

Hi, I am trying to write a script to search a log- IPaddress being the search criteria, I would ideally like the script to ask the ipaddress Enter IP address - 244.258.27.225 And the ideal result would be for the script to get all the entries in the log file for a particular IP address,... (3 Replies)
Discussion started by: fuzion.hyd
3 Replies

6. Shell Programming and Scripting

Small Search script

I need to write a small shell script which does the following : I have a file : root/var/log/ocmp/ocmpclient.log This is a log file which is continuosly getting updated . I have to keep looking into this file all the time. I have to look for four keywords, "File Detected", File Sending",... (2 Replies)
Discussion started by: appu1987
2 Replies

7. Shell Programming and Scripting

script to search a file

hi all i have a requirement to write a script which will serach a file in a particuar location for 3 hours from the time when the script is run.if during this period the file arrives in a particular location then this will transmit the file to a other server.please help me writing the script which... (1 Reply)
Discussion started by: dr46014
1 Replies

8. UNIX for Advanced & Expert Users

Help with search script

HI all, I am trying to find files that have .cc3 file extenstion folder using this script below, but this fails as there are files in the folder and i get a message saying i have no files. Please anyone reveiw and let me know if i am missing anything.I am working on this from two days and i... (9 Replies)
Discussion started by: bsandeep_80
9 Replies

9. Shell Programming and Scripting

Search File Script

Nice forum for noobs like me to UNIX. With that said, I need assistance. I am trying to make a script to search files for a specific date and string within the file. Then I need the script to move the file that have the specific date and string to a directory. In the interim I have been manually... (7 Replies)
Discussion started by: I4ce
7 Replies

10. Shell Programming and Scripting

script to search all the directories

Hi there, Is there any command or script to search all the directories for duplicated files? Thanks, Abrahim (3 Replies)
Discussion started by: abk
3 Replies
Login or Register to Ask a Question