Skip to content
Advertisement

Finding repeated names in a file

Hi i have a txt file with last name and name of people, now i want do use egrep to only display the names of the people with the same last name. I have no idea how i could do this. Thanks for help my txt looks like this:

snow john
snow jack
miller george
mcconner jenny

and the output should be:

john 
jack

I’ve currently tried running:

cat names.txt | cut -d " " -f 1 | awk 'seen[$]++' 

…but this fails with an error:

awk: syntax error at source line 1
 context is
     >>> seen[$] <<<
awk: bailing out at source line 1

Advertisement

Answer

You can use a typical 2-pass approach with awk:

awk 'NR == FNR {freq[$1]++; next} freq[$1]>1{print $2}' file file

john
jack

Reference: Effective AWK Programming

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement