在 grep --include 中使用文件列表中的流

问题描述 投票:0回答:1

您好,我想使用文件列表流作为 grep 参数 include

请告诉我如何解决问题(请不要建议我使用循环,因为它会减慢这个脚本的速度)。

grep -rl --include={$(sed 's/\ /,/g' <<< $(basename -a $(grep -Eo 'ht+ps*:/+[[:graph:]]+' /path/to/fileslist)))} '' /path/for/search

echo $?
1
or
a=$(sed 's/\ /,/g' <<< $(basename -a $(grep -Eo 'ht+ps*:/+[[:graph:]]+' /path/to/fileslist)))
grep -rl --include={$a} '' /path/for/search

echo $?
1

but when I use it in explicitly (fileslist delimited by ,) it work properly
grep -rl --include={file1.ext,file2.ext,...,fileN.ext} '' /media/mint/
echo $?
0
variables grep include
1个回答
0
投票

您无法以这种方式动态构建和执行该语法。
它将需要至少一个抽象步骤,以便解析器将模式视为文字。

假设构建模式的代码是正确的,

a="$(sed 's/\ /,/g' <<< $(basename -a $(grep -Eo 'ht+ps*:/+[[:graph:]]+' /path/to/fileslist)))"

可以使用

eval

eval "grep -rl --include={$a} '' /path/for/search"

我通常不喜欢

eval
,有时会编写一个小的动态生成的代码文件,然后获取它,尽管它实际上并没有更快或更安全。

echo "grep -rl --include={$a} '' /path/for/search" > tmpfile
. tmpfile

但是为什么要包含空的正则表达式模式?
当您确实应该使用

grep
时,您是否尝试使用
find

这是一个 XY 问题吗

© www.soinside.com 2019 - 2024. All rights reserved.