如何在awk中运行grep? [英] How to run grep inside awk?

查看:139
本文介绍了如何在awk中运行grep?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个文件 input.txt ,只有少量列和少量行,第一列是关键字,目录 dir 与包含这些键的文件。我想在 dir 中包含这些关键字的文件中查找所有行。起初我试图运行命令

  cat input.txt | awk'{print $ 1}'| xargs grep dir 

这不起作用,因为它认为键是我的文件系统上的路径。接下来,我尝试了类似于

  cat input.txt | awk'{system(grep -rn dir $ 1)}'

但这不起作用无论如何,最终我不得不承认,即使这样也行不通

  cat input.txt |在我尝试使用 \ 来转义空格和 $ 标志,我来这里是为了征求你的建议,有什么想法吗?



当然我可以在`cat input.txt中为x做类似于

  `;做grep -rn $ x dir;完成

这不够好,因为它需要两个命令,但我只需要一个。这也显示了为什么 xargs 不起作用,该参数不是最后一个参数。

解决方案 div>

尝试以下内容:

  awk'{print $ 1}'input.txt | xargs -n 1 -I pattern grep -rn pattern dir 


Suppose I have a file input.txt with few columns and few rows, the first column is the key, and a directory dir with files which contain some of these keys. I want to find all lines in the files in dir which contain these key words. At first I tried to run the command

cat input.txt | awk '{print $1}' | xargs grep dir

This doesn't work because it thinks the keys are paths on my file system. Next I tried something like

cat input.txt | awk '{system("grep -rn dir $1")}'

But this didn't work either, eventually I have to admit that even this doesn't work

cat input.txt | awk '{system("echo $1")}'

After I tried to use \ to escape the white space and the $ sign, I came here to ask for your advice, any ideas?

Of course I can do something like

for x in `cat input.txt` ; do grep -rn $x dir ; done

This is not good enough, because it takes two commands, but I want only one. This also shows why xargs doesn't work, the parameter is not the last argument

解决方案

Try following

awk '{print $1}' input.txt | xargs -n 1 -I pattern grep -rn pattern dir

这篇关于如何在awk中运行grep?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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