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
You can extract the first column, pick up the duplicates, and grep them back from the file again:
The example input and output was updated; the examples in the first section use the original example input to show detail examples on variants:
Original input in
Original_File
:You can skip the
userN
part foruniq
with the option-f
to skip leading fields - separated by space:For the same order as in the example output, you can reverse the sort - this will change the "label" values of the unique lines:
Note the
User5
in the first result line. If that's not acceptable, just sort again:If the
UserN
part is not separated by space, but has a fixed length, you can skip it foruniq
with the option-s
:With the updated example input, this is the command for creating the required sort order:
it sorts the second field to reverse order.