国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Linux > 正文

Shell正則表達式之grep、sed、awk實操筆記

2019-10-26 18:42:01
字體:
來源:轉載
供稿:網友

最近一直在研究shell腳本這塊,剛好閑下來整了下自己手頭上比較好的資料中的一些范例,以下是我整理的鳥哥私房菜里面正則表達式里面比較基礎的一些語法詳解,適合新手查閱。

首先先復制一段范例:
代碼如下:
# vi regular_express.txt
-------------------------------
"Open Source" is a good mechanism to develop programs.
apple is my favorite food.
Football game is not use feet only.
this dress doesn't fit me.
However, this dress is about $ 3183 dollars.
GNU is free air not free beer.
Her hair is very beauty.
I can't finish the test.
Oh! The soup taste good.
motorcycle is cheap than car.
This window is clear.
the symbol '*' is represented as start.
Oh!My god!
The gd software is a library for drafting programs.
You are the best is mean you are the no. 1.
The world <Happy> is the same with "glad".
I like dog.
google is the best tools for search keyword.
goooooogle yes!
go! go! Let's go.
# I am VBird
--------------------------------

設置語系為C
代碼如下:#export LANG=C

grep

1.搜尋特定字符串"the"
注: n為顯示行號
代碼如下:# grep -n 'the' regular_express.txt

2.反向搜尋特定字符串"the"
代碼如下:# grep -vn 'the' regular_express.txt

3.取得任意大小寫"the"的這個字符串
代碼如下:# grep -in 'the' regular_express.txt

4.利用括號 [] 來搜尋集合字符
搜索test或taste這兩個單詞時,發現他們有共同的't?st',所以可以這么搜尋
代碼如下:# grep -n 't[ae]st' regular_express.txt
這樣其實就是在找t[a]st和t[e]st這兩個分開的字符
如果搜索有 oo 的字符時,則可以使用:
代碼如下:# grep -n 'oo' regular_express.txt
如果搜索oo時不想搜到 oo 前面有 g 的話,我們可以利用反向選擇[^]來達成:
代碼如下:# grep -n '[^g]oo'  regular_express.txt
如果搜索oo前面不想有小寫字符,則:
代碼如下:# grep -n '[^a-z]oo' regular_express.txt
注: 大寫英文/小寫英文/數字 可以使用 [a-z]/[A-Z]/[0-9]等方式來書寫,也可以寫在一起
[a-zA-Z0-9]表示要求字符串是數字以及英文
如果我們要取得有數字的那行,則:
代碼如下:# grep -n '[0-9]' regular_express.txt
注:但考慮到語系對編碼順序的影響,因此除了連續編碼使用減號[-]外,也可以用[:lower:]代替a-z 以及 [:digit:] 代替0-9 使用
代碼如下:# grep -n '[^[:lower:]]oo' regular_express.txt
# grep -n '[[:digit:]]' regular_express.txt

5.顯示行首為'the'的字符串
代碼如下:# grep -n '^the' regular_express.txt
顯示行首是小寫字符
代碼如下:# grep -n '^[a-z]' regular_express.txt

6.顯示行尾為點 . 的那一行

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 苍溪县| 宜章县| 台东县| 湘潭县| 凤阳县| 原平市| 博罗县| 正镶白旗| 庆云县| 资兴市| 修武县| 汶上县| 民县| 通许县| 绍兴市| 土默特左旗| 蓝田县| 左贡县| 河北区| 邯郸县| 略阳县| 海安县| 璧山县| 广西| 通河县| 永胜县| 金堂县| 平果县| 长顺县| 措勤县| 正镶白旗| 乌什县| 仁怀市| 宜都市| 会泽县| 五河县| 南汇区| 凤凰县| 新丰县| 辉县市| 大洼县|