bash 从两个文件中删除公共行

Gir*_*ish 2 diff text-processing comm

我有两个文件,(没有空行/空格/制表符)

/tmp/所有

aa  
bb  
cc  
hello  
SearchText.json  
xyz.txt  
Run Code Online (Sandbox Code Playgroud)

/tmp/必需

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)

Jef*_*ler 6

作为 的替代方案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”行在两个文件中完全匹配(相同数量的尾随空格,如果有的话)。