我有两个文件:大约有 10 000 行的 file1 和有几百行的 file2。我想检查 file2 的所有行是否都出现在 file1 中。那是: ?线 ?? 文件2:?? 文件 1
是否有人不知道这些符号的含义或“检查文件 2 的所有行是否都出现在文件 1 中”的含义:任一文件中的几行等效行不会影响检查是否返回文件满足要求。
我该怎么做呢?
Min*_*Max 25
comm -13 <(sort -u file_1) <(sort -u file_2)
Run Code Online (Sandbox Code Playgroud)
此命令将输出file_2. 因此,如果输出为空,则所有file_2行都包含在file_1.
来自通讯员:
Run Code Online (Sandbox Code Playgroud)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)
[ $(grep -cxFf file2 <(sort -u file1)) = $(sort -u file2 | wc -l) ] &&
echo all there ||
echo some missing
Run Code Online (Sandbox Code Playgroud)
如果 file1 中(唯一行)中 file2 的匹配数与 file2 中唯一行数相同,则它们都在那里;否则,他们不是。
awk在它确实支持特定length(array)功能(以及awk可能支持的其他一些实现)的情况下使用 GNU ,如果对文件进行排序则不需要。
gawk 'FNR==NR{seen[$0];next} ($0 in seen){delete seen[$0]};
END{print (!length(seen))?"Matched":"Not Matched"}' file2 file1
Run Code Online (Sandbox Code Playgroud)
这是将file2读入一个seen以file2 的整行作为键调用的数组。
然后读取file1并读取每一行,如果与数组中的行匹配,则删除该键。
最后,如果数组为空,则表示file2 中的所有行都存在于file1 中,并将打印Matched,否则将显示Not Matched.
为了在所有awk实现中的兼容性。
awk 'FNR==NR{seen[$0];next} ($0 in seen){delete seen[$0]};
END{for(x in seen);print (!x)?"Matched":"Not Matched"}' file2 file1
Run Code Online (Sandbox Code Playgroud)
要仅在file2 中忽略空行/或带有空格的行,您需要添加NF到条件中NR==FNR && NF {...以跳过将它们读入数组。