【问题标题】:Shell line command Sorting command [duplicate]外壳线命令排序命令[重复]
【发布时间】:2011-11-10 20:24:18
【问题描述】:

我有一个 Masters.txt(所有记录)和一个 New.txt 文件。我想针对 Masters.txt 处理 New.txt 并输出 New.txt 中 Masters.txt 中不存在的所有行

我不确定这是否是 sort -u 命令可以做到的。

【问题讨论】:

标签: shell


【解决方案1】:
【解决方案2】:

首先使用sort 对两个文件进行排序,然后使用comm 命令列出仅存在于new.txt 中而不存在于masters.txt 中的行。比如:

sort masters.txt >masters_sorted.txt
sort new.txt >new_sorted.txt
comm -2 -3 new_sorted.txt masters_sorted.txt

comm 默认在其输出中生成三​​列;第 1 列包含第一个文件独有的行,第 2 列包含第二个文件独有的行;第 3 列包含两个文件共有的行。 -2 -3 开关抑制了第二列和第三列。

【讨论】:

  • 不应该是类似 comm -2 -3 new_sorted.txt masters_sorted.txt > new.txt
  • 好吧,如果你想将结果存储在new.txt(丢失旧的内容),那么是的,它应该。
  • hmm 是否有文件列表项限制...它似乎不起作用。没有输出,两个文件都没有变化
  • 你不需要临时文件:comm -23 <(sort masters.txt) <(sort new.ext)
猜你喜欢
  • 2021-03-27
  • 2015-10-19
  • 2015-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多