Linux查找文件中的字符串命令


 

grep -nr 'archermind'

 

-r, --recursive
Read all files under each directory, recursively, following symbolic links
only if they are on the command line. This is equivalent to the -d
recurse option.

 

-n, --line-number
Prefix each line of output with the 1-based line number within its input
file. (-n is specified by POSIX.)

 

grep介紹   (1)grep 的一般格式為 grep [options] 基本正則表達式 [文件] 
      字符串參數最好采用是雙引號括,一是以防被誤解為shell命令,二是可以用來查找多個單詞組成的字符串 
      -c:只輸出匹配行的記數 
      -i:不區分大小寫(只適用於單個字符) 
      -h:查詢多個文件時不顯示文件名 
      -H:只顯示文件名 
      -l:查詢多文件時只輸出包含匹配字符的文件名 
      -n:只顯示匹配行及其行號 
      -s:不顯示不存在或無匹配文本的錯誤信息。 
      -v:顯示不包含匹配文本的所有行。 
  (2)舉例說明: 
      grep ^[^210] myfile 匹配myfile中以非2、1、0開頭的行 
      grep "[5-8][6-9][0-3]" myfile 匹配myfile中第一位為5|6|7|8,第二位6|7|8|9,第三位為0|1|2|3的三個字符的行 
      grep "4\{2,4\}" myfile 匹配myfile中含有44,444或4444的行 
      grep "\?" myfile匹配myfile中含有任意字符的行 
  (3)grep命令類名 
      [[:upper:]]   表示[A-Z] 
      [[:alnum:]]   表示[0-9a-zA-Z] 
      [[:lower:]]   表示[a-z] 
      [[:space:]]   表示空格或者tab鍵 
      [[:digit:]]   表示[0-9] 
      [[:alpha:]]   表示[a-zA-Z] 
    如:grep "5[[:digit:]][[:digit:]]" myfile 匹配myfile中含有5開頭接下去兩位都是數字的行。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM