Perl Script issue. What am I doing wrong?

#!/usr/local/bin/perl

open (MYFILE, 'logs_report');

while (<MYFILE>) {

$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?

write it to the file and not to the stream

print MYF $rec,"\n" if ($arr[12]!~/OK/);
#!/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.

below is the code I came up with to get the file data.txt as an attachment and send it out:

sub test_mail {
my $msg = MIME::Lite->new(
   From    =>  'noreply@faroko.com',
   To      =>  'jamie.henson@faroko.com',
   Subject =>  'TESTING',
   Type    =>  'multipart/mixed',
);

$msg->attach(
    Type   =>  'TEST',
    Data   =>  "TESTING ATTACHMENT FEATURE",
);

$msg->attach(
    Type   =>   'TEST',
    Path   =>   'data.txt',
    Filename => 'data.txt',
);

$msg->send;
}

How can I incorporate this script into the existing script to have it send out the data.txt file to the email addresses.

Whoops. But a perfect demonstration for how useful perl -c and use strict; are :slight_smile:

Not a problem, everyone started as a new guy here at some point.

To send the results as an eMail, you've got a few options, but none can be done using simple Perl commands.

  1. The (probably) easiest way would be to use a module like Mail::Send
  2. If you want to have a lot of control about how the mail is sent, use Net::SMTP
  3. If you don't want to/can't install modules, open a file handle to a pipe that feeds into the sendmail program. Example here

hehehe..... very true :slight_smile:

It does assume that the pattern to find is exactly STATE: OK, if the pattern is different you need to change accordingly.

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:

Path => '/full/path/to/data.txt'

i included a code on the first page of this thread. do you see anything wrong with it? i'm running the script but its not sending out emails.

see my post above

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'

thanks

because you have the variable inside of single-quotes, which kills all variable interpolation. Change to double-quotes.