$rec=$_;
chomp ($rec);
@arr=split(/ /,$rec);
print $rec,"\n" if ($arr[12]!~/OK/);
open (MYF, '>data.txt');
print $rec,"\n" if ($arr[12]!~/OK/);
close (MYF);
}
close (MYFILE);
ok, here's what i'm trying to u. I'm opening the file logs_report and i'm grepping only for lines that DOES NOT have OK in it.
Now, the result i get from that grep is what I"m trying to write to a file.
How can I do that?
so pretty much, i'm trying to open up a file, read the contents of the file, pull out certain lines from that file, and the certain lines i print out, i want to write it to a file. how can i do that? also, if i want to mail the file that is written out to, is there a way to do that in perl?
#!/usr/local/bin/perl
open (MYFILE, 'logs_report');
open (MYF, '>data.txt');
while ($rec = <MYFILE>) {
chomp($rec);
@arr = split(/ /,$rec);
if ($arr[12] !~ /OK/) {
print $rec, "\n";
print MYF $red, "\n";
}
}
close (MYF);
close (MYFILE);
This is probably what you want. The way you wrote it, you'd overwrite your data.txt every time you find something, plus opening and closing repeatedly in a loop can be quite I/O cost intensive.
And the next time you post listings or source, enclose it in [ code][/code ] tags (sans the spaces), to preserve formatting for readability.
Similar to my reply in your other thread that I think you didn't read. Assumes same example data from that thread.
This should be more efficient and maybe more accurate:
#!/usr/local/bin/perl
use strict;
open (MYFILE, 'logs_report') or die "$!";
open (MYF, '>data.txt') or die "$!";
while (my $rec = <MYFILE>) {
next if ($rec =~ /STATE: OK/) {
print MYF $rec;
}
close (MYF);
close (MYFILE);
Pludis code has a small typo, it has $red where it should be $rec.
thank you. sorry for not including the codes. would you happen to know how to include a code in ur code that will email the file to a set of email addresses? i mean the file that is written to? your help or input is greatly appreciated. thanks
yes i did read your post in the other thread. when i went to use it i wasn't getting any response back. i think the problem was in the line that i bolded.
i'll try this script of yours and see if it outputs something. thanks a million.
First thing is you have to check if MIME::Lite is installed. It does not come installed by default. If it is or you can install it just laod the module:
use MIME::Lite;
and call the subroutine as needed:
test_mail();
the path to the attachment will probably need to be changed since it assume the attached document is in the same folder as the perl script. Easiest to use the full path if you know if:
Can someone please tell me why the value stored in the $date_string variable below isn't showing?
in the email that is sent out i get the word "$date_string" instead of the contents of the variable.
test_mail ();
sub test_mail {
my $msg = MIME::Lite->new(
From => 'noreply@faroko.com',
To => 'jamie.henson@faroko.com',
Subject => 'Status - \'$date_string\'',
Type => 'multipart/mixed'