1. 程式人生 > >grep/sed、正則表示式略解

grep/sed、正則表示式略解

1、grep

 grep [-acinv] [--color=auto] '搜尋字串' filename
選項與引數:
-a :將 binary 檔案以 text 檔案的方式搜尋資料
-c :計算找到 '搜尋字串' 的次數 #加上c之後,命令返回次數(數字)
-i :忽略大小寫
-n :輸出行號
-v :反向選擇,亦即顯示出沒有 '搜尋字串' 內容的那一行!
--color=auto :可以將找到的關鍵詞部分加上顏色的顯示喔!
//在當前目錄搜尋帶'test'行的檔案
grep ‘test’ *   
//在當前目錄及其子目錄下搜尋'test'行的檔案
grep -r ‘test’ * 
//
在當前目錄及其子目錄下搜尋'test'行的檔案,但是不顯示匹配的行,只顯示匹配的檔案 grep -l -r ‘test’ *
. (小數點):代表『一定有一個任意位元組』。
* (星號):代表『重複前一個字元, 0 到無窮多次』。
^ :在字元類符號(括號[])之內與之外是不同的! 在 [] 內代表『反向選擇』,在 [] 之外則代表定位在行首的意義!
$ :代表定位在行尾。

故:

 grep -n '^$' test.txt #代表找出空白行

……

2、sed命令
與grep一樣,sed也支援特殊元字元,來進行模式查詢、替換。不同的是,sed使用的正則表示式是括在斜槓線”/”之間的模式。
預設情況下,sed把輸入行列印在螢幕上,選項-n用於取消預設的列印操作。

替換操作:s命令

-n選項和-p命令一起使用表示只打印那些發生替換的行:

root@ubuntu:~# ifconfig |sed -n 's/inet/hello/p' 
          hello addr:192.168.2.195  Bcast:192.168.2.255  Mask:255.255.255.0
          hello6 addr: fe80::20c:29ff:fe66:996/64 Scope:Link
          hello addr:127.0.0.1  Mask:255.0.0.0
          hello6 addr: ::1/128 Scope
:Host root@ubuntu:~#

替換inet為hello。其中後面的‘p’命令一定要加上,否則無列印輸出。

延伸一下:
使用grep命令定位到行,然後使用sed命令進行替換修改。

//定位到帶有“inet”的行
root@ubuntu:~# ifconfig |grep 'inet'                                                 
          inet addr:192.168.2.195  Bcast:192.168.2.255  Mask:255.255.255.0                               
          inet6 addr: fe80::20c:29ff:fe66:996/64 Scope:Link                                              
          inet addr:127.0.0.1  Mask:255.0.0.0                                                            
          inet6 addr: ::1/128 Scope:Host  
//定位到帶有“inet”的行 |  把“inet.addr:” 替換為“ ”(空格)                                                          
root@ubuntu:~# ifconfig |grep 'inet'|sed -n 's/inet.addr:/ /p'
           192.168.2.195  Bcast:192.168.2.255  Mask:255.255.255.0
           127.0.0.1  Mask:255.0.0.0
//定位到帶有“inet”的行 | 把“inet.addr:” 替換為“ ”(空格)| 把“ ”(空格)替換為空
//(不能直接把  “inet.addr:” 替換為空)(此處不知為什麼)        
root@ubuntu:~# ifconfig |grep 'inet'|sed -n 's/inet.addr:/ /p'|sed -n 's/  *//p'     
192.168.2.195  Bcast:192.168.2.255  Mask:255.255.255.0
127.0.0.1  Mask:255.0.0.0
//定位到帶有“inet”的行 | 把“inet.addr:” 替換為“ ”(空格)| 把“ ”(空格)替換為空 | 替換“大寫字母開頭到一行結束”的內容為“ ”(空格)
root@ubuntu:~# ifconfig |grep 'inet'|sed -n 's/inet.addr:/ /p'|sed -n 's/  *//p'|sed 's/[B-M]..*//'
192.168.2.195  
127.0.0.1  
root@ubuntu:~# 
//新增的最後一條中,沒有新增p命令。如果新增的話,會列印兩次ip地址。
root@ubuntu:~# ifconfig |grep 'inet'|sed -n 's/inet.addr:/ /p'|sed -n 's/  *//p'|sed 's/[B-M]..*//p'
192.168.2.195  
192.168.2.195  
127.0.0.1  
127.0.0.1  
root@ubuntu:~#

這種靠替換的方法獲取ip很麻煩,還有其他簡便獲取ip的方法。

刪除操作:d命令

//刪除空白行
ifconfig | sed '/^$/d'
//刪除第2行
ifconfig | sed '2d' 
//刪除檔案的第2行到末尾所有行
ifconfig | sed '2,$d' 
//刪除最後一行
ifconfig | sed '$d'
//刪除最後兩行
ifconfig | sed 'N;$!P;$!D;$d'
//刪除最後三行
ifconfig | sed -n '1{N;N;};:a;N;P;D;t a'
//刪除最後四行  //其他依次類推,增加N的數量就行。
ifconfig | sed -n '1{N;N;N;};:a;N;P;D;t a' 

sed命令在檔案某行前後新增內容的用法

//向test.c檔案的nameserver 127.0.0.1行之後新增:nameserver 8.8.8.8行
sed -i '/nameserver 127.0.0.1/a\nameserver 8.8.8.8' test.c
//向test.c檔案的nameserver 127.0.0.1行之前新增:nameserver 192.168.2.1行
sed -i '/nameserver 127.0.0.1/i\nameserver 192.168.2.1' test.c

測試過程:

root@ubuntu:~/adams# cat test.c 
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
#     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
nameserver 127.0.0.1
root@ubuntu:~/adams# sed -i '/nameserver 127.0.0.1/a\nameserver 8.8.8.8' test.c
root@ubuntu:~/adams# cat test.c                                                      
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
#     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
nameserver 127.0.0.1
nameserver 8.8.8.8
root@ubuntu:~/adams# sed -i '/nameserver 127.0.0.1/i\nameserver 192.168.2.1' test.c
root@ubuntu:~/adams# cat test.c                                                      
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
#     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
nameserver 192.168.2.1
nameserver 127.0.0.1
nameserver 8.8.8.8
root@ubuntu:~/adams# 

參考連結:
http://www.cnblogs.com/edwardlost/archive/2010/09/17/1829145.html
http://www.cnblogs.com/ggjucheng/archive/2013/01/13/2856896.html
http://man.linuxde.net/sed