Silver Searcher:如何仅返回与模式匹配的文件夹中的结果?

发布于 2025-01-19 17:12:03 字数 603 浏览 1 评论 0原文

我的文件结构是这样的:

A
   G (want to ignore)
   H (want to ignore)
   X (contains files I want to search)
B
   G (want to ignore)
   H (want to ignore)
   X (contains files I want to search)
C (want to ignore)
X (contains files I want to search)

我只想返回 X 是目录路径某处的结果。

看起来我可以使用 ag foo --ignore={G,H} 但要忽略的文件夹列表会非常长(几乎令人望而却步)。我通常想搜索所有文件夹,这样我就可以手动构建一个包含所有内容的全局忽略文件,并在我想忽略几乎所有内容时将其交换到位。

我发现这种语法可以让我在 ABX 文件夹中进行搜索,所以我很接近了!

ag foo */X

所以问题本质上是:如何才能拥有任意数量的正斜杠(0、1 或更多)?

My file structure is like this:

A
   G (want to ignore)
   H (want to ignore)
   X (contains files I want to search)
B
   G (want to ignore)
   H (want to ignore)
   X (contains files I want to search)
C (want to ignore)
X (contains files I want to search)

I want to only return results where X is somewhere the directory path.

It looks like I could use ag foo --ignore={G,H} but the list of folders to ignore would be very (almost prohibitively) long. I normally want to search in ALL of the folders so I could manually construct a global ignore file with everything and swap it in place when I want to ignore mostly everything.

I found that this syntax lets me search within A and B's X folder so I'm close!

ag foo */X

So the question is essentially: How can I have an arbitrary number of forward slashes (0, 1, or more)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一个人练习一个人 2025-01-26 17:12:03

简单ag命令是不够的,

我不认为这仅通过ag(银搜索器)命令。您需要以某种方式从整个目录树中过滤输入文件,并根据x目录名称过滤。

我会按照这些方式做一些事情:

ag 'search\sstring'  `tree -dfi | ag '\/X.*'`  | less

它做什么?

命令在:

`

执行,然后通过ag搜索输出。

  1. 滤波器目录
    tree -dfi ...打印整个路径(dir and files)您要搜索
  • -d ...仅打印目录
  • -f ...打印每个文件的完整路径前缀
  • -i ...跳过输出中的空格(尝试尽可能处理)

ag'\/x。*' ...搜索路径中的目录/x(随后发生的任何字符0或多次) - 使用任何将选择要搜索的目录的REGEXP。

  1. 搜索文本在文件

中具有可搜索的输出(您可以以任何方式重定向)

Simple ag command is not enough

I don't think this simply just via ag (The silver searcher) command only. You need to somehow filter the input files from the whole directory tree, filtered based on X directory name.

I would do something along these lines:

ag 'search\sstring'  `tree -dfi | ag '\/X.*'`  | less

What it does?

The command in:

`

is executed and the output is then searched via ag.

  1. filter directories
    tree -dfi ... prints the whole path (dir and files) you want to search
  • -d ... prints only directories
  • -f ... prints full path prefix for each file
  • -i ... skips spaces in the output (tries to be as processable as possible)

ag '\/X.*' ... searches for the directory /X within the path (followed by any character present 0 or many times) - use any regexp which will select the directory you want to search in.

  1. search for a text within files

ag 'search\sstring' ... searches for the 'search string' string within the filtered path

less is there for you to have searchable output (you can redirect any way you want)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文