2014-10-06 33 views

回答

3

,只顯示文件名,沒有他們的路徑,你可以做

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. 
2

隨意嘗試:

grep -rle "====[1-9][0-9]*" /path/to/directory 
2

嘗試這樣的:

grep -rle '====[1-9][0-9]*' /path/to/files 

其中:

-r recurse into the directory (and sub-directories) 
-l only list files 
-e use regexp 

的正則表達式匹配四個相同的符號,後面跟着一個數字大於零,然後零個或更多的其他數字。

+2

-e實際上打開*擴展* regul ar表達式,這並不是實際需要的。 – 2014-10-06 22:21:29

+0

有沒有辦法顯示文件名所在的完整目錄?只是文件名本身? – sSmacKk 2014-10-06 22:54:24

+0

@JesseWatZ nope,'-E'如果是擴展的; thx爲'-r'編輯,我沒有錯過遞歸請求 – 2014-10-07 15:53:38

相關問題