`Globbing` 语法(实际上是 `花括号扩展`)与 `grep`:为什么这不起作用?

huangapple go评论60阅读模式
英文:

Globbing syntax (actually `brace expansion`) with grep: Why doesn't this work?

问题

以下是翻译好的部分:

"我正在尝试查找函数的所有实例,同时改善我对grep和globbing的理解。然而,我无法弄清楚我误解了什么。

例如,如果我不使用'glob',并且我想在以'Rmd'结尾的文件中搜索字符串,我可以得到以下结果

$ grep -rl --include="*Rmd" "plotRFPByPosition" .
./R_notebooks/detecting_stall_sites.Rmd
./R_notebooks/effects_of_data_filtering.Rmd
./R_notebooks/impact_of_ramp.Rmd
./Paper/final_analyses_and_plots.Rmd
$

类似地,对于以R结尾的文件,我可以得到

$ grep -rl --include="*R" "plotRFPByPosition" .
./R_notebooks/helperFunctions.R
./Paper/R_notebooks/helperFunctions.R
$

但是,如果我想要查找以RmdR结尾的文件,这不会返回任何结果(甚至不返回先前的结果)。

$ grep -rl --include="*{Rmd,R}" "plotRFPByPosition" .
$ 

我还尝试过--include="{*Rmd,*R}"--include="{Rmd,R}",但这些都不起作用。

我误解了globbing的哪个简单方面?"

英文:

I'm trying to find all of the instances of a function while also improving my understanding of grep and globbing. However, I can't figure out what I'm misunderstanding.

For example, if I don't glob, and I want to search for a string in files ending in Rmd, I can get the following results

$ grep -rl --include="*Rmd" "plotRFPByPosition" .
./R_notebooks/detecting_stall_sites.Rmd
./R_notebooks/effects_of_data_filtering.Rmd
./R_notebooks/impact_of_ramp.Rmd
./Paper/final_analyses_and_plots.Rmd
$

Similarly for files ending in R, I can get

$ grep -rl --include="*R" "plotRFPByPosition" .
./R_notebooks/helperFunctions.R
./Paper/R_notebooks/helperFunctions.R
$

But if I want to look for files ending in either Rmd or R, this doesn't return anything (not even the previous results.

$ grep -rl --include="*{Rmd,R}" "plotRFPByPosition" .
$ 

I've also tried --include="{*Rmd,*R}" and --include="{Rmd,R}" but none of these work.

What simple aspect about globbing do I misunderstand?

答案1

得分: 2

要使用多个模式,您可以使用多个 --include 选项。

grep -rl --include="*Rmd" --include="*R" "plotRFPByPosition"
英文:

To use multiple patterns you can use multiple --include options.

grep -rl --include="*Rmd" --include="*R" "plotRFPByPosition"

答案2

得分: 1

花括号展开是与通配符不同的一个步骤,而不是通配符操作的一部分;它不是由fnmatch()glob()执行的,这两个C函数通常用于通配符类型的操作(除了通常包含自己扩展通配符引擎的shell之外的程序)。

您可以在这里使用它,将花括号从引号中取出:

grep -rl --include="*"{Rmd,R} plotRFPByPosition

将由shell(执行花括号展开)重写为:

grep -rl --include="*"Rmd --include="*"R plotRFPByPosition

...之后,grep本身可能会使用glob()fnmatch()或类似方法来筛选与这两个模式之一匹配的文件。

英文:

Brace expansion is a separate step from globbing, rather than part of the glob operation; it isn't performed by fnmatch() or glob(), the two C functions most commonly used for glob-type operations (by programs other than shells, which typically include their own extended globbing engines).

You can use it here, by taking the braces out of quotes:

grep -rl --include="*"{Rmd,R} plotRFPByPosition

will be rewritten by the shell (performing brace expansion) into:

grep -rl --include="*"Rmd --include="*"R plotRFPByPosition

...after which point grep itself will presumably use glob(), fnmatch() or similar to filter for files matching either of those patterns.

huangapple
  • 本文由 发表于 2023年7月14日 03:18:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76682611.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定