title: Perl: Find a match, remove the same lines, and to get the last field
body: <p>Being a Perl newbie, please pardon me for asking this basic question.</p>
  
  <p>I have a text file @server1 that shows a bunch of sentences (white space is the field separator) on many lines in the file.</p>
  
  <p>I needed to match lines with my keyword, remove the same lines, and extract only the last field, so I have tried with:</p>
  
  <pre><code>my @allmatchedlines;
  
  open(output1, "ssh user1@server1 cat /tmp/myfile.txt |");
  
      while(&lt;output1&gt;) {
              chomp;
              @allmatchedlines = $_ if /mysearch/;
      }
      close(output1);
  
      my @uniqmatchedline = split(/ /, @allmatchedlines);
  
      my $lastfield = $uniqmatchedline[-1]\n";
      print "$lastfield\n";
  </code></pre>
  
  <p>and it gives me the output showing:
  1</p>
  
  <p>I don't know why it's giving me just "1".</p>
  
  <p>Could someone please explain why I'm getting "1" and how I can get the last field of the matched line correctly?</p>
  
  <p>Thank you!</p>