<em id="0a85b"><option id="0a85b"></option></em>

<abbr id="0a85b"></abbr>

      <nobr id="0a85b"></nobr>
        <tr id="0a85b"></tr>
        9久久伊人精品综合,亚洲一区精品视频在线,成 人免费va视频,国产一区二区三区黄网,99国产精品永久免费视频,亚洲毛片多多影院,精品久久久无码人妻中文字幕,无码国产欧美一区二区三区不卡
        學(xué)習(xí)啦>學(xué)習(xí)電腦>操作系統(tǒng)>Linux教程>

        grep用法詳解grep與正則表達(dá)式

        時(shí)間: 志藝942 分享

          正則表達(dá)式只是一種表示法,只要工具支持這種表示法, 那么該工具就可以處理正則表達(dá)式的字符串。vim、grep、awk 、sed 都支持正則表達(dá)式,也正是因?yàn)橛捎谒鼈冎С终齽t,才顯得它們強(qiáng)大。接下來(lái)是小編為大家收集的grep用法詳解,希望能幫到大家。

          grep用法詳解

          1基礎(chǔ)正則表達(dá)式

          grep 工具,以前介紹過(guò)。

          grep -[acinv] '搜索內(nèi)容串' filename

          -a 以文本文件方式搜索

          -c 計(jì)算找到的符合行的次數(shù)

          -i 忽略大小寫

          -n 順便輸出行號(hào)

          -v 反向選擇,即找 沒有搜索字符串的行

          其中搜索串可以是正則表達(dá)式!

          1

          搜索有the的行,并輸出行號(hào)

          $grep -n 'the' regular_express.txt

          搜 索沒有the的行,并輸出行號(hào)

          $grep -nv 'the' regular_express.txt

          2 利 用[]搜索集合字符

          [] 表示其中的某一個(gè)字符 ,例如[ade] 表示a或d或e

          woody@xiaoc:~/tmp$ grep -n 't[ae]st' regular_express.txt

          8:I can't finish the test.

          9:Oh! the soup taste good!

          可以用^符號(hào)做[]內(nèi)的前綴,表示除[]內(nèi)的字符之外的字 符。

          比如搜索oo前沒有g(shù)的字符串所在的行. 使用 '[^g]oo' 作搜索字符串

          woody@xiaoc:~/tmp$ grep -n '[^g]oo' regular_express.txt

          2:apple is my favorite food.

          3:Football game is not use feet only.

          18:google is the best tools for search keyword.

          19:goooooogle yes!

          [] 內(nèi)可以用范圍表示,比如[a-z] 表示小寫字母,[0-9] 表示0~9的數(shù)字, [A-Z] 則是大寫字母?jìng)儭a-zA-Z0-9]表示所有數(shù)字與英文字符。 當(dāng)然也可以配合^來(lái)排除字符。

          搜索包含數(shù)字的行

          woody@xiaoc:~/tmp$ grep -n '[0-9]' regular_express.txt

          5:However ,this dress is about $ 3183 dollars.

          15:You are the best is menu you are the no.1.

          行首與行尾字符 ^ $. ^ 表示行的開頭,$表示行的結(jié)尾( 不是字符,是位置)那么‘^$' 就表示空行,因?yàn)橹挥?/p>

          行首和行尾。

          這里^與[]里面使用的^意義不同。它表示^后面的串是在行的開頭。

          比如搜索the在開頭的行

          woody@xiaoc:~/tmp$ grep -n '^the' regular_express.txt

          12:the symbol '*' is represented as star.

          搜索以小寫字母開頭的行

          woody@xiaoc:~/tmp$ grep -n '^[a-z]' regular_express.txt

          2:apple is my favorite food.

          4:this dress doesn't fit me.

          10:motorcycle is cheap than car.

          12:the symbol '*' is represented as star.

          18:google is the best tools for search keyword.

          19:goooooogle yes!

          20:go! go! Let's go.

          woody@xiaoc:~/tmp$

          搜索開頭不是英文字母的行

          woody@xiaoc:~/tmp$ grep -n '^[^a-zA-Z]' regular_express.txt

          1:"Open Source" is a good mechanism to develop programs.

          21:#I am VBird

          woody@xiaoc:~/tmp$

          $表示它前面的串是在行的結(jié)尾,比如 '\.' 表示 . 在一行的結(jié)尾

          搜索末尾是.的行

          woody@xiaoc:~/tmp$ grep -n '\.$' regular_express.txt //. 是正則表達(dá)式的特殊符號(hào),所以要用\轉(zhuǎn)義

          1:"Open Source" is a good mechanism to develop programs.

          2:apple is my favorite food.

          3:Football game is not use feet only.

          4:this dress doesn't fit me.

          5:However ,this dress is about $ 3183 dollars.

          6:GNU is free air not free beer.

          .....

          注意在MS的系統(tǒng)下生成的文本文件,換行會(huì)加上一個(gè) ^M 字符。所以最后的字符會(huì)是隱藏的^M ,在處理Windows

          下面的文本時(shí)要特別注意!

          可以用cat dos_file | tr -d '\r' > unix_file 來(lái)刪除^M符號(hào)。 ^M==\r

          那么'^$' 就表示只有行首行尾的空行拉!

          搜索空行

          woody@xiaoc:~/tmp$ grep -n '^$' regular_express.txt

          22:

          23:

          woody@xiaoc:~/tmp$

          搜索非空行

          woody@xiaoc:~/tmp$ grep -vn '^$' regular_express.txt

          1:"Open Source" is a good mechanism to develop programs.

          2:apple is my favorite food.

          3:Football game is not use feet only.

          4:this dress doesn't fit me.

          ..........

          任意一個(gè)字符. 與重復(fù)字符 *

          在bash中*代表通配符,用來(lái)代表任意個(gè) 字符,但是在正則表達(dá)式中,他含義不同,*表示有0個(gè)或多個(gè) 某個(gè)字符。

          例如 oo*, 表示第一個(gè)o一定存在,第二個(gè)o可以有一個(gè)或多個(gè),也可以沒有,因此代表至少一個(gè)o.

          點(diǎn). 代表一個(gè)任意字符,必須存在。 g??d 可以用 'g..d' 表示。 good ,gxxd ,gabd .....都符合。

          woody@xiaoc:~/tmp$ grep -n 'g..d' regular_express.txt

          1:"Open Source" is a good mechanism to develop programs.

          9:Oh! the soup taste good!

          16:The world is the same with 'glad'.

          woody@xiaoc:~/tmp$

          搜索兩個(gè)o以上的字符串

          woody@xiaoc:~/tmp$ grep -n 'ooo*' regular_express.txt //前兩個(gè)o一定存在,第三個(gè)o可沒有,也可有多個(gè)。

          1:"Open Source" is a good mechanism to develop programs.

          2:apple is my favorite food.

          3:Football game is not use feet only.

          9:Oh! the soup taste good!

          18:google is the best tools for search keyword.

          19:goooooogle yes!

          搜索g開頭和結(jié)尾,中間是至少一個(gè)o的字符串,即gog, goog....gooog...等

          woody@xiaoc:~/tmp$ grep -n 'goo*g' regular_express.txt

          18:google is the best tools for search keyword.

          19:goooooogle yes!

          搜索g開頭和結(jié)尾的字符串在的行

          woody@xiaoc:~/tmp$ grep -n 'g.*g' regular_express.txt // .*表示 0個(gè)或多個(gè)任意字符

          1:"Open Source" is a good mechanism to develop programs.

          14:The gd software is a library for drafting programs.

          18:google is the best tools for search keyword.

          19:goooooogle yes!

          20:go! go! Let's go.

          限定連續(xù)重復(fù)字符的范圍 { }

          . * 只能限制0個(gè)或多個(gè), 如果要確切的限制字符重復(fù)數(shù)量,就用{范圍} 。范圍是數(shù)字用,隔開 2,5 表示2~5個(gè),

          2表示2個(gè),2, 表示2到更多個(gè)

          注意,由于{ }在SHELL中有特殊意義,因此作為正則表達(dá)式用的時(shí)候要用\轉(zhuǎn)義一下。

          搜索包含兩個(gè)o的字符串的行。

          woody@xiaoc:~/tmp$ grep -n 'o\{2\}' regular_express.txt

          1:"Open Source" is a good mechanism to develop programs.

          2:apple is my favorite food.

          3:Football game is not use feet only.

          9:Oh! the soup taste good!

          18:google is the best tools for search keyword.

          19:goooooogle yes!

          搜索g后面跟2~5個(gè)o,后面再跟一個(gè)g的字符串的行。

          woody@xiaoc:~/tmp$ grep -n 'go\{2,5\}g' regular_express.txt

          18:google is the best tools for search keyword.

          搜索包含g后面跟2個(gè)以上o,后面再跟g的行。。

          woody@xiaoc:~/tmp$ grep -n 'go\{2,\}g' regular_express.txt

          18:google is the best tools for search keyword.

          19:goooooogle yes!

          注意,相讓[]中的^ - 不表現(xiàn)特殊意義,可以放在[]里面內(nèi)容的后面。

          '[^a-z\.!^ -]' 表示沒有小寫字母,沒有. 沒有!, 沒有空格,沒有- 的 串,注意[]里面有個(gè)小空格。

          另外shell 里面的反向選擇為[!range], 正則里面是 [^range]

          2擴(kuò)展正則表達(dá)式

          擴(kuò)展正則表達(dá)式是對(duì)基礎(chǔ)正則表達(dá)式添加了幾個(gè)特殊構(gòu)成的。

          它令某些操作更加方便。

          比如我們要去除 空白行和行首為 #的行, 會(huì)這樣用:

          woody@xiaoc:~/tmp$ grep -v '^$' regular_express.txt | grep -v '^#'

          "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.

          ............

          然而使用支持?jǐn)U展正則表達(dá)式的 egrep 與擴(kuò)展特殊符號(hào) | ,會(huì)方便許多。

          注意grep只支持基礎(chǔ)表達(dá)式, 而egrep 支持?jǐn)U展的, 其實(shí) egrep 是 grep -E 的別名而已。因此grep -E 支持?jǐn)U展正則。

          那么:

          woody@xiaoc:~/tmp$ egrep -v '^$|^#' 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.

          ....................

          這里| 表示或的關(guān)系。 即滿足 ^$ 或者 ^# 的字符串。

          這里列出幾個(gè)擴(kuò)展特殊符號(hào):

          +, 于 . * 作用類似,表示 一個(gè)或多個(gè)重復(fù)字符。

          ?, 于 . * 作用類似,表示0個(gè)或一個(gè)字符。

          |,表示或關(guān)系,比如 'gd|good|dog' 表示有g(shù)d,good或dog的串

          (),將部分內(nèi)容合成一個(gè)單元組。 比如 要搜索 glad 或 good 可以這樣 'g(la|oo)d'

          ()的好處是可以對(duì)小組使用 + ? * 等。

          比如要搜索A和C開頭結(jié)尾,中間有至少一個(gè)(xyz) 的串,可以這樣 : 'A(xyz)+C'


        看了“grep用法詳解”還想看:

        1.linux grep正則表達(dá)式與grep用法詳解

        2.全面解析Linux的grep命令中正則表達(dá)式的用法

        3.Linux系統(tǒng)grep正則表達(dá)式怎么用

        4.Linux下如何使用grep命令搜索多個(gè)單詞

        2836398 主站蜘蛛池模板: 亚洲精品成人无限看| 久久精品国产无限资源| 久久不卡精品| 国产av不卡一区二区| 亚洲岛国av一区二区| 天堂www在线中文| 国产99在线 | 亚洲| 99爱视频精品免视看| 亚洲永久精品日韩成人av| 中文字幕一区二区三区精彩视频| 嫩草成人AV影院在线观看| 国产精品呻吟一区二区三区 | 国产影片AV级毛片特别刺激| 久久香蕉国产线看观看式| 成人aⅴ综合视频国产| 亚洲中文字幕无码专区| 精品国产免费第一区二区三区日韩 | 看免费的无码区特aa毛片| 亚洲日韩欧美丝袜另类自拍| 亚洲 欧洲 无码 在线观看 | 亚洲成人动漫av在线| 国产精品成| 国产初高中生粉嫩无套第一次| 中国熟妇毛多多裸交视频| 国产成年无码aⅴ片在线观看| 91高清免费国产自产拍| 国语精品国内自产视频| 亚洲日韩精品无码一区二区三区| 国产精品日日摸夜夜添夜夜添无码| 国产午夜一区二区在线观看| 1区2区3区4区产品不卡码网站| 亚洲另类无码专区国内精品| 国产目拍亚洲精品一区二区| 亚洲中文字幕久久精品码| h动态图男女啪啪27报gif| 日韩欧美视频一区二区三区| 日韩人妻中文字幕精品| 一本久久a久久免费精品不卡| 国内久久人妻风流av免费| 好男人视频在线播放| 天天碰天天狠天天透澡|