The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-02-2006
BCarlson BCarlson is offline
Registered User
  
 

Join Date: May 2005
Posts: 48
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
  #2 (permalink)  
Old 02-02-2006
linuxpenguin's Avatar
linuxpenguin linuxpenguin is offline Forum Advisor  
Registered User
  
 

Join Date: May 2002
Location: India
Posts: 295
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 (permalink)  
Old 02-02-2006
PWSwebmaster PWSwebmaster is offline
Registered User
  
 

Join Date: Feb 2006
Location: Canada
Posts: 33
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 (permalink)  
Old 02-02-2006
PWSwebmaster PWSwebmaster is offline
Registered User
  
 

Join Date: Feb 2006
Location: Canada
Posts: 33
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 (permalink)  
Old 02-03-2006
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,409

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 (permalink)  
Old 02-03-2006
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,433

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 (permalink)  
Old 02-03-2006
BCarlson BCarlson is offline
Registered User
  
 

Join Date: May 2005
Posts: 48
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..
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:59 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0