两个文件的内容之间的差异

Shr*_*uti 8 unix diff

我有两个文件,一个文件子集的其他,我想获得一个文件,其中包含两者都不常见的内容.例如

文件1

apple
mango
banana
orange
jackfruit
cherry
grapes
eggplant
okra
cabbage
Run Code Online (Sandbox Code Playgroud)

文件2

apple
banana
cherry
eggplant
cabbage
Run Code Online (Sandbox Code Playgroud)

结果文件,以上两个文件的区别

mango
orange
jackfruit
grapes
okra
Run Code Online (Sandbox Code Playgroud)

对此有任何想法表示赞赏.

Mar*_*ers 11

您可以对文件进行排序然后使用comm:

$ comm -23 <(sort file1.txt) <(sort file2.txt)
grapes
jackfruit
mango
okra
orange
Run Code Online (Sandbox Code Playgroud)

您可能还想使用comm -3而不是comm -23:

  -1              suppress lines unique to FILE1
  -2              suppress lines unique to FILE2
  -3              suppress lines that appear in both files


gho*_*g74 2

使用 awk,无需排序(减少开销)

$ awk 'FNR==NR{f[$1];next}(!($1 in f)) ' file2 file
mango
orange
jackfruit
grapes
okra
Run Code Online (Sandbox Code Playgroud)