代码之家  ›  专栏  ›  技术社区  ›  vehomzzz

查找每行一项的两个文本文件之间的差异[重复]

  •  71
  • vehomzzz  · 技术社区  · 14 年前

    我有两个文件:

    dsf
    sdfsd
    dsfsdf
    

    文件2

    ljljlj 
    lkklk 
    dsf
    sdfsd
    dsfsdf
    

    我想显示文件2中的内容,而不是文件1中的内容,所以文件3应该是

    ljljlj 
    lkklk 
    
    11 回复  |  直到 14 年前
        1
  •  51
  •   jopasserat    9 年前

    你可以试试

    grep -f file1 file2
    

    grep -v -F -x -f file1 file2
    
        2
  •  144
  •   dogbane    14 年前
    grep -Fxvf file1 file2
    

    旗帜的意思是:

    -F, --fixed-strings
                  Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched.    
    -x, --line-regexp
                  Select only those matches that exactly match the whole line.
    -v, --invert-match
                  Invert the sense of matching, to select non-matching lines.
    -f FILE, --file=FILE
                  Obtain patterns from FILE, one per line.  The empty file contains zero patterns, and therefore matches nothing.
    
        3
  •  39
  •   dogbane    14 年前

    你可以使用 comm 比较两个排序文件的命令

    comm -13 <(sort file1) <(sort file2)
    
        4
  •  12
  •   Luca Borrione    8 年前

    我成功地使用了

    diff "${file1}" "${file2}" | grep "<" | sed 's/^<//g' > "${diff_file}"
    

        5
  •  8
  •   Nate    14 年前

    如果你希望它们按一定的顺序排列,你可以使用 diff

    diff file1 file2 | grep ">"

        6
  •  6
  •   Dennis Williamson    14 年前
    join -v 2 <(sort file1) <(sort file2)
    
        7
  •  3
  •   Chals    11 年前
    文件1
    m1型
    平方米
    
    文件2
    m4级
    m5型
    
    >awk'NR==FNR{file1[$0]++;下一个}!(文件1中的$0)文件1文件2
    m5型
    
    平方米
    
    >获取“m1和m3”的awk命令是什么?像文件1而不是文件2?
    m1型
    立方米
    
        8
  •  3
  •   Community Nick Dandoulakis    7 年前

    Luca 他的回答对我很有用。

    diff file1 file2 | grep ">" | sed 's/^> //g' > diff_file
    

    注意sed中的搜索模式是 > 后面跟着一个空格。

        9
  •  1
  •   letsc    14 年前

    while read line
    do
        flag = 0
        while read line2
        do
           if ( "$line" = "$line2" )
            then
                flag = 1
            fi
         done < file1 
         if ( flag -eq 0 )
         then
             echo $line > file3
         fi
    done < file2
    

    注:该程序只是提供一个基本的洞察,如果你不想使用系统调用,如diff n comm.可以做什么。。

        10
  •  1
  •   glenn jackman    14 年前

    awk 'NR == FNR {file1[$0]++; next} !($0 in file1)' file1 file2

        11
  •  0
  •   Jahid Ramsy de Vos    8 年前

    与GNU sed :

    sed 's#[^^]#[&]#g;s#\^#\\^#g;s#^#/^#;s#$#$/d#' file1 | sed -f- file2
    

    工作原理:

    第一次 塞德

    /^[d][s][f]$/d
    /^[s][d][f][s][d]$/d
    /^[d][s][f][s][d][f]$/d
    

    然后它被用作 第二个剧本 塞德