• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 203
  • Last Modified:

Repeat action for multiple files

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
Sparky-Plug
Asked:
Sparky-Plug
3 Solutions
 
ahoffmannCommented:
foreach my $file (@files) { .. }
0
 
Sparky-PlugAuthor Commented:
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
 
ahoffmannCommented:
no, you first need to open the file

foreach my $file (@files) {
   open(F,"<$file) or die "cannot open file";
foreach $line (<F>) {
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
TintinCommented:
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
 
ahoffmannCommented:
Tintin,
:-( probably very slow, and not very perlish ;-)
0
 
TintinCommented:
Certainly not very perlish, but would be quicker in most circumstances than opening the file and searching for the string.
0
 
ozoCommented:
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
 
fouaddbaCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now