Shell 查找两个文件中的记录
我想找出两个文件中的相交记录。限制是记录可以出现在文件中的任何位置。为此,我使用下面的一段代码Shell 查找两个文件中的记录,shell,Shell,我想找出两个文件中的相交记录。限制是记录可以出现在文件中的任何位置。为此,我使用下面的一段代码 awk 'FNR==NR{a[$0];next}($0 in a)' 1.txt 2.txt 但它不能正常工作 假设我有两个文件1.txt和2.txt 1.txt 2.txt 输出应该是这样的 B A 请帮我解决这个问题。试试这个: grep -w -f 1.txt 2.txt -w状态表示单词 -f文件状态另一个选项: 使用comm命令,这就是此命令的目的: comm <(sort 1.
awk 'FNR==NR{a[$0];next}($0 in a)' 1.txt 2.txt
但它不能正常工作
假设我有两个文件1.txt和2.txt
1.txt
2.txt
输出应该是这样的
B
A
请帮我解决这个问题。试试这个:
grep -w -f 1.txt 2.txt
-w状态表示单词
-f文件状态另一个选项:
使用comm
命令,这就是此命令的目的:
comm <(sort 1.txt) <( sort 2.txt) -12
但是,comm
需要排序的文件。因此,过程替换,如
grep -w -f 1.txt 2.txt
comm <(sort 1.txt) <( sort 2.txt) -12
With no options, produce three-column output. Column one contains lines
unique to FILE1, column two contains lines unique to FILE2, and column
three contains lines common to both files.
-1 suppress column 1 (lines unique to FILE1)
-2 suppress column 2 (lines unique to FILE2)
-3 suppress column 3 (lines that appear in both files)