shell Regex和grep awk find sed sort uniq cut
一,grep
1,基礎參數
grep-[acinv]'搜尋內容串'filename
-a以文字檔方式搜尋
-c計算找到的符合行的次數
-i忽略大小寫
-n順便輸出行號
-v反向選擇,即找沒有搜尋字串的行
[root@test3 ~]# cat Travel.docTravel is a good way to refresh and broaden our horizon.During your travel, you can turn off your cellphone and keep far from the Internet.You can forget your work, your study or your family, and just enjoy the leisure time. From the moment when you start your journey, all the trifles should be locked at your house.During the travel, you can kill your time slowly to have a good rest, and you can contact with different people.They may be a window for you to know a different world.Meet various people, experience things, which make your world bigger.You just clean up your entire mind and you can get the true meaning of travel.找出有The的行,並列印行號[root@test3 ~]# grep -n "The" Travel.doc5:They may be a window for you to know a different world.搜尋沒有The的行[root@test3 ~]# grep -vn "The" Travel.doc1:Travel is a good way to refresh and broaden our horizon.2:During your travel, you can turn off your cellphone and keep far from the Internet.3:You can forget your work, your study or your family, and just enjoy the leisure time. From the moment when you start your journey, all the trifles should be locked at your house.4:During the travel, you can kill your time slowly to have a good rest, and you can contact with different people.6:Meet various people, experience things, which make your world bigger.7:You just clean up your entire mind and you can get the true meaning of travel.計算含有travel單詞的行數,不區分大小寫[root@test3 ~]# grep -nic "travel" Travel.doc4
2利用[]搜尋集合字元
[]表示其中的某一個字元,例如[ade]表示a或d或e
找出ting hing ning
[root@test3 ~]# grep -ni "[thn]ing" Travel.doc
6:Meet various people, experience things, which make your world bigger.
7:You just clean up your entire mind and you can get the true meaning of travel.
用^符號做[]內的首碼,表示除[]內的字元之外的字元。
比如搜尋oo前沒有g的字串所在的行.使用'[^g]oo'作搜尋字串
[root@test3 ~]# grep -ni "[^g]oo" Travel.doc
8:foo
[]內可以用範圍表示,比如[a-z]表示小寫字母,[0-9]表示0~9的數字,[A-Z]則是大寫字母們。[a-zA-Z0-9]表示所有數字與英文字元。當然也可以配合^來排除字元。
[root@test3 ~]# grep -ni "[1-9]" Travel.doc
9:2020 12 31
3,行首與行尾字元^$
^表示行的開頭,$表示行的結尾(不是字元,是位置)那麼‘^$'就表示空行,因為只有
行首和行尾。
這裡^與[]裡面使用的^意義不同。它表示^後面的串是在行的開頭。
比如搜尋Travel在開頭的行
[root@test3 ~]# grep -ni "^Travel" Travel.doc
1:Travel is a good way to refresh and broaden our horizon.
4,搜尋以小寫字母開頭的行
[root@test3 ~]# grep -n "^[a-z]" Travel.doc
8:foot
5,搜尋開頭不是英文字母的行
[root@test3 ~]# grep -n "^[^a-zA-Z]" Travel.doc
9:2020 12 31
6,$表示它前面的串是在行的結尾,
比如'\.'表示.在一行的結尾
搜尋末尾是.的行
[root@test3 ~]# grep -n "\.$" Travel.doc1:Travel is a good way to refresh and broaden our horizon.2:During your travel, you can turn off your cellphone and keep far from the Internet.3:You can forget your work, your study or your family, and just enjoy the leisure time.4:From the moment when you start your journey, all the trifles should be locked at your house.5:During the travel, you can kill your time slowly to have a good rest, and you can contact with different people.6:They may be a window for you to know a different world.7:Meet various people, experience things, which make your world bigger.8:You just clean up your entire mind and you can get the true meaning of travel.
7,任意一個字元.與重複字元*
在bash中*代表萬用字元,用來代表任意個字元,但是在Regex中,他含義不同,*表示有0個或多個某個字元。
例如oo*,表示第一個o一定存在,第二個o可以有一個或多個,也可以沒有,因此代表至少一個o.
點.代表一個任一字元,必須存在。
g??d可以用'g..d'表示。good,gxxd,gabd.....都符合。
a,搜尋兩個o以上的字串
[root@test3 ~]# cat a.shgoodfootvnhsdooos[root@test3 ~]# grep -n "ooo*" a.sh1:good2:foot3:vnhsdooos
b,搜尋g開頭和結尾的字串在的行.*表示0個或多個任一字元
[root@test3 ~]# cat b.sh"Open Source" is a good mechanism to develop programs.The gd software is a library for drafting programs.google is the best tools for search keyword.goooooogle yes!go! go! Let's go.[root@test3 ~]# grep -n 'g.*g' b.sh1:"Open Source" is a good mechanism to develop programs.2:The gd software is a library for drafting programs.3:google is the best tools for search keyword.4:goooooogle yes!5:go! go! Let's go.
8,限定連續重複字元的範圍{}
.*只能限制0個或多個,如果要確切的限制字元重複數量,就用{範圍}。範圍是數字用,隔開2,5表示2~5個,
2表示2個,2,表示2到更多個
注意,由於{}在SHELL中有特殊意義,因此作為Regex用的時候要用\轉義一下。
搜尋包含兩個o的字串的行。
woody@xiaoc:~/tmp$grep-n'o\{2\}'regular_express.txt
1:"OpenSource"isagoodmechanismtodevelopprograms.
2:appleismyfavoritefood.
3:Footballgameisnotusefeetonly.
9:Oh!thesouptastegood!
18:googleisthebesttoolsforsearchkeyword.
19:goooooogleyes!