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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 14:54:07  来源:igfitidea点击:

diff on columns of two files in shell

linuxbashshellfilediff

提问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

回答by sjsam

Use [ process substitution ]

使用[进程替换]

diff -y <( awk '{print ,}' file1) <( awk '{print ,}' file2 )

should do it. Note -yoption with diffis for side-by-side o/p.

应该这样做。注意-y选项 withdiff用于并排 o/p。