Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search for words in a file

Status
Not open for further replies.

oasys

Technical User
Jun 27, 2000
20
CA
I know very little about Perl, I picked up a book hoping to teach myself.
Here’s what I would like this Perl program to do.
At 9:00am and 9:00pm to change directory to c:\modems delete the file failure.txt. Now open/read any file with the extension .log there will be sub directories. In these log files search for the word “Failure”. If it finds the word Failure print the name of the file it found the word failure in to c:\modem\failure.txt
Here’s what I have been able to do so far.

$target = "Failure";
open(INPUT, &quot;<HAYES.LOG&quot;);
while (<INPUT>) {
if (/$target/) {
print &quot;HAYES&quot;;
}
}
close(INPUT);

This program prints the result to the screen and not a file.
Any help would be gratefully appreciated.
 
glad to hear another person is learning perl...
to print to a file, you open up a file for writing like this:[tt]
open(OUTPUT, &quot;>failure.txt&quot;);
[/tt]
and then you print to that file handle, using normal print statements:[tt]
print OUTPUT &quot;$filename\n&quot;;
[/tt]

another thing of interest to you is that you can open up and read directories. 'opendir' opens the directory, 'readdir' returns an array of it's contents, and 'closedir' closes the handle. you could take the array that readdir returns, perform a 'grep' search through it to find all files that end in '.log', and then open each of those one at a time, look through for your $target, and then print their name to the OUTPUT file if it's found.

HTH &quot;If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito.&quot;
 
Just to learn perl myself and was wondering if u could help? I help to write a script which constantly reads in certain files from several directories. Each file is to be tar and zipped. But any directory which has a file which is .main cannot be tarred and zipped. Plus it cannot tar and zip any file which is the latest, meaning the most up to date, so it should do a time check.
Any help would be greatly appreciated.
Patrick.
 
What you want to do is some what more complexed than what I was doing.I was pressed for time to complete the script which I was working on and took the short cut by runing the same script a few time. Here's what I came up with by piecing different scripts together.
The firt part gets time and date. The middle section is finding the log files and the end section prints what it finds with the date and time attached to it
I'm sure there is a better way,

@days = (&quot;Sunday&quot;,&quot;Monday&quot;,&quot;Tuesday&quot;,&quot;Wednesday&quot;,&quot;Thursday&quot;,
&quot;Friday&quot;,&quot;Saturday&quot;);
@shortdays = (&quot;Sun&quot;,&quot;Mon&quot;,&quot;Tue&quot;,&quot;Wed&quot;,&quot;Thu&quot;,&quot;Fri&quot;,&quot;Sat&quot;);
@months = (&quot;January&quot;,&quot;February&quot;,&quot;March&quot;,&quot;April&quot;,&quot;May&quot;,&quot;June&quot;,
&quot;July&quot;,&quot;August&quot;,&quot;September&quot;,&quot;October&quot;,&quot;November&quot;,
&quot;December&quot;);
@shortmonths = (&quot;Jan&quot;,&quot;Feb&quot;,&quot;Mar&quot;,&quot;Apr&quot;,&quot;May&quot;,&quot;Jun&quot;,&quot;Jul&quot;,&quot;Aug&quot;,
&quot;Sep&quot;,&quot;Oct&quot;,&quot;Nov&quot;,&quot;Dec&quot;);

($sec,$min,$hr,$mday,$mon,$year,$wday,$yday,$isdst) =
localtime(time);
$longyr = $year + 1900;
$fixmo = $mon + 1;
if ($isdst == 1) {
$tz = &quot;CDT&quot;;
} else {
$tz = &quot;CST&quot;;
}

$target = &quot;Fail&quot;;
unlink (&quot;failure.txt&quot;);
open(INPUT, &quot;<c:/aec/omniarc/ml_bm30a/ml_bm30a.log&quot;);

while (<INPUT>) {

if (/$target/) {
open(OUTPUT, &quot;>failure.txt&quot;);
printf(OUTPUT &quot;ML_BM30A failed %3s %3s %2d %02d:%02d:%02d %04d\n&quot;, $shortdays[$wday],
$shortmonths[$mon], $mday, $hr, $min, $sec, $longyr);

}

}

close(INPUT);

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top