簡體   English   中英

使用 grep 或 rg 查找不同的目錄名稱

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM