Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 198
  • 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

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