Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extracting lines from a file for given condition. 1

Status
Not open for further replies.

Bootstrap

Technical User
Nov 9, 2001
12
US
Hi,

I am trying to write a script that pull three lines from specific entries in a config file. Example: manager.cfg

10000 Service Orange 2.1
MIN=1 MAX=5
START=AUTO
PATH=/user/bin/Orange

10000 Service Orange 2.0
MIN=1 MAX=8
START=MANUAL
PATH=/user/bin/Orange

10000 Service Orange 1.1
MIN=1 MAX=3
START=MANUAL
PATH=/user/bin/Orange

10000 Service Orange 1.0
MIN=1 MAX=5
START=AUTO
PATH=/user/bin/Orange

6346 Service Green 2.0
MIN=1 MAX=3
START=MANUAL
PATH=/user/bin/Green

6346 Service Green 1.1
MIN=1 MAX=3
START=AUTO
PATH=/user/bin/Green

6346 Service Green 1.0
MIN=1 MAX=3
START=MANUAL
PATH=/user/bin/Green

100003 Service Red 1.0
MIN=1 MAX=3
START=MANUAL
PATH=/user/bin/Red

100003 Service Red 1.1
MIN=3 MAX=6
START=AUTO
PATH=/user/bin/Red

The goal is to print the first 3 lines of all the entries that have "START=AUTO". Example:
100003 Service Red 1.1
MIN=3 MAX=6
START=AUTO
Awk worked fine until I needed to do this in *nix and Windows. Perl seems like a good choice, but I'm trying to learn as I go. I can't seem to get the two lines before START=AUTO to print.

Thanks in advance.

Robert

 
This could likely be a good bit more concise, but, maybe it gets you going in a workable direction.

Code:
#!/usr/local/bin/perl
$str = '10000 Service Orange 2.1
      MIN=1 MAX=5
      START=AUTO
      PATH=/user/bin/Orange

10000 Service Orange 2.0
      MIN=1 MAX=8
      START=MANUAL
      PATH=/user/bin/Orange

10000 Service Orange 1.1
      MIN=1 MAX=3
      START=MANUAL
      PATH=/user/bin/Orange

10000 Service Orange 1.0
      MIN=1 MAX=5
      START=AUTO
      PATH=/user/bin/Orange

6346  Service Green 2.0
      MIN=1 MAX=3
      START=MANUAL
      PATH=/user/bin/Green

6346  Service Green 1.1
      MIN=1 MAX=3
      START=AUTO
      PATH=/user/bin/Green';

# match each group of lines from #### Service
# to PATH=/somestuff     
while ($str =~ /\d+\s+Service.*?PATH.*?\s/sg)
  {
  my $match = $&;
  # check to see if chunk contains START=AUTO
  if ($match =~ /START=AUTO/) { print "$match\n"; }
  }
[code]
 'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top