开发者

How to search duplicate users in two files and then print those lines?

I have two files: FILE1 and FILE2: FILE1:

user1        1.1.1.1
user2        2.2.2.2
user3        3.14.14.3
user4        4.4.4.4
user5        198.222.222.222

FILE2

user1        99.22.54.214
user66       45.22.88.88
user99       44.55.66.66
user4        8.8.8.8
user39       54.54.54.54
user2        2.2.2.2

OUTPUT FILE

user1        1.1.1.1
user1        99.22.54.214
user2        2.2.2.2
user4        4.4.4.4
user4        8.8.8.8

I tried with a for loop b开发者_JAVA百科ut with particular succes.. Can anyone write me a code for this? Thx!


I might be missing something, but I'd think a "sort unique" should give the right answer.

$ sort -u file1 file2
user1        1.1.1.1
user1        99.22.54.214
user2        2.2.2.2
user3        3.14.14.3
user39       54.54.54.54
user4        4.4.4.4
user4        8.8.8.8
user5        198.222.222.222
user66       45.22.88.88
user99       44.55.66.66

Unless, of course, the output file you provided is the entire output you expect. Since you didn't say anything about eliminating lines from the output, I'll assume you want all of them.


Be sure to put the larger file first, in this case it was file2

awk 'FNR==NR{a[$1]=$2;next}(a[$1]){if(a[$1]!=$2)print $1,$2 RS $1,a[$1];else print $1,$2}' OFS='\t' file2 file1

Proof of Concept

$ awk 'FNR==NR{a[$1]=$2;next}(a[$1]){if(a[$1]!=$2)print $1,$2 RS $1,a[$1];else print $1,$2}' OFS='\t' file2 file1
user1   1.1.1.1
user1   99.22.54.214
user2   2.2.2.2
user4   4.4.4.4
user4   8.8.8.8

Alternatively, you can use this but it requires two calls to awk and is thus not as fast in performance:

awk 'FNR==NR{a[$1]=$2;next}(a[$1]){print $1,$2 RS $1,a[$1]}' OFS='\t' file2 file1 | awk '!a[$0]++'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜