【问题标题】:diff on columns of two files in shellshell中两个文件的列的差异
【发布时间】:2016-11-17 06:34:29
【问题描述】:

我想做一件非常简单的事情。我有两个文件如下:

FILE 1:
A s1 p1
B s2 p2
C s3 p3

FILE2:
B s4 p4
A s1 p1
C s6 p6

我想从文件中提取第一列和第三列并打印该文件的差异。一种简单的方法是使用两个文件的 cut -f1,3 创建中间文件并进行 diff。这正是我想要的输出。但我不想创建中间文件。任何一个简单的班轮都可以做到这一点。

还有一点,两个文件都没有排序,所以不能直接使用join。

【问题讨论】:

    标签: linux bash shell file diff


    【解决方案1】:

    试试这个:

    diff <(cut -f1,3 file1) <(cut -f1,3 file2)
    

    参考资料:

    Compare two files line by line and generate the difference in another file

    【讨论】:

    • 是的,正如 sjsam 所发布的,它在 bash 中称为进程替换;在鱼壳中你使用psub:diff (cut -f1,3 file1 | psub) (cut -f1,3 file2 | psub)(你还没有&gt;(...)
    【解决方案2】:

    使用[ process substitution ]

    diff -y <( awk '{print $1,$3}' file1) <( awk '{print $1,$3}' file2 )
    

    应该这样做。注意-y 选项和diff 用于并排o/p。

    【讨论】:

      猜你喜欢
      • 2011-10-28
      • 1970-01-01
      • 1970-01-01
      • 2021-10-03
      • 1970-01-01
      • 2023-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多