1

我是 Linux 命令的新手,我正在尝试在目录和包含该单词的所有目录名称中递归搜索一个说“apple”的单词。

我正在使用类似于 grep 的 ripgrep(rg) 来获取其中包含苹果的文件名

前任:

rg -l "apple" 

给我输出:

a/fruit.c
b/fruit.c
a/fruits.c
a/fruitsnames.c
a/fruity/fruits.c
b/fru/fruit/fru/fr.c
c/frts.c

有没有办法只能使用唯一的父文件夹名称?

Example output I expect is to get only the folder names:
a
b
c

使用 grep 的解决方案也很好。

太感谢了

4

2 回答 2

3

用于sed从 中删除所有内容/,然后用于sort -u删除重复项

ripgrep -l apple | sed 's#/.*##' | sort -u
于 2021-01-07T21:51:59.880 回答
1

使用 Perl 单线,sort -u因此保持唯一的目录:

rg -l "apple" | perl -pe 's{/[^/]+$}{}' | sort -u

或仅保留最高级别的目录:

rg -l "apple" | perl -pe 's{/.*}{}' | sort -u
于 2021-01-07T21:54:44.093 回答