正規表達式是一種符号表示法,被用來識别文本模式。在某種程度上,它們與比對 檔案和路徑名的 shell 通配符比較相似,但其規模更龐大。許多指令行工具和大多數的程式設計語言 都支援正規表達式,以此來幫助解決文本操作問題。
一、正規表達式的字元組成
<code>^ $ . [ ] { } - ? * + ( ) | \</code>
二、grep
grep 程式會在文本檔案中查找一個指定的正規表達式,并把比對行輸出到标準輸出。
<code>[root@node1 ~]</code><code># grep root /etc/passwd</code>
<code>root:x:0:0:root:</code><code>/root</code><code>:</code><code>/bin/bash</code>
<code>operator:x:11:0:operator:</code><code>/root</code><code>:</code><code>/sbin/nologin</code>
這個指令列出了檔案/etc/passwd中含有root的行。
文法規則:
grep [options] regex [file……]
其中regex是正規表達式
常用選項
選項描述
-i忽略大小寫。不會區分大小寫字元。也可用--ignore-case 來指定。
-v不比對。通常,grep 程式會列印包含比對項的文本行。這個選項導緻 grep 程式 隻會不包含比對項的文本行。也可用--invert-match 來指定。
-c列印比對的數量(或者是不比對的數目,若指定了-v 選項),而不是文本行本身。 也可用--count 選項來指定。
-l列印包含比對項的檔案名,而不是文本行本身,也可用--files-with-matches 選項來指定。
-L相似于-l 選項,但是隻是列印不包含比對項的檔案名。也可用--files-without-match 來指定。
-n在每個比對行之前列印出其位于檔案中的相應行号。也可用--line-number 選項來指定。
-h應用于多檔案搜尋,不輸出檔案名。也可用--no-filename 選項來指定。
-E 增強型的grep程式
<code>root@node1 ~]</code><code># grep -l lhl *</code>
<code>grep</code><code>: 160822.bak: Is a directory</code>
<code>a.txt</code>
<code>grep</code><code>: bin: Is a directory</code>
<code>username.conf</code>
<code>[root@node1 ~]</code><code># grep "^lhl" a.txt //以lhl開頭的行</code>
<code>lhl:x:0:0:lhl:</code><code>/lhl</code><code>:</code><code>/bin/bash</code>
<code>[root@node1 ~]</code><code># grep "bash$" a.txt //以bash結尾的行</code>
<code>[root@node1 ~]</code><code># grep -n \' a.txt //\的作用取消字元反義</code>
<code>12:uucp:x:10:14:</code><code>'uucp'</code><code>:</code><code>/var/spool/uucp</code><code>:</code><code>/sbin/nologin</code>
<code>[root@node1 ~]</code><code># grep l[az] a.txt //查找以l之後從a~z的行</code>
<code>saslauth:x:499:76:Saslauthd user:</code><code>/var/empty/saslauth</code><code>:</code><code>/sbin/nologin</code>
<code>[root@node1 ~]</code><code># grep -v "^#" a.txt //查找不一警号開頭的行</code>
<code>bin:x:1:1:bin:</code><code>/bin</code><code>:</code><code>/sbin/nologin</code>
<code>daemon:x:2:2:daemon:</code><code>/sbin</code><code>:</code><code>/sbin/nologin</code>
<code>[root@node1 ~]</code><code># grep [4,6] -n a.txt //有數字4~6的行</code>
<code>4:</code><code>#adm:x:3:4:adm:/var/adm:/sbin/nologin</code>
<code>5:</code><code>#lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin</code>
<code>9:</code><code>#shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown</code>
<code>[root@xuegod60 ~]</code><code># grep^g.*g a.txt //以g開頭和以g結束的行</code>
<code>games:x:12:100:games:</code><code>/usr/games</code><code>:</code><code>/sbin/nologin</code>
注意:
\? 用于修飾前導字元,表示前導字元出現0或1次
\+ 用于修飾前導字元,表示前導字元出現1或多次 例:a\+比對1或多個a
\{n,m\} 用于修飾前導字元,表示前導字元出現n至m次(n和m都是整數,且n<m)
例:a\{3,5\}比對3至5個連續的a
<code>[root@node1 ~]</code><code># grep 'lhl\{1,2\}' a.txt //出現lhl的行在1個或2個</code>
<code>#operator:x:11:0:operator:/lhl:/sbin/nologin</code>
<code>[root@node1 ~]</code><code># grep '\<lhl\>' a.txt //精确比對</code>
二、sed編輯器
sed是一種線上編輯器,其處理方式是每次處理一行内容,處理時,把目前的行存儲在臨時緩沖區中,sed則直接處理緩沖區中的内容,并将處理過後的内容送往螢幕顯示,接着處理下一行直至檔案内容末尾。
sed文法:
sed [選項] '[指令]' 檔案
選項及參數
<code>-n 抑制自動(預設的) 輸出 *** 讀取下一個輸入行</code>
<code>-e 表示直接在指令行模式上進行</code><code>sed</code><code>的操作,是預設的選項,可以不寫</code>
<code>-f 将</code><code>sed</code><code>操作寫在一個檔案裡,用的的方法則是:“-f filename”就可以按照内容進行</code><code>sed</code><code>的操作了。</code>
<code>-i 表示插入,後接字元串,添加到目前行的上一行</code>
<code>-i.bak 編輯的同時創造.bak的備份</code>
<code>-r 使用擴充的正規表達式 ***</code>
<code>參數:</code>
<code>a 在比對後追加 ***</code>
<code>i 在比對前插入 ***</code>
<code>p 列印某個選擇資料,通常與-n安靜模式一起使用。 ***</code>
<code>d 删除 ***</code>
<code>r</code><code>/R</code> <code>讀取檔案/一行</code>
<code>w 另存</code>
<code>s 查找,還可以替換,可參考vim裡面的搜尋替換功能。% s</code><code>/root/lhl/g</code> <code>全部替換。</code>
<code>c 替換,後接字元串,用它代替n1到n2行之間的行</code>
<code>y 替換</code>
<code>h</code><code>/H</code> <code>複制拷貝/追加模式空間(緩沖區)到存放空間</code>
<code>g</code><code>/G</code> <code>粘貼從存放空間取回/追加到模式空間</code>
<code>x 兩個空間内容的交換</code>
<code>n</code><code>/N</code> <code>拷貝/追加下一行内容到目前</code>
<code>D 删除\n之前的内容</code>
<code>P 列印\n之前的内容</code>
<code>b 無條件跳轉</code>
<code>t 滿足比對後的跳轉</code>
<code>T 不滿足比對時跳轉</code>
常用分隔符:_、/、|、.等
[root@node1 ~]# sed 's/lhl/root/g' a.txt //替換操作
用 & 表示比對的字元串
[root@xuegod60 ~]# sed 's/root/(&)/' /etc/passwd>
sed 預設隻替換搜尋字元串的第一次出現 , 利用 /g 可以替換搜尋字元串所有
<code>[root@node1 ~]</code><code>#sed -e '3,5s/nologin/bash/' -e'9,11s/sbin/bin/' passwd > b.txt //同時替換多項内容:-e</code>
<code>[root@node1 ~]</code><code>#sed -n '3p' passwd #顯示檔案第三行</code>
<code>[root@node1 ~]</code><code># cat -n /etc/passwd | sed -n '2,5p' //顯示2—5行的内容p與-n選項配合使用</code>
<code> </code><code>2 bin:x:1:1:bin:</code><code>/bin</code><code>:</code><code>/sbin/nologin</code>
<code> </code><code>3 daemon:x:2:2:daemon:</code><code>/sbin</code><code>:</code><code>/sbin/nologin</code>
<code> </code><code>4 adm:x:3:4:adm:</code><code>/var/adm</code><code>:</code><code>/sbin/nologin</code>
<code> </code><code>5 lp:x:4:7:lp:</code><code>/var/spool/lpd</code><code>:</code><code>/sbin/nologin</code>
<code>[root@node1 </code><code>test</code><code>]</code><code>#sed -n '1,3!p' passwd //顯示檔案除前三行之外的全部内容</code>
<code>[root@node1 ~]</code><code># cat -n /etc/passwd | sed -n '3,+3p'//顯示第三行及第三行之後的内容</code>
<code> </code><code>6 </code><code>sync</code><code>:x:5:0:</code><code>sync</code><code>:</code><code>/sbin</code><code>:</code><code>/bin/sync</code>
<code>[root@node1 ~]</code><code># sed'1i###' passwd //在檔案頭插入“###”如果沒有1的話則是在每一行之前插入###</code>
<code>[root@node1 ~]</code><code># sed'$a@@@' passwd //在檔案尾插入"@@@"</code>
注意:nl指令在linux系統中用來計算檔案中行号。nl可以将輸出的檔案内容自動的加上行号
<code>[root@node1 ~]</code><code>#sed '2a hello \ //在檔案的第二行後插入兩行内容</code>
<code>> how are your?'</code><code>passwd</code>
<code>[root@node1 ~]</code><code>#sed '3c$$$'passwd //把檔案第三行替換成“$$$”</code>
<code>[root@node1 ~]</code><code># sed '$c####' passwd //把檔案的最後一行替換成####</code>
<code>[root@node1 ~]</code><code>#nl /etc/passwd | sed '2ahello' //nl顯示行号,而空行的行号則不顯示</code>
<code>注意:</code>
<code>sed</code> <code>的-i選項可以直接修改檔案中的内容</code>
<code>[root@node1 ~]</code><code>#sed -i 's/root/rm/' passwd</code>
<code>複制粘貼</code>
<code>把檔案的第二行到第四行複制到檔案的末尾</code>
<code>[root@node1 ~]</code><code>#sed '2,4H;$G'passwd > b.txt</code>
<code>h</code><code>/H</code> <code>複制拷貝/追加模式空間(緩沖區)到存放空間</code>
<code>g</code><code>/G</code> <code>粘貼從存放空間取回/追加到模式空間</code>
<code>删除空行 d 删除 ***</code>
<code>[root@node1 ~]</code><code>#sed '/^$/d' passwd > c.txt //删除空行顯示</code>
<code>[root@node1 ~]</code><code>#sed '/xfs/w newfstab' /etc/fstab //把fstab中包含xfs的記錄(行)寫入新的檔案中</code>
[root@node1 ~]# cat a.txt | sed 's/#.*$//g' | sed '/^$/d' //删除檔案的注釋部分并删除空行。
[root@node1 ~]# cat a.txt | sed 's/^[ ]* //g' //删除行首空格
[root@node1 ~]# sed -i "s/lhl/$var1/g" a.txt //變量替換
[root@node1 ~]# cat a.txt
###:x:0:0:###:/###:/bin/bash
awk的應用
(1)列印出eth0ip位址。
[root@node1 ~]# ifconfig eth0 | grep "inet addr:" | awk -F [:" "]+ '{print $4}'
192.168.213.100
或:
[root@node1 ~]# ifconfig eth0 | grep "inet addr:" | awk -F: '{print $2}' | awk '{print $1}'
awk的預設分隔符是空格,預設輸出是$0,一整行内容。
awk '/root/{print}' passwd 或awk '/root/' passwd從 //列印出檔案passwd中包含root的行
awk 'length$0>40 {print NR $0}' nginxd,sh //顯示檔案nginxd.sh内容中超過40個字元的行号。其中NR作為awk的内置标志符不需要使用$符号,而如果引用自定義變量隻需要$符号,且自定義變量需要與awk保留字不同。
awk '/[Rr]estart/,/[Rr]eload/ {print $1}' nginxd,sh //顯示檔案nginxd.sh中包含Restart/restart和Reload/reload的行中第一個字元。
解釋:awk -F [:" "]表示awk 以空格作為分隔符,然後列印出第4列參數
本文轉自 CARYFLASH 51CTO部落格,原文連結:http://blog.51cto.com/maoxiaoxiong/1841666