Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.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
Bash 链grep命令以搜索与另一个模式匹配的文件中的模式_Bash_Grep - Fatal编程技术网

Bash 链grep命令以搜索与另一个模式匹配的文件中的模式

Bash 链grep命令以搜索与另一个模式匹配的文件中的模式,bash,grep,Bash,Grep,如何链接多个grep命令 例如,如果我想递归搜索所有可公开访问的PHP文件,即那些包含$\u user\u location='public;然后在所有这些文件中搜索“SendQueue(),我该怎么办 我的几次失败尝试: grep -rnw ./* -e "^.*user_location.*public" *.php | grep -i "^.*SendQueue().*" --color 打印带有文件名的grep结果,提取文件名并将这些文件名传递给

如何链接多个grep命令

例如,如果我想递归搜索所有可公开访问的PHP文件,即那些包含
$\u user\u location='public
;然后在所有这些文件中搜索
“SendQueue()
,我该怎么办

我的几次失败尝试:

grep -rnw ./* -e "^.*user_location.*public" *.php | grep -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

打印带有文件名的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

如果您将
-l
选项添加到第一个grep,您将获得可以提供给第二个grep的所有文件名,如:

grep -i "^.*SendQueue().*" --color $(grep -l ...)

假设文件名中没有特殊字符。

如果在第一个grep中添加
-l
选项,您将获得可以提供给第二个grep的所有文件名,如:

grep -i "^.*SendQueue().*" --color $(grep -l ...)
假设文件名中没有特殊字符。

使用awk:

$ awk '
/SendQueue\(\)/ {             # hash all SendQueue() containing records
    a[++i]=$0
}
/.*user_location.*public/ {   # if condition met, flag up
    f=1
}
END {
    if(f)                     # if flag up
        for(j=1;j<=i;j++)     # output all hashed records
            print a[j]
}' file
在缺少样本输出的情况下,您只能得到:

SendQueue()
对于多个文件:

$ for f in *.php ; do awk ... $f ; done
使用awk:

$ awk '
/SendQueue\(\)/ {             # hash all SendQueue() containing records
    a[++i]=$0
}
/.*user_location.*public/ {   # if condition met, flag up
    f=1
}
END {
    if(f)                     # if flag up
        for(j=1;j<=i;j++)     # output all hashed records
            print a[j]
}' file
在缺少样本输出的情况下,您只能得到:

SendQueue()
对于多个文件:

$ for f in *.php ; do awk ... $f ; done

我可以提到两种方法:

POSIX方式

您可以使用
find(1)
进行递归搜索。
find
由POSIX定义,很可能包含在您的系统中

find.-type f-name'*.php'-exec grep-q“\$\u user\u location.*=.*'public”{}\.-exec grep'SendQueue(){}+
以下是此命令的作用说明:

  • -键入f
    查找文件
  • -name'*.php
    加后缀
    .php
  • -exec grep-q…{}\;
    分别运行第一个grep序列
  • -exec grep{}+
    在先前匹配的文件上运行第二个grep序列
急流路

是一个非常快速的递归
grep
工具。这将花费更少的搜索时间,但您需要单独获取它

rg--glob'*.php'-l“\$\u用户位置。*=.*public”\xargs rg'SendQueue\(\)'
以下是此命令的作用说明:

  • --glob'*.php'
    只查找后缀为
    .php
  • -l
    仅列出匹配的文件
  • 我们输入第一个查询并将所有匹配的文件传送到
    xargs
  • xargs
    使用第二个查询运行
    rg
    ,并将收到的文件添加为参数,以便
    ripgrep
    仅搜索这些文件
使用哪一个

ripgrep
确实适用于庞大的目录,但对于您所要求的内容来说,它真的不是必需的。在大多数情况下,选择
find
就足够了。您获得
ripgrep
所花费的时间可能会超过您使用它进行此特定操作所节省的时间。
ripgrep
是一个r不管怎样,这都是个不错的工具

编辑:

find
命令有2个
-exec
选项:

  • -exec grep(…){}\;
    这将为每个文件匹配调用grep命令。这将运行以下操作:
grep(query)file1.php
grep(查询)file2.php
grep(query)file3.php
find
跟踪每个文件的命令结果,如果成功,则将其传递到下一个测试

  • -exec grep(…){}+
    这将调用命令,并将所有文件作为参数附加。这将展开为:
grep(查询)file1.php file2.php file3.php

我可以提到两种方法:

POSIX方式

您可以使用
find(1)
进行递归搜索。
find
由POSIX定义,很可能包含在您的系统中

find.-type f-name'*.php'-exec grep-q“\$\u user\u location.*=.*'public”{}\.-exec grep'SendQueue(){}+
以下是此命令的作用说明:

  • -键入f
    查找文件
  • -name'*.php
    加后缀
    .php
  • -exec grep-q…{}\;
    分别运行第一个grep序列
  • -exec grep{}+
    在先前匹配的文件上运行第二个grep序列
急流路

是一个非常快速的递归
grep
工具。这将花费更少的搜索时间,但您需要单独获取它

rg--glob'*.php'-l“\$\u用户位置。*=.*public”\xargs rg'SendQueue\(\)'
以下是此命令的作用说明:

  • --glob'*.php'
    只查找后缀为
    .php
  • -l
    仅列出匹配的文件
  • 我们输入第一个查询并将所有匹配的文件传送到
    xargs
  • xargs
    使用第二个查询运行
    rg
    ,并将收到的文件添加为参数,以便
    ripgrep
    仅搜索这些文件
使用哪一个

ripgrep
确实适用于庞大的目录,但对于您所要求的内容来说,它真的不是必需的。在大多数情况下,选择
find
就足够了。您获得
ripgrep
所花费的时间可能会超过您使用它进行此特定操作所节省的时间。
ripgrep
是一个r不管怎样,这都是个不错的工具

编辑:

find
命令有2个
-
grep -c "\(SendQueue()\|_user_location   = 'public\)" *.txt | sed -ne 's/:2$//p'
find /path -type f -name '*.php' -exec grep -c \
    "\(SendQueue()\|_user_location   = 'public\)" {} + |
    sed -ne 's/:2$//p'
"^[^#/]*\(SendQueue()\|_user_location   = 'public\)"