Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Shell 查找两个文件中的记录_Shell - Fatal编程技术网

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)