基礎正規表示法

  1. 重要特殊字元(characters)
    RE字符         意義與範例
    ^word    待搜尋的字串(word)在行首。
    word$    待搜尋的字串(word)在行尾。
      .      代表『任意一個』字符,一定是一個任意字符。
      \      跳脫字符,將特殊符號的特殊意義去除。
      *      重複零個或多個的前一個 RE 字符
    \{n,m\}  連續 n 到 m 個的『前一個 RE 字符』
             若為 \{n\} 則是連續 n 個的前一個 RE 字符,
             若是 \{n,\} 則是連續 n 個以上的前一個 RE 字符。
      [ ]    在 [ ] 當中『僅代表一個待搜尋的字元』
             若 [^],^ 在中括號中表示 NOT。
    
  2. 找檔案 /etc/man.config 中行首是大寫 C 的行。
    [dywang@dywmac ~]$ grep -n ^C /etc/man.config 
    108:CAT		/bin/cat
    114:CMP		/usr/libexec/man-cmp.sh
    118:COMPRESS	/usr/bin/lzma
    119:COMPRESS_EXT	.lzma
    
  3. 查詢行尾是 s.
    [dywang@dywmac ~]$ grep -n 's\.$' /etc/man.config
    37:# and to determine the correspondence between extensions and decompressors.
    63:# lots of other nearby files and directories.
    144:# Enable/disable makewhatis database cron updates.
    
  4. 計算行首是 # 且只有這個註解符號,共有 34 行。
    [dywang@dywmac ~]$ grep '^#$' /etc/man.config | wc -l
    34
    
  5. 計算行首不是 # 的行數,也就是不是註解的行,共有 39 行。
    [dywang@dywmac ~]$ grep -v '^#' /etc/man.config | wc -l
    113
    
  6. 搜尋 C 接 H 或 M
    [dywang@dywmac ~]$ grep -n 'C[HM]' /etc/man.config
    80:# NOCACHE keeps man from creating cache pages ("cat pages")
    84:#NOCACHE
    111:# When CMP is defined man will try to avoid showing the same
    114:CMP		/usr/libexec/man-cmp.sh
    
  7. 搜尋行首 C 但不接 H 或 M
    [dywang@dywmac ~]$ grep -n '^C[^HM]' /etc/man.config
    108:CAT		/bin/cat
    118:COMPRESS	/usr/bin/lzma
    119:COMPRESS_EXT	.lzma
    
  8. 搜尋數字後至少接一個以上英文字母
    [dywang@dywmac ~]$ grep '[0-9][a-zA-Z][a-zA-Z]*' /etc/man.config
    # man.conf from man-1.6f
    MANPATH	/usr/X11R6/man
    MANPATH_MAP	/usr/X11R6/bin		/usr/X11R6/man
    MANPATH_MAP	/usr/bin/X11		/usr/X11R6/man
    # and the MANSECT environment variable is not set (1x-8x sections are used by
    MANSECT		1:1p:8:2:3:3p:4:5:6:7:9:0p:n:l:p:o:1x:2x:3x:4x:5x:6x:7x:8x
    
  9. 搜尋連續 12 到 15 個英文字母的字串
    [dywang@dywmac ~]$ grep '[a-zA-Z]\{12,15\}' /etc/man.config
    # Generated automatically from man.conf.in by the
    # pages corresponding to given man pages should be stored,
    # MANPATH		manpath_element	[corresponding_catdir]
    # and to determine the correspondence between extensions and decompressors.
    # Every automatically generated MANPATH includes these fields
    # NOAUTOPATH keeps man from automatically adding directories that look like
    #MANDEFOPTIONS	-a
    # Decompress with given decompressor when input file has given extension
    # If MAKEWHATISDBUPDATES variable is uncommented
    #MAKEWHATISDBUPDATES	n
    
  10. 搜尋連續 14 個以上的英文字母
    [dywang@dywmac ~]$ grep '[a-zA-Z]\{14,\}' /etc/man.config
    # and to determine the correspondence between extensions and decompressors.
    # If MAKEWHATISDBUPDATES variable is uncommented
    #MAKEWHATISDBUPDATES	n
    
  11. 搜尋剛好連續 13 個英文字母
    [dywang@dywmac ~]$ grep '[^a-zA-Z][a-zA-Z]\{13\}[^a-zA-Z]' /etc/man.config
    # Generated automatically from man.conf.in by the
    # pages corresponding to given man pages should be stored,
    # MANPATH		manpath_element	[corresponding_catdir]
    # and to determine the correspondence between extensions and decompressors.
    # Every automatically generated MANPATH includes these fields
    # NOAUTOPATH keeps man from automatically adding directories that look like
    #MANDEFOPTIONS	-a
    
  12. 搜尋 x 開始 y 結束的字串
    [dywang@dywmac ~]$ grep 'x.*y' /etc/man.config
    # Explicitly given catdirs override.
    # The command "man -a xyzzy" will show all man pages for xyzzy.
    # and the MANSECT environment variable is not set (1x-8x sections are used by
    
  13. 搜尋多個字串,例如:this 及 will 兩個字串。
    [root@dywssd bin]# grep 'this\|will' /etc/man.config 
    # For more information about this file, see the man pages man(1)
    # (so that this dir has both man1 etc. and cat1 etc. subdirs).
    # The keyword FSSTND will cause this behaviour.
    # The keyword FHS will cause this behaviour (and overrides FSSTND).
    # in the mandatory manpath already, but will keep man from statting
    # The command "man -a xyzzy" will show all man pages for xyzzy.
    # When CMP is defined man will try to avoid showing the same
    # will not update makewhatis database.
    # Otherwise the database will be updated.