Solved

search file and send output

Posted on 1998-07-06
4
147 Views
Last Modified: 2010-03-05
I am a very new Perl user.  I am trying to write some code to read a list of files with lines formatted like the following:  dumptrend /emsdat/trend/ANALOG:AGCGHOST.SYSLOAD-RT_10_3 /emsdat/trend/cps1/lod.ems
I would like the program to get today's date formatted like yymmdd and append this date to the 10_3 and to the ems such that the command looks like the following:

dumptrend /emsdat/trend/ANALOG:AGCGHOST.SYSLOAD-RT_10_3.yymmdd > /emsdat/trend/cps1/lod.emsyymmdd

I then want to run all the commands in the file containing the above lines.  The output from the above command will look like the following:

07/05/98xxxxx00:00:10xxxxx-6.00x01  
07/05/98xxxxx00:00:20xxxxx-6.00x01
07/05/98xxxxx00:00:30xxxx-10.00x01
07/05/98xxxxx00:00:40xxxx-10.00x01
07/05/98xxxxx00:00:50xxxx-20.00x01
07/05/98xxxxx00:01:00xxxxx-5.00x01
07/05/98xxxxx00:01:00xxxxx-5.00x01

where the x's denote spaces.  I would like to search through each of the .ems files each minute for the entries associated with that minute and send the output to a file in the same format.  I would greatly appreciate any assistance I could receive.  I'm under the gun to accomplish this and am trying to learn Perl.  I am running Unix 3.2D on a DEC Unix.
0
Comment
Question by:hbostic
  • 2
  • 2
4 Comments
 
LVL 5

Accepted Solution

by:
b2pi earned 100 total points
ID: 1208297
## Here's the first part.

#!/usr/bin/perl -w

## takes a list of filenames on the command line

use strict;

use vars ($Date);

$Date = &GetDate;

## execute the commands
foreach (@ARGV) {
   &ParseAndRun($_, $Date);
}





sub ParseAndRun {
   my($file, $date) = @_;
  open(FLIST, "<$file") || print STDERR "Unable to open $file ... skipping\n";
  while (<FLIST>) {
        ## Note, no line break below, it just looks like there is one
         if (m/dumptrend \/emsdat\/trend\/ANALOG:AGCGHOST.SYSLOAD-RT_10_3\/emsdat\/trend\/cps\/lod.ems/ ) {
           ## Again, no line break below
            my($syscmd) = "dumptrend /emsdat/trent/ANALOG:AGCGHOST.SYSLOAD-RT_10_3.$date > /emsdata/trent/cps1/lod.ems$date";
          system($syscmd) || print STDERR "Error executing $syscmd\n";
      }
  }
  close(FLIST);
}

sub GetDate {
    my($d, $m, $y);
   (undef, undef, undef, $m, $d, $y) = localtime(time);
   sprintf("%.2d%.2d%.2d",$y,++$m,$d);
}


## That's the end of the first one.

For the second one, you should be able to figure it out from the above. You can either have the second script run as a cron job, or do a sleep (60) inside a loop.  To find which files to open, take a look at

perldoc -f opendir

THe code above should tell you how to open and read from a file.  Finally, if the lines are as listed, you can use

while (<FHANDLE>) {
   my($d, $t, $s, $c);
   ($d, $t, $s, $c) = split(/\s+/);
 }

to read and parse.  Good luck.
0
 

Author Comment

by:hbostic
ID: 1208298
The program will have to run every minute and the output file is continually growing.  The program will need to search through the output of the dumptrend for the associated minutes entries.  
0
 
LVL 5

Expert Comment

by:b2pi
ID: 1208299
It sounds more like you want two programs, or I misunderstood the original question.

Nevertheless, you should have enough to go on with the above code.
0
 

Author Comment

by:hbostic
ID: 1208300
I only want one program.  The input file which is changed to a .ems file by the dumptrend command is updated every 10 seconds.  I will go through that output which is the .ems file , search for the associated minutes entries and then output that to another file.  That can be accomplished with a while loop and some sort of string search function I believe.  Perhaps, I wasn't clear about all of this.  In addition, it doesn't seem that the sub ParseAndRun is generic.  Will ParseAndRun go through each line of my input file and append the date? There are 15 lines in the input file. Anyway, thank you for your help.  
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now