[英]Compare two files with awk or sed
This must be easy for you 这一定很容易
Here is file1 (one column) 这是file1(一列)
1
2
3
4
5
6
7
8
9
and here is file2 (two columns) 这里是file2(两列)
2 yay
3 ups
4 wow
8 hey
There must be a simple one liner to print out lines in file1 that don´t match to file2 必须有一个简单的单行内容来打印file1中与file2不匹配的行
awk 'NR==FNR{a[$1]++;next;}!($0 in a)' file2 file1
or using join with -v, as @Michael suggested: 或者使用-v加入,正如@Michael建议的那样:
join -v1 file1 file2
both will print : 两者都会打印:
1
5
6
7
9
You can do this by combining cut
and comm
: 你可以通过结合
cut
和comm
来做到这一点:
cut -d' ' -f1 file2 | comm -13 - file1
You might also consider join
, depending on how you want to handle repeated lines. 您可能还会考虑
join
,具体取决于您希望如何处理重复行。
This sed solution might work for you: 这个sed解决方案可能适合您:
{ seq 1 10; echo -e "2 yay\n3 ups\n4 wow\n8 hey"; } | sort -n |
sed '1{h;d};H;${x;s/\(\S\+\)\n\1[^\n]*\n//g;p};d'
1
5
6
7
9
10
Explanation: Sort the files numerically then using sed
slurp the file in to the hold space (HS). 说明:以数字方式对文件进行排序,然后使用
sed
将文件粘贴到保留空间(HS)。 At end of file swap to the HS and then delete lines with the duplicate keys. 在文件末尾交换到HS然后删除带有重复键的行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.