ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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.
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.
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();
}
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.
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.
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.
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
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.