将每行awk输出用作grep模式 [英] Using each line of awk output as grep pattern

查看:80
本文介绍了将每行awk输出用作grep模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我试过了

grep$(awk'{print $ 1}'file1.txt)file2.txt

但是只输出file2.txt



我知道我之前用这个网站上的模式做过这件事,但我再也找不到这个问题了。

解决方案

我在OP的评论中看到,也许问题不再是问题。但是,下面的细微修改将处理空行情况。只需添加一个检查以确保该行至少有一个字段:

  grep$(awk'{if(NF> ; 0)print $ 1}'file1)file2 

如果带有模式的文件只是一个每行一套模式,然后它的一个简单得多的版本是:

$ p $ grep -f file1 file2

code>

这会导致grep使用file1中的行作为模式。


I want to find every line of a file that contains any of the strings held in a column of a different file.

I have tried
grep "$(awk '{ print $1 }' file1.txt)" file2.txt
but that just outputs file2.txt in its entirety.

I know I've done this before with a pattern I found on this site, but I can't find that question anymore.

解决方案

I see in the OP's comment that maybe the question is no longer a question. However, the following slight modification will handle the blank line situation. Just add a check to make sure the line has at least one field:

grep "$(awk '{if (NF > 0) print $1}' file1)" file2

And if the file with the patterns is simply a set of patterns per line, then a much simpler version of it is:

grep -f file1 file2

That causes grep to use the lines in file1 as the patterns.

这篇关于将每行awk输出用作grep模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆