Reputation: 1266
Not sure what is wrong in my perl script. unable to match the end of line using regular expression
[rmdev01@inches-rmdev01 SISPortal]$ forever logs
info: Logs for running Forever processes
data: script logfile
data: [0] server /home/rmdev01/.forever/WusC.log
data: [1] Server /home/rmdev01/.forever/2hai.log
script:
[rmdev01@inches-rmdev01 SISPortal]$ cat forever.pl
#! /usr/bin/perl
@logs=`forever logs`;
chomp(@logs);
foreach my $log (@logs)
{
print "checking log at end of string $log\n";
if ($log =~ /log$/)
{
print "$log\n";
}
}
Output of script:
[rmdev01@inches-rmdev01 SISPortal]$ ./forever.pl
checking log at end of string info: Logs for running Forever processes
checking log at end of string data: script logfile
checking log at end of string data: [0] server /home/rmdev01/.forever/WusC.log
checking log at end of string data: [1] Server /home/rmdev01/.forever/2hai.log
Upvotes: 0
Views: 88
Reputation: 385590
chomp
only removes a line feed[1], while you apparently have other trailing whitespace (such as a carriage return).
s/\s+\z//
removes all trailing whitespace, so it's using that is usually a better pratice than using chomp
.
#!/usr/bin/perl
use strict;
use warnings qw( all );
for (`forever logs`) {
s/\s+\n//;
print if /log$/; # Or /\blog$/ as Tim Pietzcker suggested.
}
$/
is. But $/
start off containing a line feed.Upvotes: 1
Reputation: 336108
It appears that there are some space/tab characters before the end of the string (chomp()
only removes newlines, but no other whitespace characters).
Allow those trailing whitespace characters (and perhaps avoid matches like Blog
by using a word boundary anchor):
if ($log =~ /\blog\s*$/)
Upvotes: 3