Perl Script issue. What am I doing wrong?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl Script issue. What am I doing wrong?
# 1  
Old 03-18-2009
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?
# 2  
Old 03-18-2009
Quote:
print $rec,"\n" if ($arr[12]!~/OK/);
write it to the file and not to the stream
Code:
print MYF $rec,"\n" if ($arr[12]!~/OK/);

# 3  
Old 03-18-2009
Code:
#!/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.
# 4  
Old 03-18-2009
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:

Code:
#!/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.
# 5  
Old 03-18-2009
Quote:
Originally Posted by pludi
Code:
#!/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.

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
# 6  
Old 03-18-2009
Quote:
Originally Posted by KevinADC
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:

Code:
#!/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.
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.
# 7  
Old 03-18-2009
below is the code I came up with to get the file data.txt as an attachment and send it out:

Code:
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Executing perl script in Linux gives :Exec format error. Wrong Architecture

i have perl script that used to be working great , once i edit it in windows and convert it to UTF-8 and then via FTP return it . also did: chmod +x foo.pl and then when i try to run it : ./foo.pl im getting this error: ./foo.pl: Exec format error. Wrong Architecture.... (4 Replies)
Discussion started by: umen
4 Replies

2. Shell Programming and Scripting

What is wrong with my perl script?

i managed to write a perl script for nagios. now, the script works. but i think there's somethign wrong with the exit codes. because the script doesn't show the output of the results in nagios, it instead shows null. please tell me what i'm doing wrong here: #!/usr/local/bin/perl use... (2 Replies)
Discussion started by: SkySmart
2 Replies

3. Shell Programming and Scripting

Issue regarding dos2unix perl script

Hi All, I have pearl script which will check and convert the file: INFO("dos2unix_cmds".$#{$dos2unix_cmds}); if ( $#{$dos2unix_cmds} == 0 ) { my $convert_cmd = $$dos2unix_cmds; my $rename_cmd = $$dos2unix_cmds; --conversion going here else INFO ("No need to... (8 Replies)
Discussion started by: saps19
8 Replies

4. Shell Programming and Scripting

Perl script issue

Hi All, I have a perl script which I am using in Windows environment. There is one more file called "functions.txt" which is having all the functions defined to used in my perl script. And the path for this function file is defined in my perl script. Howeever sometimes I am getting below error... (4 Replies)
Discussion started by: gr8_usk
4 Replies

5. Shell Programming and Scripting

Simple issue, what is wrong?

I had this working a few days ago but I since changed it. Heres the code x=1 while 1 2 3 4 5 6 1=$(ps -ef | grep process | awk '{ print $2}') if then echo "The database is accepting connections..." echo "Now I will check the next process" 2=$(ps -ef | grep process1 |... (10 Replies)
Discussion started by: jeffs42885
10 Replies

6. Shell Programming and Scripting

Perl...getting wrong output?

Good morning! Im trying to write and learn at the same time a simple script. fTHe script should tell me if a number is odd or even/ #!/usr/bin/perl $num = 10; $string1 = "This number is odd"; $string2 = "This number is even"; if ($num /= 2) { print "$string1\n"; }else{ ... (3 Replies)
Discussion started by: bigben1220
3 Replies

7. Web Development

Accessing a Perl CGI script, security issue

Hi Everybody, I was wondering if it was possible for someone to gain access to my Perl CGI scripts before they are interpreted by Perl (mod_perl on apache2) i.e. getting a hold of my raw scripts and not the html output? Let's say I use the DBI module where I have the hostname, user and... (2 Replies)
Discussion started by: z1dane
2 Replies

8. Shell Programming and Scripting

wrong output in perl script

Hi, Here is my piece of code-- #!/usr/bin/perl my $Time_Stamp ; my $User_Name; my $Success; my $Failure; my $ErrorCode; my $ErrorMsg; my $logDir = $ARGV; my $logPrefix = $ARGV; die "usage: $0 <logDir> <logPrefix>" unless $logDir and $logPrefix; die "Log dir $logDir doesn't... (2 Replies)
Discussion started by: namishtiwari
2 Replies

9. Shell Programming and Scripting

Perl script issue: print

Can someone tell me what I'm doing wrong with my perl script? I am new to Perl. This isn't even the final script, I'm just testing to see if it will print the directories with the files in it. For some reason my output is only printing the 1st, 6th, and last entries of the @sub_dir array. Each... (3 Replies)
Discussion started by: man
3 Replies

10. Shell Programming and Scripting

Perl Script Issue - Please Help * Thanks!!!

Please help me with my script please. I am trying to do the following: 1. Read files for the current directory 2. Open and read from nbe files files only 3. Read only the lines with the results pattern 4. Split the line and print 3rd field Please indicate what line I need to modify. ... (8 Replies)
Discussion started by: jroberson
8 Replies
Login or Register to Ask a Question