在shell中的两个文件的列上的diff

时间:2022-09-04 11:03:48

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。

2 个解决方案

#1


6  

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

逐行比较两个文件并在另一个文件中生成差异

#2


1  

Use [ process substitution ]

使用[流程替换]

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

should do it. Note -y option with diff is for side-by-side o/p.

应该这样做。注意-y选项与diff是并排的o / p。

#1


6  

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

逐行比较两个文件并在另一个文件中生成差异

#2


1  

Use [ process substitution ]

使用[流程替换]

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

should do it. Note -y option with diff is for side-by-side o/p.

应该这样做。注意-y选项与diff是并排的o / p。