bash - 链接 grep 命令以在文件中搜索与另一个模式匹配的模式
问题描述
如何链接多个 grep 命令?
例如,如果我想递归搜索所有可公开访问的 PHP 文件,即包含$_user_location = 'public
; 并在所有这些文件中搜索"SendQueue()
,我该怎么办?
我的几次失败尝试:
grep -rnw ./* -e "^.*user_location.*public" *.php | grep -i "^.*SendQueue().*" --color
grep -rnw ./* -e "^.*user_location.*public" *.php | xargs -0 -i "^.*SendQueue().*" --color
解决方案
使用文件名打印 grep 结果,提取文件名并将这些文件名传递给第二个 grep。
grep -H ..... | cut -d: -f1 | xargs -d'\n' grep ....
只要文件名中没有:
文件名就可以工作,通常没有文件名。
你总是可以做一个简单的旧循环:
for i in *.php; do
if grep -q .... "$i"; then
grep .... "$i"
fi
done
推荐阅读
- python - Python tkinter的视频播放器,当我暂停视频时,我无法重新播放
- vue.js - nuxt.js 生成卡在“生成”
- r - 对嵌套列进行变异以导致不支持的类(data.frame)
- c# - 单击父窗口时子窗体不闪烁
- c - 在 c 中的 libmodbus 中获取 modbus_report_slave_id 函数的分段错误
- javascript - 如何使用 props 组件发送 props react
- python-3.x - pandas.tools 在哪里
- reactjs - Material UI api 文档过时了吗?
- html - CSS水平调整大小?
- algorithm - 3路和2路合并排序不失一般性?