我是新來使用grep和我需要所以在這裏進行一個相當複雜的查詢雲:GREP帶通配符,但不包括特定任期,並返回文件名
我想用grep遞歸進入目錄字符串:====\d+
其中\ d +是一個或多個小數(perl語法),並且該字符串與====0
不同。
我想讓grep返回包含====\d+
的文件的文件名。
我是新來使用grep和我需要所以在這裏進行一個相當複雜的查詢雲:GREP帶通配符,但不包括特定任期,並返回文件名
我想用grep遞歸進入目錄字符串:====\d+
其中\ d +是一個或多個小數(perl語法),並且該字符串與====0
不同。
我想讓grep返回包含====\d+
的文件的文件名。
,只顯示文件名,沒有他們的路徑,你可以做
grep -ERl '====[1-9]\d*' . | while read name; do basename $name; done
或者,如果你的文件名可以包含空格,換行或其他陌生感,使用
grep -ZERl '====[1-9]\d*' . | while IFS= read -r -d '' name; do
basename "$name";
done
的grep
標誌使用(從GNU grep
的手冊):
-E, --extended-regexp
Interpret PATTERN as an extended regular expression (ERE, see
below). (-E is specified by POSIX.)
-R, --dereference-recursive
Read all files under each directory, recursively. Follow all
symbolic links, unlike -r.
-l, --files-with-matches
Suppress normal output; instead print the name of each input
file from which output would normally have been printed. The
scanning will stop on the first match. (-l is specified by
POSIX.)
-Z, --null
Output a zero byte (the ASCII NUL character) instead of the
character that normally follows a file name. For example, grep
-lZ outputs a zero byte after each file name instead of the
usual newline. This option makes the output unambiguous, even
in the presence of file names containing unusual characters like
newlines. This option can be used with commands like find
-print0, perl -0, sort -z, and xargs -0 to process arbitrary
file names, even those that contain newline characters.
隨意嘗試:
grep -rle "====[1-9][0-9]*" /path/to/directory
嘗試這樣的:
grep -rle '====[1-9][0-9]*' /path/to/files
其中:
-r recurse into the directory (and sub-directories)
-l only list files
-e use regexp
的正則表達式匹配四個相同的符號,後面跟着一個數字大於零,然後零個或更多的其他數字。
-e實際上打開*擴展* regul ar表達式,這並不是實際需要的。 – 2014-10-06 22:21:29
有沒有辦法顯示文件名所在的完整目錄?只是文件名本身? – sSmacKk 2014-10-06 22:54:24
@JesseWatZ nope,'-E'如果是擴展的; thx爲'-r'編輯,我沒有錯過遞歸請求 – 2014-10-07 15:53:38