Regex和grep、sed工具

來源:互聯網
上載者:User

標籤:Regex   grep   sed   

what is Regex

     一種處理字元的方法,只要命令工具(例:grep、sed、awk等)支援這種方法,就可以用來處理Regex字串。通過特殊字元的協助,我們就容易達到尋找、刪除、替換特定字串的命令程式。

用途

     資訊過濾,資訊匹配,擷取有用資訊

常用的運算式含義

  • ^ 行首標記  #^haha,匹配以haha起始的行

  • $ 行尾標記

  • . 匹配任意單個字元

  • ? 匹配之前的項一次或0次  #blu?e 匹配blue或ble

  • + 匹配之前的項一次或多次

  • * 匹配之前的項0次或多次

  • [] 匹配包含在[]中的任意一個字元  #a[bc]d 匹配abd或acd

  • [^] 匹配除了[^]以外的任意一個字元  #a[^bc] 匹配ad、ae...

  • [-] 匹配[-]一段字元中的任意一個字元  #[1-9] 匹配1-9任意一個字元

  • () 建立一個用於匹配的子串  #ab(cde)?匹配ab或abcde

  • {n} 匹配之前的項n次  #[1-9]{3}相當於[1-9][1-9][1-9]

  • {n,} 之前的項至少匹配n次

  • {n,m} 之前的項匹配n-m次

  • | 匹配兩邊的任意一項  #(a | b) 匹配a或b

  • \ 轉義

  • [:alnum:] 大小寫英文和數字

  • [:alpha:] 大小寫英文

  • [:digit:] 數字

  • [:lower:] 小寫英文

  • [:upper:] 大寫英文

  • [:blank:] TAB或空格

  • [:punct:] 標點符號

grep

grep [-n 行號] [-v 反選] [-A 輸出後幾行] [-B 輸出前幾行] [-C 輸出前後幾行] [-i 忽略大小寫] [-o 列印匹配到的而不是整行] [-b 匹配到的字元位移] [-l 搜尋匹配文本在那個檔案中] [-L 和-l相反] [-e 匹配多個] [-r或-R 遞迴搜尋,可指定include和exclude] [-q 靜默輸出成功返回0] [--color=auto 顏色高亮]

  • 對於擴充Regex  + ?| () 需要使用grep -E或egrep使用

  • 例:截取dmesg的eth0資訊,高亮顯示,並顯示前兩行後三行,顯示行號

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/6E/C8/wKiom1WHfnmSNAwHAAGiMzd6dNc671.jpg" style="float:none;" title="2015-06-22_10-53-55.png" alt="wKiom1WHfnmSNAwHAAGiMzd6dNc671.jpg" />

  • 參數-o

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/6E/C5/wKioL1WHgDDjrSfqAAFx37DsG7Y029.jpg" title="Image.png" style="float:none;" alt="wKioL1WHgDDjrSfqAAFx37DsG7Y029.jpg" />

  • 參數-b

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/6E/C8/wKiom1WHfnnjx9f-AAB_5lR8X54885.jpg" style="float:none;" title="Image2.png" alt="wKiom1WHfnnjx9f-AAB_5lR8X54885.jpg" />

  • 參數-l和L

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/6E/C5/wKioL1WHgDCgi094AAB_G4973cc998.jpg" style="float:none;" title="Image3.png" alt="wKioL1WHgDCgi094AAB_G4973cc998.jpg" />

  • 參數-e

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/6E/C9/wKiom1WHfnqhfzLUAABWraLUgxs389.jpg" style="float:none;" title="Image6.png" alt="wKiom1WHfnqhfzLUAABWraLUgxs389.jpg" />

  • 參數-r和-R

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/6E/C9/wKiom1WHfnniIbJpAACj3w02MH0980.jpg" title="Image4.png" style="float:none;" alt="wKiom1WHfnniIbJpAACj3w02MH0980.jpg" />

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/6E/C5/wKioL1WHgDDzLNW6AABPpI1W9dM744.jpg" style="float:none;" title="Image5.png" alt="wKioL1WHgDDzLNW6AABPpI1W9dM744.jpg" />


PS:有時候因為語系的不同,用到類似[a-z]這樣的運算式的時候會有錯誤,編碼順序不同導致的例如zh_CN.big5和C兩種語系

  • LANG=C時:01234....ABCD..YZabcd....xyz

  • LANG=zh_CN :01234....aAbBcC....zZ

可用[:lower:]代替

sed:流編輯器 stream editor

     可以對資料進行:插入、刪除、替換並列印到螢幕上,或進行直接寫入,還可以進行部分資料尋找替換、已匹配字串標記和子串匹配標記

sed [-i 直接寫入] [-e 連續操作] [-n 靜默模式] ‘ 操作內容 ’

1:新增a,i   刪除d   替換c   顯示p,-n              //(根據行號)

  • [[email protected] ~]# nl /etc/passwd | sed ‘2a hello‘    // 第二行下面增加

     1     root:x:0:0:root:/root:/bin/bash
     2     bin:x:1:1:bin:/bin:/sbin/nologin
hello
     3     daemon:x:2:2:daemon:/sbin:/sbin/nologin

  • [[email protected] ~]# nl /etc/passwd | sed ‘2i hello‘    // 第二行上面增加

     1     root:x:0:0:root:/root:/bin/bash
hello
     2     bin:x:1:1:bin:/bin:/sbin/nologin

  • d

  • [[email protected] ~]# nl /etc/passwd | sed ‘2,4d‘    // 刪 2-4行   

     1     root:x:0:0:root:/root:/bin/bash
     5     lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

  • [[email protected] ~]# nl /etc/passwd | sed ‘2,5c hello‘           // 第2-5 行換掉

     1     root:x:0:0:root:/root:/bin/bash
hello
     6     sync:x:5:0:sync:/sbin:/bin/sync

  • [[email protected] ~]# nl /etc/passwd | sed -n ‘2,4p‘               // 顯示 2-4 行 ,-n(安靜模式)不加輸出差別很大

     2     bin:x:1:1:bin:/bin:/sbin/nologin
     3     daemon:x:2:2:daemon:/sbin:/sbin/nologin
     4     adm:x:3:4:adm:/var/adm:/sbin/nologin

2:部分資料尋找替換

     sed ‘s/要替換欄位/新欄位/g’   # ‘s///ng‘ 每行第n個開始才進行替換

     例如:截取eth0的ip地址

  • [[email protected] ~]# /sbin/ifconfig eth0 | grep ‘inet addr‘ | sed ‘s/^.*addr://g‘ | sed ‘s/Bcast.*$//g‘
    192.168.159.131

     和Regex結合

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/6E/C5/wKioL1WHrDqxJhJvAABWhGXl8cw859.jpg" style="float:none;" title="Image.png" alt="wKioL1WHrDqxJhJvAABWhGXl8cw859.jpg" />

3:已匹配字串標記&

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/6E/C9/wKiom1WHqoTS8rLEAACW87fy8a0089.jpg" style="float:none;" title="Image2.png" alt="wKiom1WHqoTS8rLEAACW87fy8a0089.jpg" />

4:字串匹配標記\1\2

     格式:‘s/() ()/\1 \2‘

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/6E/C5/wKioL1WHrDvCIOiXAABZYJ-sjco421.jpg" style="float:none;" title="Image3.png" alt="wKioL1WHrDvCIOiXAABZYJ-sjco421.jpg" />


本文出自 “Call me Boxin” 部落格,請務必保留此出處http://boxinknown.blog.51cto.com/10435935/1664098

Regex和grep、sed工具

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.