0

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:

163.213.54.88     aaaaa
163.213.54.88     bbbbb
9.143.93.246      ccccc
219.98.24.226     aaaaa
163.213.54.88     aaaaa
215.134.242.10    ccccc
131.16.225.31     bbbbb
9.143.93.246      aaaaa
131.16.225.31     bbbbb

and in this case the result should be:

163.213.54.88
9.143.93.246

Thank you very much.

0

3 Answers 3

2

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 }'
163.213.54.88
9.143.93.246
2

Without needing to sort:

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

outputs

163.213.54.88
9.143.93.246
0

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.

1
  • If this was code golf, I'd be winning! :)
    – Jim L.
    Commented 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 .