I try to write script to get the the values from the first column, which are the same, but have a different value in the second column. Does someone know how to do it ? Example: I have this .txt file:     aaaaa     bbbbb      ccccc     aaaaa     aaaaa    ccccc     bbbbb      aaaaa     bbbbb

and in this case the result should be:

Thank you very much.


3 Answers 3


Remove all lines that are duplicates of other lines, then extract the first field of the lines where the first field occurs twice or more times:

$ sort -u file | awk '++count[$1] == 2 { print $1 }'

Without needing to sort:

awk '!($1 in a) {a[$1] = $2; next} $2 != a[$1] {print $1}' file


Highly similar to @Kusalananda's answer (he beat me to it), but with a simpler awk invocation:

sort -u file | awk '{print $1}' | uniq -d

This method also removes all lines that are duplicates of other lines; then extracts the first field of those lines; and then outputs all the first fields that occur two or more times.

  • If this was code golf, I'd be winning! :)
    – Jim L.
    Apr 21, 2022 at 22:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .