jonah_w
jonah_w

Reputation: 1032

Perl search for unique lines and corresponding file names

I was using perl -ne '$h{$_}++; END {print grep { $h{$_} == 1 } %h}' <a.txt <b.txt to get the unique lines of both files. While it's printing all the unique lines of these two files, I want it to print the corresponding name of the file which contains these unique lines.

Is there a way in perl command to accomplish this?

p.s.Please forgive my poor English. I hope I got myself clear.:)

Upvotes: 0

Views: 55

Answers (1)

H&#229;kon H&#230;gland
H&#229;kon H&#230;gland

Reputation: 40758

You can save the name of the file from $ARGV. Here is an example where I put the name of the file in another hash %n:

perl -nE 'chomp; $h{$_}++; $n{$_}=$ARGV; 
  END {say for map {"$n{$_}: $_"} grep { $h{$_} == 1 } keys %h}' a.txt b.txt

Upvotes: 2

Related Questions