Gir*_*ish 2 diff text-processing comm
我有两个文件,(没有空行/空格/制表符)
aa
bb
cc
hello
SearchText.json
xyz.txt
Run Code Online (Sandbox Code Playgroud)
SearchText.json
Run Code Online (Sandbox Code Playgroud)
我想要的最终输出是:(来自 /tmp/all 的所有不常见行)
aa
bb
cc
hello
xyz.txt
Run Code Online (Sandbox Code Playgroud)
我尝试过以下命令:-
# comm -23 /tmp/required /tmp/all
SearchText.json
Run Code Online (Sandbox Code Playgroud)
# comm -23 /tmp/all /tmp/required
aa
bb
cc
hello
SearchText.json
xyz.txt
Run Code Online (Sandbox Code Playgroud)
# comm -13 /tmp/all /tmp/required
SearchText.json
Run Code Online (Sandbox Code Playgroud)
# comm -13 /tmp/required /tmp/all
aa
bb
cc
hello
SearchText.json
xyz.txt
Run Code Online (Sandbox Code Playgroud)
# grep -vf /tmp/all /tmp/required
# grep -vf /tmp/required /tmp/all
aa
bb
cc
hello
SearchText.json
xyz.txt
Run Code Online (Sandbox Code Playgroud)
# comm -23 <(sort /tmp/all) <(sort /tmp/required)
aa
bb
cc
hello
SearchText.json
xyz.txt
Run Code Online (Sandbox Code Playgroud)
作为 的替代方案comm
,请考虑grep
:
grep -vxFf /tmp/required /tmp/all
Run Code Online (Sandbox Code Playgroud)
这要求文件 ( ) 中/tmp/all
不-v
存在 ( -f
) 的行/tmp/required
。为了避免将任何行解释/tmp/all
为正则表达式,我添加了“固定字符串”-F
标志。此外,我们要强制整行 in/tmp/all
匹配来自 的那一行/tmp/required
,因此我们使用该-x
选项。
此方法不需要排序输入。
我怀疑你的comm -23 <(sort ...) <(sort ...)
命令会起作用,如果“SearchText.json”行在两个文件中完全匹配(相同数量的尾随空格,如果有的话)。