Find the difference between two files

本小妞迷上赌 提交于 2021-01-29 03:26:47

问题


I have the following situation:

The file1.dat is like:

1 2
1 3
1 4
2 1

and the file2.dat is like:

1 2
2 1
2 3
3 4

I want to find the differences between the second file from the first. I tried wit grep -v -f file1 file2 but my real files are bigger than this two and when I tried with it the shell never ended is work.

The result should be:

2 3
3 4

The files are sorted and they have the same number of elements. Any way to find a solution with awk?


回答1:


Seems like you want lines in file2 that are not in file1:

$ awk 'FNR==NR{a[$0];next}!($0 in a)' file1 file2
2 3
3 4

However it's simpler to use comm:

$ comm -13 file1 file2
2 3
3 4


来源:https://stackoverflow.com/questions/15251188/find-the-difference-between-two-files

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!