Explain this perl code which displays common lines in 2 files

后端 未结 2 442
北恋
北恋 2020-12-10 01:12

How does this perl one-liner display lines that 2 files have in common?

perl -ne \'print if ($seen{$_} .= @ARGV) =~ /10$/\'  file1 file2
2条回答
  •  心在旅途
    2020-12-10 01:48

    The -n command line option transforms the code to something equivalent to

    while ($ARGV = shift @ARGV) {
      open ARGV, $ARGV;
      LINE: while (defined($_ = )) {
        $seen{$_} .= @ARGV;
        print $_ if $seen{$_} =~ /10$/;
      }
    }
    

    While the first file is being read, scalar @ARGV is 1. For each line, 1 will be appended to the %seen entry.

    While the second file is being read, scalar @ARGV is 0. So if a line was in file 1 and in file2, the entry will look like 1110000 (it was 3× in file1, 4× in file2).

    We only want to output common lines exactly one time. We do this when a common line was first seen in file2, so $seen{$_} is 1110. This is expressed as the regex /10$/: The string 10 must appear at the end.

提交回复
热议问题