linux 正则表达式与实践
正則表達式基礎
準備
(1)alias grep='grep --color=auto' 易于顯示
(2)LC_ALL=C,字符集,設置環境變量,字符順序
基礎正則
1)^word? 匹配以Word開頭的內容 vi/vim 里^代表一行的開頭
2)word$? 匹配以Word結尾的內容 vi/vim 里$代表一行的結尾
3)^$??? 表示空行
#過濾空行,并顯示行號
[root@wen data]# grep -n "^$" lrs.txt
2:
4:
6:
8:
#過濾掉所有空行不顯示
[root@wen data]# grep -v "^$" lrs.txt
You're in a battle with
A bulletproof heart
You really got me, got me
A bulletproof heart.
4). 代表且只能代表任意一個字符
5)\ 轉義符號,讓特殊字符顯示其原型 \$ \.
6)* 重復0個或多個前面一個字符,例 o* 匹配 沒有o 有一個或多個 oo? oooooo……
7).* 匹配所有字符 延伸 ^.*以任意多個字符開頭? .*$以任意多個字符結尾
點(.)含義總結
1.當前目錄
2.使得文件生效 相當于source
3.隱藏文件
4.任意一個字符(grep正則)
#只顯示匹配到的內容
[root@wen data]# grep -on "you" lrs.txt
9:you
12:you
8).[abc] 匹配字符集合內的任意一個字符[a-zA-Z],[0-9]
9).[~abc]? 匹配不包含~后的任意一個字符的內容
????????????????????????? ~為去反,注意和中括號外面以……開頭區別
????????????????????????? [root@wen data]# grep "[~ot]" lrs.txt
10.a\{n,m\} 重復n到m次,前一個重復的字符 如果用egrep/sed -r 可以去掉斜線
???????? a\{n,\}? 重復至少n次,前一個重復字符 如果用egrep或sed -r 可以去掉斜線
???????? a\{n\}? 重復n次,前一個重復字符? 如果用egrep/sed -r 可以去掉斜線
???????? a\{,m\}? 重復最多m次
擴展
???????? 11. +? 表示 重復一個或一個以上 前面的字符(*是0或多個)
???????? [root@wen data]# grep -E "on+" lrs.txt
???????? So if you don't know how to
???????? I'm not gonna break your fall.
???????? 12. ?? 表示 0或一個 前面的字符(.是有且只有一個)
???????? 13. |? 表示同時過濾多個字符串
???????? root@wen data]# grep -E "3306|1521" /etc/services
???????? mysql 3306/tcp # MySQL
???????? mysql 3306/udp # MySQL
???????? ncube-lm 1521/tcp # nCube License Manager
???????? ncube-lm 1521/udp # nCube License Manager
14.() 分組過濾,后向引用
???????? [root@wen data]# grep -E "g(la|oo)d" lrs.txt
???????? glad
???????? good
#POSIX字符類(了解)
[:lower:] 匹配小寫(麻煩,直接用下面的例子)
???????? root@wen data]# grep -E "[a-z]" lrs.txt
???????? You're in a battle with
???????? A bulletproof heart
???????? You really got me, got me
???????? A bulletproof heart.
#元字符
\b 單詞邊界 只過濾單詞
???????? [root@wen data]# grep -E "\byou\b" lrs.txt
???????? So if you don't know how to
???????? [root@wen data]# grep -E "you" lrs.txt
???????? So if you don't know how to
???????? I'm not gonna break your fall.
#三劍客grep總結
???????? -a 在二進制文件中,以文本文件的方式搜索數據
???????? -c 計算找到“搜索字符串”的次數
???????? -o 僅顯示出匹配grep的內容(用于統計出現在文中的次數)
???????? -i 忽略大小寫的不同,所有大小寫視為相同
???????? -n? 匹配的內容在其行首顯示行號
???????? -v? 反向選擇,即顯示沒有“搜索字符串”內容的那一行
???????? -E? 擴展的grep ,即egrep
???????? --color=auto? 以特定顏色高亮顯示匹配關鍵字
???????? -C #除了顯示匹配行外,顯示該行前后的num行
???????? -B #除了顯示匹配行外,顯示該行之前的num行
???????? -A #除了顯示匹配行外,顯示該行之后的num行? ?
正則表達式實戰
一,將第一列與最后一列交換位置
[root@wen data]# tail /etc/passwd
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
haldaemon:x:68:68:HAL daemon:/:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
saslauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
mygirl:x:500:500::/home/mygirl:/bin/bash
fadewalk:x:501:501::/home/fadewalk:/bin/bash
[root@wen data]# tail /etc/passwd |sed -nr 's#([^:]+)(:.*:)(/.*$)#\3\2\1#gp'? #第一個([^:])取非:開頭的
/sbin/nologin:x:69:69:virtual console memory owner:/dev:vcsa
/sbin/nologin:x:173:173::/etc/abrt:abrt
/sbin/nologin:x:68:68:HAL daemon:/:haldaemon
/sbin/nologin:x:38:38::/etc/ntp:ntp
/sbin/nologin:x:499:76:Saslauthd user:/var/empty/saslauth:saslauth
/sbin/nologin:x:89:89::/var/spool/postfix:postfix
/sbin/nologin:x:74:74:Privilege-separated SSH:/var/empty/sshd:sshd
/sbin/nologin:x:72:72::/:tcpdump
/bin/bash:x:500:500::/home/mygirl:mygirl
/bin/bash:x:501:501::/home/fadewalk:fadewalk
二,取IP或數字
1.取數字
[root@wen ~]# ifconfig | sed -nr 's#^.*dr:(.*) B.*$#\1#gp'
192.168.59.130
[root@wen ~]# stat /etc/hosts
File: "/etc/hosts"
Size: 158 Blocks: 8 IO Block: 4096 普通文件
Device: 802h/2050d Inode: 40 Links: 2
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2017-10-11 20:22:15.436493668 +0800
Modify: 2010-01-12 21:28:22.000000000 +0800
Change: 2017-08-30 05:59:08.040858188 +0800
取出644
[root@wen ~]# stat /etc/hosts|sed -nr 's#^.*0(.*)/-rw.*$#\1#gp'
644
[root@wen ~]# stat /etc/hosts|sed -nr 's#^.*\(0(.*)/-rw.*$#\1#gp'
644
2.取IP
[root@wen data]# ifconfig eth0
eth0????? Link encap:Ethernet? HWaddr 00:0C:29:E9:95:DD?
????????? inet addr:192.168.59.130? Bcast:192.168.59.255? Mask:255.255.255.0
????????? inet6 addr: fe80::20c:29ff:fee9:95dd/64 Scope:Link
????????? UP BROADCAST RUNNING MULTICAST? MTU:1500? Metric:1
????????? RX packets:6964 errors:0 dropped:0 overruns:0 frame:0
????????? TX packets:4430 errors:0 dropped:0 overruns:0 carrier:0
????????? collisions:0 txqueuelen:1000
????????? RX bytes:606332 (592.1 KiB)? TX bytes:489934 (478.4 KiB)
[root@wen ~]# ifconfig | sed -n '2s#^.*dr:##p'|sed 's# B.*$##g'
192.168.59.130
grep取IP
[root@wen ~]# ifconfig eth0 |grep "ddr:[0-9.]*"
????????? inet addr:192.168.13.128? Bcast:192.168.13.255? Mask:255.255.255.0
????????? inet6 addr: fe80::20c:29ff:fee9:95dd/64 Scope:Link
[root@wen ~]# ifconfig eth0 |grep -o "inet addr:[0-9.]*"
inet addr:192.168.13.128
[root@wen ~]# ifconfig eth0 |grep -o "inet addr:[0-9.]*"|grep -o "[0-9]*"
192
168
13
128
[root@wen ~]# ifconfig eth0 |grep -o "inet addr:[0-9.]*"|grep -o "[0-9.]*"
192.168.13.128
總結
以上是生活随笔為你收集整理的linux 正则表达式与实践的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 10.16 ln软硬链接的创建等
- 下一篇: linux 其他常用命令