print only the unique values from first column by comparing two columns in Linux

I have a text file with example (two columns separated with a :)

if the first columns first row is repeated again then dont consider it and give the output

11:22

33:44

55:66

55:77

my desired output should be

11:22

33:44

2 Answers

sort file | uniq --unique --check-chars=2

I'm making two assumptions:

  • it's okay to change the row order (sort)
  • the first column will always be two characters wide

For excluding the lines that have repetition of first column only:

sort file | uniq -u -w 1

For excluding the lines that have repetition of first field when delimited by colon:

cut -d: -f1 file | sort | uniq -u

In the two cases, we need to sort the file before piping the output to uniq (as uniq works on adjacent matching lines). Then do uniq with some options.

From uniq man page:

-u: only print unique lines
-w N: compare no more than N characters in lines
2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like