Linux中grepRegex相關講解,grepRegex

來源:互聯網
上載者:User

Linux中grepRegex相關講解,grepRegex

萬用字元

* 任一字元,可重複多次

任一字元,重複一次

[] 代表一個字元

舉例: [a,b,c] 表示abc中任意一個

萬用字元的作用是用來匹配檔案名稱的

Regex

Regex是在檔案中匹配合格字串的

ls find cp是不支援Regex的

但是grep awk sed支援Regex

[root@Hadoop-bigdata01 test]# touch aa

[root@hadoop-bigdata01 test]# touch aab aabb

[root@hadoop-bigdata01 test]# ll

total 0

-rw-r--r-- 1 root root 0 May 16 19:47 aa

-rw-r--r-- 1 root root 0 May 16 19:47 aab

-rw-r--r-- 1 root root 0 May 16 19:47 aabb

[root@hadoop-bigdata01 test]# ls aa

aa

[root@hadoop-bigdata01 test]# ls aa

aab

[root@hadoop-bigdata01 test]# ls aa*

aa aab aabb

Regex特殊字元

Regex匹配範圍

使用Regex

grep "1" /etc/passwd

包含關鍵字1的行,grep只要包含就行,不想萬用字元,要完全一致

[root@hadoop-bigdata01 test]# grep "1" /etc/passwd

bin:x:1:1:bin:/bin:/sbin/nologin

mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin

operator:x:11:0:operator:/root:/sbin/nologin

games:x:12:100:games:/usr/games:/sbin/nologin

gopher:x:13:30:gopher:/var/gopher:/sbin/nologin

ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

dbus:x:81:81:System message bus:/:/sbin/nologin

usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin

avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin

abrt:x:173:173::/etc/abrt:/sbin/nologin

wang:x:501:501::/home/wang:/bin/bash

grep 'root' /etc/passwd

cat /etc/passwd | grep 'root'

都是同樣的道理,但是管道符更吃資源

所以

1.匹配含有數位行

grep '[0-9]' /etc/passwd

2.匹配連續含有三個數位行

grep '[0-9][0-9][0-9]' /etc/passwd 或者 grep ':[0-9][0-9][0-9]:' /etc/passwd

[root@hadoop-bigdata01 test]# grep '[0-9][0-9][0-9]' /etc/passwd

games:x:12:100:games:/usr/games:/sbin/nologin

usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin

rtkit:x:499:497:RealtimeKit:/proc:/sbin/nologin

avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin

abrt:x:173:173::/etc/abrt:/sbin/nologin

nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin

saslauth:x:498:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologin

pulse:x:497:496:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin

liucheng:x:500:500::/home/liucheng:/bin/bash

wang:x:501:501::/home/wang:/bin/bas

3.匹配以r開頭 n結尾的行

grep '^r.*n$' /etc/passwd

.*代表所有

[root@hadoop-bigdata01 test]# grep '^r.*n$' /etc/passwd

rpc:x:32:32:Rpcbind Daemon:/var/cache/rpcbind:/sbin/nologin

rtkit:x:499:497:RealtimeKit:/proc:/sbin/nologin

rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin

4.過濾ifconfig ,截取ip

grep -v 代表反向截取,意思就是去除帶有某關鍵字的行 sed有替換的意思

[root@hadoop-bigdata01 test]# ifconfig | grep 'inet addr:'

inet addr:192.168.126.191 Bcast:192.168.126.255 Mask:255.255.255.0

inet addr:127.0.0.1 Mask:255.0.0.0

[root@hadoop-bigdata01 test]#

[root@hadoop-bigdata01 test]# ifconfig | grep 'inet addr:' | grep -v '127.0.0.1'

inet addr:192.168.126.191 Bcast:192.168.126.255 Mask:255.255.255.0

[root@hadoop-bigdata01 test]# ifconfig | grep 'inet addr:' | grep -v '127.0.0.1' | sed 's/inet addr://g'

192.168.126.191 Bcast:192.168.126.255 Mask:255.255.255.0

[root@hadoop-bigdata01 test]# ifconfig | grep 'inet addr:' | grep -v '127.0.0.1' | sed 's/inet addr://g' | sed 's/Bcast.*//g'

192.168.126.191

誤區

這裡有個誤區,想了好久,是Regex和萬用字元的區別

我們知道萬用字元的*指的是任一字元,可重複多次 Regex的*指的是匹配前一個字元>=0次

這兩個是完全不同的,那如何知道我用的*是萬用字元還是Regex

起初我陷入一個誤區,看下面這串命令

[root@hadoop-bigdata01 test]# touch ac aac abc abbc

[root@hadoop-bigdata01 test]# ll

total 0

-rw-r--r-- 1 root root 0 May 16 19:55 aac

-rw-r--r-- 1 root root 0 May 16 19:55 abbc

-rw-r--r-- 1 root root 0 May 16 19:55 abc

-rw-r--r-- 1 root root 0 May 16 19:55 ac

[root@hadoop-bigdata01 test]# ls | grep 'a*c'

aac

abbc

abc

ac

[root@hadoop-bigdata01 test]# ls | grep 'a.*c'

aac

abbc

abc

ac

[root@hadoop-bigdata01 test]# ls | grep '^a.*c'

aac

abbc

abc

ac

[root@hadoop-bigdata01 test]# ls | grep '^a*c'

aac

ac

為什麼grep 'a*c' 和 grep '^a*c$' 的結果會不一樣,我以為一個是萬用字元,一個是正則,因為a*c顯示的四個結果,正好

不就是匹配任意多個字元嗎

其實不然

萬用字元的作用是用來匹配檔案名稱的

Regex是在檔案中匹配合格字串的

交給管道符之後使用grep已經不是匹配檔案名稱了,這是對檔案的操作,所以說,他完全就是Regex

grep 'a*c' 表示的是匹配a>=0個所以只要含有c就是可以的

而grep '^a*c$'也是正則,表示的是以a開頭,且第二個字元匹配a零次或者多次,接下來是c字母的

所以只有aac 和ac 符合條件

所以看這個例子

[root@hadoop-bigdata01 test]# ls

a aac abb abbc abc ac b bb c cb

[root@hadoop-bigdata01 test]# ls | grep 'a*b'

abb

abbc

abc

b

bb

cb

這裡grep 'a*b' 指的可不是含有a和b 而是a重複0次或者多次然後含有b <

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.