Forum Moderators: coopster & phranque

Message Too Old, No Replies

Finding a certain word in a text file

         

ace2010

10:14 pm on Oct 14, 2005 (gmt 0)

10+ Year Member



I have a log file generated by a test. I want to find the last word in the file, which should be either "passed" or "failed". How can I reach the end of this file assign passed or failed to a variable for comparison to print a pass or fail message to the screen?

Thanks!

Ron

Matt Probert

5:44 pm on Oct 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The "Perl Cookbook" published by O'Reilly has lots of solutions to this and similar real-world problems. I thoroughly recommend you search it out at your local library.

Matt

ace2010

6:22 pm on Oct 15, 2005 (gmt 0)

10+ Year Member



Thanks Matt,

I just picked up this book yesterday. I was also able to solve the issue with a grep statement.

Ron

bennymack

11:38 pm on Oct 15, 2005 (gmt 0)

10+ Year Member



If it's the last line in the file:

chomp ( my $last = `tail -1 $somefile` );

ace2010

1:41 pm on Oct 16, 2005 (gmt 0)

10+ Year Member



Thanks Benny,

That's actually a somewhat simpler than what I had.

Ron

Mers8

4:16 pm on Oct 18, 2005 (gmt 0)



chomp ( my $last = `tail -1 $somefile` );

spawns an additional process, and it's shell rather then perl ...

## print the last word only if the string end with a letter
$a = 'bla lbval asdas lasdasd lastword';
$a =~ /(\w*)$/;
print "$1\n";

## print the last sequence of non white space characters
$a = 'bla lbval asdas lasdasd lastword.';
$a =~ /(\S*)$/;
print "$1\n";