简体   繁体   English

检查一个文件中的所有行是否都存在于另一个文件中

[英]Check if all lines from one file are present somewhere in another file

I used file1 as a source of data for file2 and now I need to make sure that every single line of text from file1 occurs somewhere in file2 (and find out which lines are missing, if any). 我使用file1作为file2的数据源,现在我需要确保file1中的每一行文本都出现在file2 某个地方 (并找出哪些行丢失,如果有的话)。 It's probably important to note that while file1 has conveniently one search term per line, the terms can occur anywhere in the file2 including in the middle of a word. 值得注意的是,虽然file1每行有一个搜索项,但这些术语可以出现在file2 任何地方 ,包括在单词的中间。 Also would help if the matching was case insensitive - doesn't matter if the text in file2 is even in all caps as long as it's there. 如果匹配不区分大小写也会有所帮助 - 如果file2的文本甚至是全部大写,则无关紧要,只要它在那里。

The lines in file1 include spaces and all sorts of other special characters like -- . file1的行包括空格和各种其他特殊字符,如--

if grep -Fqvf file2 file1; then
    echo $"There are lines in file1 that don’t occur in file2."
fi

Grep options mean: Grep选项意味着:

-F, --fixed-strings       PATTERN is a set of newline-separated fixed strings
-f, --file=FILE           obtain PATTERN from FILE
-v, --invert-match        select non-matching lines
-q, --quiet, --silent     suppress all normal output

You can try 你可以试试

awk -f a.awk file1 file2

where a.awk is a.awk在哪里

BEGIN { IGNORECASE=1 }
NR==FNR {
    a[$0]++
    next
}
{
    for (i in a) 
        if (index($0,i)) 
            delete a[i]
}

END {
    for (i in a)
        print i
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM