Take the 2-minute tour ×

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.

share|improve this question
sample input, output? –  sreservoir May 30 '13 at 23:30
Why are you splitting on newline when -n loops over the input? –  squiguy May 30 '13 at 23:33
be advised that if you are trying to validate a base64 encoded string, even if it will usually end in ==, it can end in just one = or even none. –  CSᵠ May 30 '13 at 23:58
thanks @kaᵠ. In this situation, the binary data length is constant, and it will always end in ==. –  TravisThomas May 31 '13 at 0:05
@squiguy, Look up -00 –  ikegami May 31 '13 at 0:48

2 Answers 2

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==
share|improve this answer
This is working for me in isolation but not in the context of the test I'm running. I've simplified the script (which, for the record, I did not write but inherited) to remove the foreach, but when I use print if /==$/', I get 0 matches. If I remove the $ print if /==/', I am back at 22 matches. –  TravisThomas May 30 '13 at 23:52
@trav1th, are you sure the ==s in your file are at the end of the lines? It could be foo == (a space after ==), which would keep your regex from matching. –  David X May 30 '13 at 23:56
I double checked. There is no space. The newline gets chomped by -l, so it should match fine. –  TravisThomas May 31 '13 at 0:06
@trav1th: The fact that you are getting 22 matches for a file with 8 lines is rather strange. Could you post a hex dump of this 8-line file, perhaps via hexdump -C ? –  jwd May 31 '13 at 0:38

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

share|improve this answer

Your Answer


