使用 awk 输出的每一行作为 grep 模式
我想找到包含不同文件的列中保存的任何字符串的文件的每一行。
我已经尝试过grep "$(awk '{ print $1 }' file1.txt)" file2.txt
但这只是完整输出 file2.txt 。
我知道我以前曾使用在该网站上找到的模式完成过此操作,但我再也找不到该问题了。
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 triedgrep "$(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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我在OP的评论中看到也许这个问题不再是一个问题。然而,下面的轻微修改将处理空行的情况。只需添加一个检查以确保该行至少有一个字段:
如果带有模式的文件只是每行一组模式,那么它的一个更简单的版本是:
这会导致 grep 使用 file1 中的行作为图案。
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:
And if the file with the patterns is simply a set of patterns per line, then a much simpler version of it is:
That causes grep to use the lines in file1 as the patterns.
当您有
awk
时,无需使用grep
THere is no need to use
grep
when you haveawk
$(awk '{ print $1 }' file1.txt) | $(awk '{ print $1 }' file1.txt) | grep 文本 >文件.txt
$(awk '{ print $1 }' file1.txt) | grep text > file.txt