[英]Finding the distinct directory names using grep or rg
我是 Linux 命令的新手,我正在尝试在目录和包含该单词的所有目录名称中递归搜索一个说“apple”的单词。
我正在使用类似于 grep 的 ripgrep(rg) 来获取其中包含苹果的文件名
前任:
rg -l "apple"
给我 output:
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 的解决方案也不错。
太感谢了
使用sed
从/
中删除所有内容,然后使用sort -u
删除重复项
ripgrep -l apple | sed 's#/.*##' | sort -u
使用 Perl 单行和sort -u
以保持唯一目录:
rg -l "apple" | perl -pe 's{/[^/]+$}{}' | sort -u
或仅保留最高级别的目录:
rg -l "apple" | perl -pe 's{/.*}{}' | sort -u
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.