Solved

Repeat action for multiple files

Posted on 2004-07-31
10
180 Views
Last Modified: 2013-12-25
I am making a basic logfile analsys script and need to process each log file.

foreach would be the obvious choice but how could I implement it?


For example,

foreach @file
{
# Processing code
}


Thanks,
S-P
0
Comment
Question by:Sparky-Plug
10 Comments
 
LVL 51

Expert Comment

by:ahoffmann
ID: 11683716
foreach my $file (@files) { .. }
0
 

Author Comment

by:Sparky-Plug
ID: 11685286
So if I wanted to just search for a string in each file and print the line when found would it be something like this?

foreach my $file (@files)
{
foreach $line (@files) {
 chop $line;
 if($line =~ /string/)
 {
print "$line\n";
 }
}
}

Probably missed something obvious here, but how does it know what each file is called to open?


Thanks,
S-P
0
 
LVL 51

Accepted Solution

by:
ahoffmann earned 25 total points
ID: 11687035
no, you first need to open the file

foreach my $file (@files) {
   open(F,"<$file) or die "cannot open file";
foreach $line (<F>) {
0
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 25 total points
ID: 11689727
If you are on a Unix system, you can do

foreach my $file (@files) {
  system("grep string $file") and die "Can not run grep $!\n";
}
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 11692215
Tintin,
:-( probably very slow, and not very perlish ;-)
0
 
LVL 48

Expert Comment

by:Tintin
ID: 11699264
Certainly not very perlish, but would be quicker in most circumstances than opening the file and searching for the string.
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 25 total points
ID: 11731505
system "grep","string",@files; #would save multiple opening of grep


@ARGV=@files;
while( <> ){
    print if /string/;
}
#is an easy way to process multiple files in Perl
0
 
LVL 2

Expert Comment

by:fouaddba
ID: 12280067
Hi gurus,
I have to check in my backup log files that my file  has been backed up or not.

My file names are in one file: PM.lst, which contains all the names of the files that should be backed up.

The Directory where to find:  /DDUMPS/logs (the log files dir.)

If the backup will be successful, then at the last line, it is written “successfully completed”.

The scenario: we basically save our files at a designated location (for backup purpose) /DDUMPS, and from there the backup s/w picks up the files not yet backup up & took the backup, but some time the successful backup is done in 3 attempts, don’t know why but this happened.

Now the task: I have to write a script either shell or perl, which will go to that dir /DDUMPS/logs and look for my file names (from PM.lst) in all those log files, when found, then check the last line of that log file: is it written successfully completed or not.

Very best regards,
Thanks a lot in advance for you people kind help.

0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this tutorial I will focus on how to use WhizBase as a tool for sending ICQ messages to ICQ. Here I will use a new technology in WhizBase, published in WhizBase 5.1 version. In this tutorial I will use 3 files, pager.wbsp for the processing, e…
It is becoming increasingly popular to have a front-page slider on a web site. Nearly every TV website,  magazine or online news has one on their site, and even some e-commerce sites have one. Today you can use sliders with Joomla, WordPress or …
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
The viewer will learn how to dynamically set the form action using jQuery.

679 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