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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FILEHANDLE 1

Status
Not open for further replies.

Nova980

Technical User
May 20, 2009
40
US
Thank you in advance,

Little help please? I'd like this script to know whether there is text printed to FILE_LOG and if there is no text than do not open FILE_LOG just die or unlink (delete).

open FILE_LOG, ">", "error.log" or die $!;

$LOGFILE = "keeplogging.log";
open(LOGFILE) or die("Could not open log file.");

while (<LOGFILE>)

{
print FILE_LOG if /\bhard error\b/i;
}

close(LOGFILE);
close(FILE_LOG);
 
Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]
[black][b]use[/b][/black] [green]warnings[/green][red];[/red]
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$LOGFILE[/blue] = [red]"[/red][purple]keeplogging.log[/purple][red]"[/red][red];[/red]
[url=http://perldoc.perl.org/functions/open.html][black][b]open[/b][/black][/url][red]([/red]LOGFILE, [blue]$LOGFILE[/blue][red])[/red] or [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple]Could not open log file: [blue]$![/blue][/purple][red]"[/red][red];[/red]
[black][b]my[/b][/black] [blue]@lines[/blue] = [url=http://perldoc.perl.org/functions/grep.html][black][b]grep[/b][/black][/url] [red]{[/red][red]/[/red][purple][purple][b]\b[/b][/purple]hard error[purple][b]\b[/b][/purple][/purple][red]/[/red][red]i[/red][red]}[/red] <LOGFILE>[red];[/red]
[url=http://perldoc.perl.org/functions/close.html][black][b]close[/b][/black][/url][red]([/red]LOGFILE[red])[/red][red];[/red]
[olive][b]if[/b][/olive] [red]([/red][blue]@lines[/blue][red])[/red] [red]{[/red]
   [black][b]open[/b][/black] FILE_LOG, [red]"[/red][purple]>[/purple][red]"[/red], [red]"[/red][purple]error.log[/purple][red]"[/red] or [black][b]die[/b][/black] [blue]$![/blue][red];[/red]
   [url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] FILE_LOG [blue]@lines[/blue][red];[/red]
   [black][b]close[/b][/black][red]([/red]FILE_LOG[red])[/red][red];[/red]
[red]}[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.10.0) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[li]warnings - Perl pragma to control optional warnings[/li]
[/ul]
[/tt]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Hello

utilizing this filehandle topic, i´d like to know the script that allows me to search in a file.txt a specific word or line, and report log errors by missing specific informations as words itself or datas.




 
kcire,

Start a new thread. It is not proper to ask a new question in another persons thread.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Hi Kevin,

I'm having difficulty displaying the array in FILE_LOG. When my message is sent via email, @data does not contain my FILE_LOG.

Thanks

#!/usr/bin/perl -w

use Net::SMTP;


print "Content-type: text/plain", "\n\n";

# This debug flag will print debugging code to your browser,
# depending on its value
# Set this to 1 to send debug code to your browser.
# Set it to 0 to turn it off.

my $DEBUG = 1;

if($DEBUG)
{
$| = 1;
open(STDERR, ">&STDOUT");
}


my $LOGFILE = "schedulerrun.log";

open(LOGFILE, $LOGFILE) or die "Could not open log file: $!";

my @lines = grep {/\bhard error\b/i} <LOGFILE>;

close(LOGFILE);

if (@lines) {
open FILE_LOG, ">", "hard_error.log" or die $!;
print FILE_LOG @lines;
close(FILE_LOG);
}

@data = <FILE_LOG>;

# close file
close(FILE_LOG);


# Create a new SMTP object
$smtp = Net::SMTP->new('1.1.1.25', Debug => 1);

# If you can't connect, don't proceed with the rest of the script
#die "Couldn't connect to server" unless $smtp;

# Initiate the mail transaction
# Your "real" email address
my $MailFrom = "myaddress";

# Recipient's email address
my $MailTo= "myaddress";

$smtp->mail( $MailFrom );
$smtp->to( $MailTo );
$smtp->to( $MailToBC);
$smtp->to( $MailToCC);

# Start the mail
$smtp->data();

# Send the header
# This address will appear in the message
$smtp->datasend("To: my/address",);

# So will this one
$smtp->datasend("From: my/address");
$smtp->datasend("Subject: Error Detected\n");
$smtp->datasend("\n");

# Send the body.
foreach $line (@data)
{
$smtp->datasend($line);
}

# Send the termination string
$smtp->dataend();

# Close the connection
$smtp->quit();
 
Are you adding to the log file (FILE_LOG) or overwriting it each time the script runs?

This is clearly wrong, I would have thought even a novice coder would see the problem here:

Code:
if (@lines) {
   open FILE_LOG, ">", "hard_error.log" or die $!;
   print FILE_LOG @lines;
   close(FILE_LOG);#[red]<- here the file is closed[/red]
}

@data = <FILE_LOG>;#[red]<- I think this should generate a warning since you have the -w switch turned on. @data will be empty. [/red]

# close file 
close(FILE_LOG);#[red]<- the file is already closed[/red]

FILE_LOG is closed when you try and assign the value of reading the file to the array. @lines already has the data you need, so use it to send the data.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Please start using the code tags to post code.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Sorry I disappointed you. I must admit that was a less than worthy thread. Got rid of the closed file and used @lines. Thank you Kevin, you mean so much to me in my noviciate.
 
Sorry if my comment sounded rude or demeaning, I assure it that was not my intention. I post quickly and on many forums and sometimes I may not pick my words as carefully as I should because of that. My apology if one is due. If you were not offended and took my comment in the spirit it was meant, then I apologize for apologizing. I better stop now before I taste my foot. [smile]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
mmmmm, cheese! [pacman]

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top