Solved

Repeat action for multiple files

Posted on 2004-07-31
10
175 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
 
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

If you get a (Blue Screen of Death), your system writes a small file called a minidump. Your first step is to make certain your computer is setup to record memory dumps. Right click My Computer, choose properties. Click on the advanced tab, an…
This article will show, step by step, how to integrate R code into a R Sweave document
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

867 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

15 Experts available now in Live!

Get 1:1 Help Now