bash shell 中两个文件的列的差异
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38372561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
diff on columns of two files in shell
提问by Shweta
I want to do a very simple thing. I have two files as follows:
我想做一件很简单的事情。我有两个文件如下:
FILE 1:
A s1 p1
B s2 p2
C s3 p3
FILE2:
B s4 p4
A s1 p1
C s6 p6
I want to extract first and third column from both file and print diff of that file. One easy way is to create intermediate files with cut -f1,3 of both files and do diff. Thats what exactly i want my output is. But i don't want to create intermediate file. Any simple one liner to do that.
我想从该文件的文件和打印差异中提取第一列和第三列。一种简单的方法是使用两个文件的 cut -f1,3 创建中间文件并执行 diff。这正是我想要的输出。但我不想创建中间文件。任何简单的一个班轮来做到这一点。
One more thing, both the files are NOT sorted, so unable to use join directly.
还有一件事,两个文件都没有排序,所以无法直接使用 join 。
回答by xealits
Try this:
尝试这个:
diff <(cut -f1,3 file1) <(cut -f1,3 file2)
References:
参考:
Compare two files line by line and generate the difference in another file
回答by sjsam
使用[进程替换]
diff -y <( awk '{print ,}' file1) <( awk '{print ,}' file2 )
should do it. Note -y
option with diff
is for side-by-side o/p.
应该这样做。注意-y
选项 withdiff
用于并排 o/p。