正规表示法就是处理字符串的方法,他是以行为单位来进行字符串的处理行为,正规表示法透过一些特殊符号的辅助,可让使用者轻易的达到『搜寻/删除/取代』某特定字符串的处理程序!git
使用正规表示法时,须要特别留意当时环境的语系为什么,不然可能会发现与别人不相同的撷取结果喔!底下的不少练习都是使用『 』这个语系数据来进行express
特殊符号bash |
表明意义app |
[:alnum:]ide |
表明英文大小写字符及数字,亦即 0-9, A-Z, a-z测试 |
[:alpha:]this |
表明任何英文大小写字符,亦即 A-Z, a-zgoogle |
[:blank:]编码 |
表明空格键与 [Tab] 按键二者spa |
[:cntrl:] |
表明键盘上面的控制按键,亦即包括 CR, LF, Tab, Del.. 等等 |
[:digit:] |
表明数字而已,亦即 0-9 |
[:graph:] |
除了空格符 (空格键与 [Tab] 按键) 外的其余全部按键 |
[:lower:] |
表明小写字符,亦即 a-z |
[:print:] |
表明任何能够被打印出来的字符 |
[:punct:] |
表明标点符号 (punctuation symbol),亦即:" ' ? ! ; : # $... |
[:upper:] |
表明大写字符,亦即 A-Z |
[:space:] |
任何会产生空白的字符,包括空格键, [Tab], CR 等等 |
[:xdigit:] |
表明 16 进位的数字类型,所以包括:0-9, A-F, a-f 的数字与字符 |
[dmtsai@study ~]$ grep [-A] [-B] [--color=auto] '搜寻字符串' filename
-A :后面可加数字,为 after 的意思,除了列出该行外,后续的 n 行也列出来;
-B :后面可加数字,为 befer 的意思,除了列出该行外,前面的 n 行也列出来;
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@localhost tmp]# alias | grep "mv"
[root@localhost tmp]# alias | grep -A1 -B2 "mv"
RE 字符 |
意义与范例 |
^word |
意义:待搜寻的字符串(word)在行首! 范例:搜寻行首为 # 开始的那一行,并列出行号 grep -n '^#' regular_express.txt |
word$ |
意义:待搜寻的字符串(word)在行尾! 范例:将行尾为 ! 的那一行打印出来,并列出行号 grep -n '!$' regular_express.txt |
. |
意义:表明『必定有一个任意字符』的字符! 范例:搜寻的字符串能够是 (eve) (eae) (eee) (e e),但不能仅有 (ee) !亦即 e 与 e 中间『必定』仅有一个字符,而空格符也是字符! grep -n 'e.e' regular_express.txt |
\ |
意义:跳脱字符,将特殊符号的特殊意义去除! 范例:搜寻含有单引号 ' 的那一行! grep -n \' regular_express.txt |
* |
意义:重复零个到无穷多个的前一个 RE 字符 范例:找出含有 (es) (ess) (esss) 等等的字符串,注意,由于 * 能够是 0 个,因此 es 也是符合带搜寻字符串。另外,由于 * 为重复『前一个 RE 字符』的符号,所以,在 * 以前必需要紧接着一个 RE 字符喔!例如任意字符则为『.*』! grep -n 'ess*' regular_express.txt |
[list] |
意义:字符集合的 RE 字符,里面列出想要撷取的字符! 范例:搜寻含有 (gl) 或 (gd) 的那一行,须要特别留意的是,在 [] 当中『谨表明一个待搜寻的字符』,例如『 a[afl]y 』表明搜寻的字符串能够是 aay, afy, aly 即 [afl] 表明 a 或 f 或 l 的意思! grep -n 'g[ld]' regular_express.txt |
[n1-n2] |
意义:字符集合的 RE 字符,里面列出想要撷取的字符范围! 范例:搜寻含有任意数字的那一行!需特别留意,在字符集合 [] 中的减号 - 是有特殊意义的,他表明两个字符之间的全部连续字符!但这个连续与否与 ASCII 编码有关,所以,你的编码须要设定正确(在 bash 当中,须要肯定 LANG 与 LANGUAGE 的变量是否正确!) 例如全部大写字符则为 [A-Z] grep -n '[A-Z]' regular_express.txt |
[^list] |
意义:字符集合的 RE 字符,里面列出不要的字符串或范围! 范例:搜寻的字符串能够是 (oog) (ood) 但不能是 (oot) ,那个 ^ 在 [] 内时,表明的意义是『反向选择』的意思。例如,我不要大写字符,则为 [^A-Z]。可是,须要特别注意的是,若是以 grep -n [^A-Z] regular_express.txt 来搜寻,却发现该档案内的全部行都被列出,为何?由于这个 [^A-Z] 是『非大写字符』的意思,由于每一行均有非大写字符,例如第一行的"Open Source"就有 p,e,n,o.... 等等的小写字 grep -n 'oo[^t]' regular_express.txt |
\{n,m\} |
意义:连续 n 到 m 个的『前一个 RE 字符』 意义:若为 \{n\} 则是连续 n 个的前一个 RE 字符, 意义:如果 \{n,\} 则是连续 n 个以上的前一个 RE 字符! 范例:在 g 与 g 之间有 2 个到 3 个的 o 存在的字符串,亦即 (goog)(gooog) grep -n 'go\{2,3\}g' regular_express.txt |
[root@localhost tmp]# cat test
"Open Source" is a good mechanism to develop programs.
Football game is not use feet only.
However, this dress is about $ 3183 dollars.^M
GNU is free air not free beer.^M
the symbol '*' is represented as start.
The gd software is a library for drafting programs.^M
You are the best is mean you are the no. 1.
The world <Happy> is the same with "glad".
google is the best tools for search keyword.
[root@localhost tmp]# grep -n "the" test #查找含有"the"的行
8:I can't finish the test.^M #-n,显示行号
12:the symbol '*' is represented as start.
15:You are the best is mean you are the no. 1.
16:The world <Happy> is the same with "glad".
18:google is the best tools for search keyword.
[root@localhost tmp]# grep -vn "the" test #-v,查找不含"the"的行
[root@localhost tmp]# grep -in "the" test #-i,不区分大小写
[root@localhost tmp]# grep -n 't[ae]st' test #查找含有test或者tast的行
[root@localhost tmp]# grep -n 'oo' test #查找含有oo的行
1:"Open Source" is a good mechanism to develop programs.
3:Football game is not use feet only.
18:google is the best tools for search keyword.
[root@localhost tmp]# grep -n '[^g]oo' test #不是以g开头的oo
3:Football game is not use feet only.
18:google is the best tools for search keyword.
[root@localhost tmp]# grep -n '[^a-z]oo' test #不是以小写字母开头的oo
3:Football game is not use feet only.
[root@localhost tmp]# grep -n '[^[:lower:]]oo' test
[root@localhost tmp]# grep -n '[0-9]' test #含有数字的行
5:However, this dress is about $ 3183 dollars.^M
15:You are the best is mean you are the no. 1.
[root@localhost tmp]# grep -n '[[:digit:]]' test
[root@localhost tmp]# grep -n '^the' test #以the开头的行
12:the symbol '*' is represented as start.
[root@localhost tmp]# grep -n '^[0-9]' test #以数字开头的行
[root@localhost tmp]# grep -n '^[[:digit:]]' test
[root@localhost tmp]# grep -n '^[a-z]' test #以小写字母开头的行
10:motorcycle is cheap than car.
12:the symbol '*' is represented as start.
18:google is the best tools for search keyword.
[root@localhost tmp]# grep -n '^[[:lower:]]' test
[root@localhost tmp]# grep -n '^[^a-zA-Z]' test #不是以字母开头的行
1:"Open Source" is a good mechanism to develop programs.
[root@localhost tmp]# grep -n '\.$' test #以.结尾的行
1:"Open Source" is a good mechanism to develop programs.
3:Football game is not use feet only.
10:motorcycle is cheap than car.
12:the symbol '*' is represented as start.
15:You are the best is mean you are the no. 1.
16:The world <Happy> is the same with "glad".
18:google is the best tools for search keyword.
[root@localhost tmp]# grep -n '^$' test #空行
[root@localhost tmp]# grep -vn '^$' test | grep -vn '^#' #去掉空行和注释行
u * (星星号):表明『重复前一个字符, 0 到无穷屡次』的意思,为组合形态
[root@localhost tmp]# grep -n 'g..d' test #g和d中间有两个字符
1:"Open Source" is a good mechanism to develop programs.
16:The world <Happy> is the same with "glad".
[root@localhost tmp]# grep -n 'ooo*' test #两个或以上的o
[root@localhost tmp]# grep -n 'goo*g' test #两个g之间至少有一个o
[root@localhost tmp]# grep -n 'g*g' test #至少一个g
[root@localhost tmp]# grep -n 'g.*g' test #含有两个g
[root@localhost tmp]# grep -n '[0-9][0-9]*' test #含有数值
[root@localhost tmp]# grep -n '[0-9]' test
[root@localhost tmp]# grep -n 'o\{2\}' test #含有连续两个o
1:"Open Source" is a good mechanism to develop programs.
3:Football game is not use feet only.
18:google is the best tools for search keyword.
[root@localhost tmp]# grep -n 'ooo*' test
[root@localhost tmp]# grep -n 'o\{2,5\}' test #含有连续2-5个o
1:"Open Source" is a good mechanism to develop programs.
3:Football game is not use feet only.
18:google is the best tools for search keyword.
[root@localhost tmp]# grep -n 'o\{5,\}' test #含有5个或以上个o
[root@localhost tmp]# grep -n 'go\{2,\}g' test #g和g之间含有2个或以上个o
18:google is the best tools for search keyword.
[root@localhost tmp]# grep -n 'gooo*g' test
[root@localhost tmp]# grep -n 'go\{2,5\}g' test #g和g之间含有2-5个o
18:google is the best tools for search keyword.