Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Search 如果grep没有';不匹配_Search_Awk_Grep - Fatal编程技术网

Search 如果grep没有';不匹配

Search 如果grep没有';不匹配,search,awk,grep,Search,Awk,Grep,我有文件1 BOB JOHN SALLY 我有文件2 There was a boy called JOHN and he was playing with FRED while JILL went off to find a bucket of water from TOM but she fell down the hill. 我想遍历file1单词并在file2中搜索这些单词。 我想打印文件2中找不到的单词 因此,输出将是 BOB SALLY 我想如果grep失败,我想打印grep搜

我有文件1

BOB
JOHN
SALLY
我有文件2

There was a boy called JOHN and he was playing with FRED while
JILL went off to find a bucket of water from TOM but she
fell down the hill.
我想遍历file1单词并在file2中搜索这些单词。
我想打印文件2中找不到的单词

因此,输出将是

BOB
SALLY
我想如果grep失败,我想打印grep搜索的字符串

我从这里开始:

grep -o -f file1 file2 
但当然,这会回来

JOHN

如何获取不匹配的原始搜索字符串-改为打印?

对于显示的示例,请尝试以下内容

awk '
FNR==NR{
  arr[$0]
  next
}
{
  for(i in arr){
    if(index($0,i)){
      delete arr[i]
      next
    }
  }
}
END{
  for(i in arr){
    print i
  }
}
' file1 file2

这里有一个
grep
1行程序来完成这项工作:


grep-vxFf如果订单不重要,您可以使用:

awk '
    FNR == NR { a[$1]=0; next } 
    { for (i=1;i<=NF;i++)
        if ($i in a)
            a[$i]++
    }
    END { 
        for (i in a)
            if (!a[i])
                print i 
    }
' file1 file2
awk'
FNR==NR{a[$1]=0;next}
{for(i=1;i FNR==NR{a[$1]=0;next}
>{for(i=1;i如果($i在a中)
>a[$i]++
>     }
>结束{
>为了(我在a)
>如果(!a[i])
>打印i
>     }
>'文件1文件2
俏皮话
上下快速移动

tr
好吧,那太棒了。我想了一系列的流程替换,但没能成功。
tr
是关键。形式很好。
grep-vxFf是的@DavidC.RankinWow,如此优雅的解决方案,它们增加了我对这些强大工具的学习,谢谢@anubhava和DavidC.Rankin!现在你很好。
delete
I这是一个干净的方式,我也喜欢。很酷,你们两个都有有效的awk解决方案。与简明的
grep
解决方案相比,这种情况下的
awk
解决方案相形见绌。你得到了我的投票。文件1中的
JOHN
是否应该与文件2中的
JOHNSON
匹配?你得到的答案对此做出了不同的假设。哟你的问题包括一个这样的部分匹配的例子。