The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Perl Issue raj001 Shell Programming and Scripting 23 01-30-2009 06:12 AM
Perl Script Issue - Please Help * Thanks!!! jroberson Shell Programming and Scripting 8 11-03-2008 03:47 AM
perl issue .. zedex Shell Programming and Scripting 3 09-13-2008 11:22 PM
What is wrong with this script? heprox Shell Programming and Scripting 8 11-16-2006 05:43 AM
perl regexp error , I cant understand what is wrong umen Shell Programming and Scripting 4 10-19-2006 10:35 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-18-2009
SkySmart SkySmart is offline
Registered User
  
 

Join Date: Dec 2006
Posts: 48
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 (permalink)  
Old 03-18-2009
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,930
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 (permalink)  
Old 03-18-2009
pludi's Avatar
pludi pludi is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2008
Location: .at
Posts: 1,788
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 (permalink)  
Old 03-18-2009
KevinADC KevinADC is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2008
Posts: 731
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 (permalink)  
Old 03-18-2009
SkySmart SkySmart is offline
Registered User
  
 

Join Date: Dec 2006
Posts: 48
Quote:
Originally Posted by pludi View Post
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 (permalink)  
Old 03-18-2009
SkySmart SkySmart is offline
Registered User
  
 

Join Date: Dec 2006
Posts: 48
Quote:
Originally Posted by KevinADC View Post
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 (permalink)  
Old 03-18-2009
SkySmart SkySmart is offline
Registered User
  
 

Join Date: Dec 2006
Posts: 48
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.
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 02:15 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0