美文网首页生物信息学与算法
linux中文件比较comm/diff

linux中文件比较comm/diff

作者: 生信编程日常 | 来源:发表于2020-04-05 20:23 被阅读0次

    比较两个文件内容的不同,主要有comm和diff两个命令。

    1. comm
      主要用法:comm file1.txt file2.txt

    在comm比较之前需要对两个文件进行sort,可以输出在仅第一个文件里出现的、仅在第二个文件里出现的和两个文件共有的内容。

    comm -1 file1.txt file2.txt # 不显示第一个文件特有的内容,显示第二个文件特有的内容和共有的内容
    comm -2 file1.txt file2.txt  # 不显示第二个文件特有内容
    comm -3 file1.txt file2.txt # 不显示共有
    comm -12 file1.txt file2.txt # 第一个和第二个共有
    comm  -23 file1.txt file2.txt # 第一个特有
    comm -13 file1.txt file2.txt  # 第二个特有
    

    也可以专门比较某两列或某几列:

    comm -12 <(sort file1.txt | cut -f1) <(sort file1.txt | cut -f2.txt) # 这个只能在前台运行
    
    1. diff
      diff可以直接输出比较结果,对第一个文件提出修改建议使得第一个文件与第二个文件一致。
      创建两个文件:cat file1.txt
      one
      two
      three
      第二个文件: file2.txt
      one
      two
    diff file1.txt file2.txt
    

    输出:
    3d2
    < three
    即第一个文件的第三行three需要被删除。

    文件夹内容比较:

    diff -ruNa dir1/ dir2/
    

    相关文章

      网友评论

        本文标题:linux中文件比较comm/diff

        本文链接:https://www.haomeiwen.com/subject/shepphtx.html