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

Bash:判断一个文件是否包含在另一个文件中

  •  2
  • gregseth  · 技术社区  · 12 年前

    我试图比较两个文件的内容,判断其中一个文件的属性是否完全包含在另一个文件中(也就是说,如果一个文件有三行,A、B和C,我能在第二个文件中按这个顺序找到这三行吗)。我看过 diff grep 但无法找到相关选项(如果有的话)。

    示例:

    file1.txt   file2.txt  <= should return true (file2 is included in file1)
    ---------   ---------
    abc         def
    def         ghi
    ghi
    jkl    
    
    file1.txt   file2.txt  <= should return false (file2 is not included in file1)
    ---------   ---------
    abc         abc
    def         ghi
    ghi
    jkl    
    

    知道吗?

    4 回复  |  直到 12 年前
        1
  •  1
  •   Community CDub    8 年前

    使用来自的答案 here

    使用以下python函数:

    def sublistExists(list1, list2):
        return ''.join(map(str, list2)) in ''.join(map(str, list1))
    

    实际操作:

    In [35]: a=[i.strip() for i in open("f1")]
    In [36]: b=[i.strip() for i in open("f2")]
    In [37]: c=[i.strip() for i in open("f3")]
    
    In [38]: a
    Out[38]: ['abc', 'def', 'ghi', 'jkl']
    
    In [39]: b
    Out[39]: ['def', 'ghi']
    
    In [40]: c
    Out[40]: ['abc', 'ghi']
    
    In [41]: sublistExists(a, b)
    Out[41]: True
    
    In [42]: sublistExists(a, c)
    Out[42]: False
    
        2
  •  1
  •   Micha Wiedenmann Lieven Keersmaekers    12 年前

    假设你 file2.txt 不包含对正则表达式具有特殊意义的字符,可以使用:

    grep "$(<file2.txt)" file1.txt
    
        3
  •  1
  •   Olivier Dulac    12 年前

    即使file2.txt包含特殊字符,这也应该有效:

    cp file1.txt file_read.txt
    
    while read -r a_line ; do
       first_line_found=$( fgrep -nx "${a_line}" file_read.txt 2>/dev/null | head -1 )
       if [ -z "$first_line_found" ]; 
       then 
            exit 1 # we couldn't find a_line in the file_read.txt
       else
            { echo "1,${first_line_found}d" ; echo "w" ; } | ed file_read.txt  #we delete up to line_found
       fi   
    done < file2.txt
    exit 0
    

    (“出口0”是为了“可读性”,所以可以很容易地看到,只有当fgrep在file1.txt中找不到一行时,它才会以1退出。这是不需要的)

    (fgrep是一个literral grep,用于搜索字符串(而不是regexp))

    (我还没有测试过以上内容,这只是一个总体想法。我希望它能起作用^^)

    “-x”强制它与行完全匹配,即没有额外的字符(即:“to”不能再与“toto”匹配。添加-x时只有“toto”会与“totto”匹配)

        4
  •  0
  •   Kent    12 年前

    请尝试一下这个awk“一行”^_^是否适用于您的真实文件。对于您问题中的示例文件,它起到了作用:

    awk 'FNR==NR{a=a $0;next}{b=b $0}
    END{while(match(b,a,m)){
        if(m[0]==a) {print "included";exit}
        b=substr(b,RSTART+RLENGTH)
       }
        print "not included"
    }' file2 file1