TravisThomas
TravisThomas

Reputation: 631

perl regex to match "==" at end of line

I'm having trouble getting this perl statement to properly filter an output file.

perl -00 -wnl -e '
   /Instance list size <\d+>\n(.+)\*\*end/s or die "error msg\n";
foreach my $line (split("\n", $1)) {
  $line =~ /(==)$/ && print "\"$_\"" ;
}' $Output_File 2>&1

The output I'm interested is all lines terminated with ==. But for some reason, when I run this, even in a file with only 8 lines and 1 intended match, I'm getting 22 matches.

Upvotes: 1

Views: 365

Answers (2)

Lorkenpeist
Lorkenpeist

Reputation: 1495

You could always try grep: grep '==$' file.txt

Upvotes: 2

jwd
jwd

Reputation: 11114

I'm not sure why you're using the -n option and still have a foreach my $line ... in your script. The whole point of -n is that it will feed your script one line at a time automatically.

Here's something that works for me:

$ cat foo.txt
asdfasdfsadf asdf a== asfd a sdf
aasdf asdf asd f==

asdf asdf asdf sad fsdaf==
asdfasdfasdf sadfsadf=aa sdfasdf =

$ perl -ne "print if /==$/" < foo.txt
aasdf asdf asd f==
asdf asdf asdf sad fsdaf==

Upvotes: 2

Related Questions