RE字符 意義與範例 ^word 待搜尋的字串(word)在行首。 範例:grep -n '^#' re.txt 搜尋行首為 # 開始的那一行。 word$ 待搜尋的字串(word)在行尾。 範例:grep -n '!$' re.txt 將行尾為 ! 的那一行列印出來。 . 代表『任意一個』字符,一定是一個任意字符。 範例:grep -n 'e.e' re.txt 搜尋的字串可以是 (eve) (eae) (eee) (e e), 但不能僅有 (ee)。 亦即 e 與 e 中間『一定』僅有一個字元,而空白字元也是字元。 \ 跳脫字符,將特殊符號的特殊意義去除。 範例:grep -n \' re.txt 搜尋含有單引號 ' 的那一行。 * 重複零個或多個的前一個 RE 字符 範例:grep -n 'ess*' re.txt 找出含有 (es) (ess) (esss) 等等的字串。 \{n,m\} 連續 n 到 m 個的『前一個 RE 字符』 若為 \{n\} 則是連續 n 個的前一個 RE 字符, 若是 \{n,\} 則是連續 n 個以上的前一個 RE 字符。 範例:grep -n 'go\{2,3\}g' re.txt 在 g 與 g 之間有 2 個到 3 個的 o 存在的字串,亦即 (goog)(gooog) [ ] 在 [ ] 當中『謹代表一個待搜尋的字元』 範例:grep -n 'g[ld]' re.txt 搜尋含有 (gl) 或 (gd) 的那一行 範例:grep -n '[0-9]' re.txt 搜尋含有任意數字的那一行。 在字元集合 [ ] 中的減號 - 是代表兩個字元之間的所有連續字元。 [^]:^ 在 [ ] 內時, 代表的意義是『反向選擇』 範例:grep -n 'oo[^t]' re.txt 搜尋的字串可以是 (oog) (ood) 但不能是 (oot)。 [[:digit:]] 數字,與[0-9]相同 [[:lower:]] 小寫字元,與[a-z]相同 [[:upper:]] 大寫字元,與[A-Z]相同 更多的表示法可下指令 man 7 regex 查詢。
[root@kvm5 ~]# cp /etc/auto.master /tmp [root@kvm5 ~]# cat /tmp/auto.master # Sample auto.master file # This is an automounter map and it has the following format # key [ -mount-options-separated-by-comma ] location # For details of the format look at autofs(5). # /misc /etc/auto.misc # # NOTE: mounts done from a hosts map will be mounted with the # "nosuid" and "nodev" options unless the "suid" and "dev" # options are explicitly given. # /net -hosts # # Include central master map if it can be found using # nsswitch sources. # # Note that if there are entries for /net or /misc (as # above) in the included master map any keys that are the # same will not be seen as the first read key seen takes # precedence. # +auto.master
[root@kvm5 ~]# grep 'format' /tmp/auto.master # This is an automounter map and it has the following format # For details of the format look at autofs(5). [root@kvm5 ~]# grep --color 'format' /tmp/auto.master # This is an automounter map and it has the following format # For details of the format look at autofs(5).
[root@kvm5 ~]# grep --color '^+' /tmp/auto.master +auto.master
[root@kvm5 ~]# grep --color 'the$' /tmp/auto.master # NOTE: mounts done from a hosts map will be mounted with the # above) in the included master map any keys that are the
[root@kvm5 ~]# grep --color 'auto.*' /tmp/auto.master # Sample auto.master file # This is an automounter map and it has the following format # For details of the format look at autofs(5). /misc /etc/auto.misc +auto.master [root@kvm5 ~]# grep --color 'auto\.*' /tmp/auto.master # Sample auto.master file # This is an automounter map and it has the following format # For details of the format look at autofs(5). /misc /etc/auto.misc +auto.master [root@kvm5 ~]# grep --color 'auto\.\.*' /tmp/auto.master # Sample auto.master file /misc /etc/auto.misc +auto.master
[ ]
來搜尋集合字元
[root@kvm5 ~]# grep --color 'auto[a-z]' /tmp/auto.master # This is an automounter map and it has the following format # For details of the format look at autofs(5).
[ ]
反向搜尋集合字元
[root@kvm5 ~]# grep --color 'auto[^a-z]' /tmp/auto.master # Sample auto.master file /misc /etc/auto.misc +auto.master
[root@kvm5 ~]# grep --color '[0-9]' /tmp/auto.master # For details of the format look at autofs(5).
[root@kvm5 ~]# grep --color '[[:upper:]]' /tmp/auto.master # Sample auto.master file # This is an automounter map and it has the following format # For details of the format look at autofs(5). # NOTE: mounts done from a hosts map will be mounted with the # Include central master map if it can be found using # Note that if there are entries for /net or /misc (as
#
,即非註解行
[root@kvm5 ~]# grep --color '^[^#]' /tmp/auto.master /misc /etc/auto.misc /net -hosts +auto.master
>
的字行( ! 在正規表示法中並不是特殊字元):
grep -n '[!>]' re.txt
2015-04-13