Solved

Repeat action for multiple files

Posted on 2004-07-31
10
172 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This tutorial will discuss the log-in process using WhizBase. In this article I assume you already know HTML. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you might look at some of my other articles abo…
This article will show, step by step, how to integrate R code into a R Sweave document
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

746 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