LinuxQuestions.org
Support LQ: Use code LQCO20 and save 20% on CrossOver Office
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 12-21-2006, 05:12 AM   #1
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,427

Rep: Reputation: 51
Perl grabbing value


Hi,

I'm trying to open up a file to parse it. The contents of the file will have:

Code:
crap
crap
crap
Your order # 9999 on Dec 17th 2009 blah blah
crap
crap
I'm trying to only grab the number 9999 or whatever number is in place of that. My perl script so far is below:

Code:
my $file="/home/file.txt";
open (FH, $file) or die "Can't open $!";
   while ($line = <FH>) {
      chomp;
      my $string =~ /Your order # (\d+$)/;
      print "$string";
}
close (FH);
As you can see, I'm just trying to match the darn string (and it's printing nothing) but I actually want to grab the value '9999' and save it to a variable. Can anyone guide me? Thank you.

-twantrd
 
Old 12-21-2006, 05:32 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 39,853

Rep: Reputation: 1121Reputation: 1121Reputation: 1121Reputation: 1121Reputation: 1121Reputation: 1121Reputation: 1121Reputation: 1121Reputation: 1121
simple example:
Code:
my $string = "Your order # 9999 on Dec 17th 2009 blah blah";
if ( $string =~ /Your order # (\d+)/ )
  {
    print "Order number = $1";
}
just convert that basic syntax to use the external file and you'll be fine i'm sure.
 
Old 12-21-2006, 06:15 AM   #3
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 39,853

Rep: Reputation: 1121Reputation: 1121Reputation: 1121Reputation: 1121Reputation: 1121Reputation: 1121Reputation: 1121Reputation: 1121Reputation: 1121
in fact, when you're in a loop already like you will be, it gets even easier:
Code:
   while (<FH>) {
      chomp;
      if (/Your order # (\d+)/) {
        print "Order number = $1\n";
      }
as the variables all become implied. the default parameter used when none is provided in that context is $_, which is the value being iterated over, i.e. each line in the file in turn.

also, there's no need to chomp that either, as you've no need to cling to either end, and as it's only one line on the if, you can go further still:

Code:
while (<FH>) { print "Order number = $1\n" if (/Your order # (\d+)/) }
i think...

Last edited by acid_kewpie; 12-21-2006 at 06:20 AM.
 
Old 12-21-2006, 07:30 PM   #4
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,427

Original Poster
Rep: Reputation: 51
Thanks, Acid! That helped me. Thank you very much.

-twantrd
 
Old 12-22-2006, 06:20 PM   #5
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,427

Original Poster
Rep: Reputation: 51
Ok, I'm on the last leg of writing this perl script. I'm trying to send an e-mail containing the lines that match my string. The code that does this is this:

Code:
use FileHandle;
my $match;

 # Grab the filenames that have been downloaded
        open (FILENAME, "<$logfile");
                while (my $match=<FILENAME>) {
                        if ($match =~ /Starts:/) {
                                print "$match"; # printing the files works
                                
                        }
                }
        close (FILENAME);
                        my $mail=new FileHandle;
                        $mail->open("| /usr/sbin/sendmail -t") or die "Cannot open $!";
                        $mail->print("From: root\@host.mydomain.com\n");
                        $mail->print("To: $recipients\n");
                        $mail->print("Subject: Downloaded files\n");
                        $mail->print("*** Downloaded Files ***\n\nFiles: $match");
                        $mail->close();
                }
An example of a logfile looks like this:
Code:
Starts: 312312312.zip
crap
crap
crap
Starts: 312355555.data
So, I'm trying to grab only the lines that start with "Starts: *" and emailing that using sendmail -t. Sending the mail works fine but it doesn't print the files. However, the "print $match;" statement shows my files. Thanks for your guidance.

-twantrd

Last edited by twantrd; 12-22-2006 at 06:22 PM.
 
Old 12-24-2006, 09:04 AM   #6
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 39,853

Rep: Reputation: 1121Reputation: 1121Reputation: 1121Reputation: 1121Reputation: 1121Reputation: 1121Reputation: 1121Reputation: 1121Reputation: 1121
well essewntially $match would appear to not exist within the scope you're using it. the my statement in the while loop causes the variable to be initialized witihn that loop, and destroyed after leaving that level of processing. so the data isn't ther outside of the while loop. if you remove the "my" itself, then i assume the last value of $match will be there still, but only the last one. you presumably really want to append each successful match result to an array and print that out in one go later on.
 
Old 12-25-2006, 01:03 AM   #7
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,427

Original Poster
Rep: Reputation: 51
Ahh thanks for the catch. Ok, so I changed my script to save the matching strings into an array and it still doesn't work. Below is my code:

Code:
my @lines;

open (FILENAME, "<$logfile");
  while (my $match=<FILENAME>) {
        if ($match =~ /Starts:/) {
           @lines=$match; # put matching strings into array
           print "@lines"; # this shows me my matching strings
        }
  }
close (FILENAME);
open (SENDMAIL, "| /usr/sbin/sendmail -oi -t") or die "cannot open sendmail $!";
print SENDMAIL "To: $recipients\n";
print SENDMAIL "From: root\@hostname.domain.com\n";
print SENDMAIL "Subject: Downloaded files\n";
print SENDMAIL @lines;
close (SENDMAIL);
I get an email but the body is completely blank. Thank you for your help.

-twantrd
 
Old 12-25-2006, 10:41 AM   #8
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 706

Rep: Reputation: 68
Hi.

The line:
Code:
@lines=$match;
will not accumulate, it will over-write. I'd probably use push here.

Does sendmail automatically place a blank line between the headers and the body, or does the creator need to attend to that detail?

I think I would write to STDOUT until I got it debugged, then use sendmail. The last time I did something like this, I had to use that extra step to look at the mail. That got old in a hurry ... cheers, makyo
 
Old 12-26-2006, 09:11 AM   #9
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 39,853

Rep: Reputation: 1121Reputation: 1121Reputation: 1121Reputation: 1121Reputation: 1121Reputation: 1121Reputation: 1121Reputation: 1121Reputation: 1121
I'd also point out that you've not used a large number of shortcuts i orignally posted. not tha you need to of course, but for one there's no need to make the $match variable even exist in the first place.
 
Old 12-26-2006, 09:30 PM   #10
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,427

Original Poster
Rep: Reputation: 51
Hi Makyo,

I have been using print statements to debug my code and it shows up fine but when I call sendmail, it prints nothing. I'll start looking into using push.

Acid,
I'm not using your shortcuts because:
1. I want to finish writing up this code
2. It's easier for me to understand it this way

Once this code works the way I want it, I'll start putting in your shortcuts. But for right now, it's easier for me to understand it the way it is. Don't ask me why . Again, I'm learning perl.

Thanks for your help guys.

-twantrd
 
  


Reply

Tags
expressions, regular, sendmail


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Packet Grabbing in NS ashutoshsharma Linux - Networking 0 06-25-2004 01:53 AM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 08:09 PM
tv grabbing with rivatv saavik Linux - Software 0 10-10-2003 05:06 AM
Video grabbing xgreen Linux - Newbie 4 08-24-2003 04:54 PM
Picture grabbing zeky Linux - General 1 12-06-2002 09:54 AM


All times are GMT -5. The time now is 06:37 PM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration