Ubuntu – Sorting two columns with sort and uniq

sorttext processinguniq

I have a file that needs sorting in a specific way but I can't figure out how to do it.

I've tried the following command but it doesn't give me what I want:

sort -t" " -k1,1 Original_File | uniq -d > Desired_Output

Original_File:

User1 US
User1 NG
User2 US
User3 US
User4 US
User4 EN
User5 US

Desired_Output:

User1 US
User1 NG
User4 US
User4 EN

Best Answer

You can extract the first column, pick up the duplicates, and grep them back from the file again:

cut -f1 -d' ' Original_File | sort | uniq -d | grep -wFf- Original_File
Related Question