Rossano Fenner
Rossano Fenner

Reputation: 135

Extract lines with duplicate last field

Based on a list generated with the command

find '/patch' -name A* -exec ls -la {} \;

Get only a list with file names that appears more than once (duplicated), but I want display the full line not only the file name.

Example

Based on this file:

-rw-r--r-- 1 root root 34K Jan  9  2014 /usr/share/dia/sheets/AADL.sheet
-rw-r--r-- 1 root root 952 Fev 14 07:07 /usr/share/postgresql/9.3/man/man7/ABORT.7.gz
-rw-r--r-- 1 root root 955 Jul 30  2014 /usr/share/postgresql/9.1/man/man7/ABORT.7.gz
-rw-r--r-- 1 root root 92K Abr 28  2014 /usr/share/gettext/ABOUT-NLS
-rw-r--r-- 1 root root 545 Dez 14  2013 /usr/share/dia/sheets/Automata.sheet
-rw-r--r-- 1 root root 6,7K Dez 21 2012 /usr/share/perl5/Mail/Address.pm
-rw-r--r-- 1 root root 709 Mar  3 09:03 /home/test/Address.pm
-rw-r--r-- 1 root root 709 Mar  3 11:13 /home/test/Automata.sheet
-rw-r--r-- 1 root root 520 Mar  3 11:15 /home/test/t2/Address.pm

I want get this result:

-rw-r--r-- 1 root root 952 Fev 14 07:07 /usr/share/postgresql/9.3/man/man7/ABORT.7.gz
-rw-r--r-- 1 root root 955 Jul 30  2014 /usr/share/postgresql/9.1/man/man7/ABORT.7.gz
-rw-r--r-- 1 root root 6,7K Dez 21 2012 /usr/share/perl5/Mail/Address.pm
-rw-r--r-- 1 root root 709 Mar  3 09:03 /home/test/Address.pm
-rw-r--r-- 1 root root 520 Mar  3 11:15 /home/test/t2/Address.pm
-rw-r--r-- 1 root root 545 Dez 14  2013 /usr/share/dia/sheets/Automata.sheet
-rw-r--r-- 1 root root 709 Mar  3 11:13 /home/test/Automata.sheet

Using this commands

awk -F. '{ n = split($0, a, "/"); print a[n] }' file |sort | uniq -d > filedups

I got

ABORT.7.gz
Address.pm
Automata.sheet

and after

grep -f filedups file

I get expected result.

My question: Is there a direct way to do this in just one line using awk and/or other commands?

Upvotes: 0

Views: 47

Answers (1)

karakfa
karakfa

Reputation: 67507

awk to the rescue!

starting with your initial file

$ awk '{n=split($NF,a,"/"); k=a[n]; c[k]++; 
        v[k]=k in v?v[k] ORS $0:$0} 
   END {for(k in c) if(c[k]>1) print v[k]}' file

-rw-r--r-- 1 root root 6,7K Dez 21 2012 /usr/share/perl5/Mail/Address.pm
-rw-r--r-- 1 root root 709 Mar  3 09:03 /home/test/Address.pm
-rw-r--r-- 1 root root 520 Mar  3 11:15 /home/test/t2/Address.pm
-rw-r--r-- 1 root root 545 Dez 14  2013 /usr/share/dia/sheets/Automata.sheet
-rw-r--r-- 1 root root 709 Mar  3 11:13 /home/test/Automata.sheet
-rw-r--r-- 1 root root 952 Fev 14 07:07 /usr/share/postgresql/9.3/man/man7/ABORT.7.gz
-rw-r--r-- 1 root root 955 Jul 30  2014 /usr/share/postgresql/9.1/man/man7/ABORT.7.gz

Upvotes: 1

Related Questions