GREP正則表達式:
復習:
glob文件通配符: *:任意長度字符: ?:任意單個字符: []:括號內的任意單個字符: [^]:括號內字符除外: [:digit:]:任意數字-->[0-9]: [:space:]:空白字符: [:lower:]:任意小寫字母: [:upper:]:任意大寫字母: [:alpha:]:任意字母不區分大小寫: [:alnum:]:任意字母或數字: [:punct:]:標點符號:
練習:
顯示/var目錄下所有以l開頭,以一個小寫字母結尾,且中間出現至少一位數字的文件或目錄; [user1@localhost ~]$ ls /var/l[[:digit:]]*[[:lower:]] 顯示/etc目錄下,以任意一位數字開頭,且以非數字結尾的文件或目錄; [root@localhost ~]# ls /etc/[[:digit:]]*[^[:digit:]] 顯示/etc/目錄下,以非字母開頭,后面跟了一個字母及其它任意長度任意字符的文件或目錄; [root@localhost ~]# ls /etc/[^[:alpha:]][[:alpha:]]* 復制/etc目錄下,所有以m開頭,以非數字結尾的文件或目錄至/tmp/mageedu目錄中; [root@localhost ~]# cp -a /etc/m*[^[:digit:]] /tmp/mageedu 復制/etc目錄下,所有以.d結尾的文件或目錄至/tmp/magedu.com目錄中; [root@localhost ~]# cp -a /etc/*.d /tmp/magedu.com 復制/etc目錄下,所以有.conf結尾,且以m,n,r,p開頭的文件或目錄至/tmp/mageedu.com目錄中; [root@localhost ~]# cp /etc/[mnrp]*.conf /tmp/mageedu.com
重定向
>:覆蓋重定向,目標文件的原有內容被清除: [root@localhost ~]# cat /etc/fstab > /tmp/wanghongkai [root@localhost ~]# [root@localhost ~]# cat /tmp/wanghongkai # # /etc/fstab # Created by anaconda on Sun Jul 10 08:30:21 2016 # # Accessible filesystems, by reference, are maintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # UUID=36e12a53-107b-4f2c-93d2-1a4836d67bab / ext4 defaults 1 1 UUID=b66484d9-582d-4371-b4d0-e4ced790c209 /boot ext4 defaults 1 2 UUID=001595df-98dd-441b-bb32-267407ff2e30 /home ext4 defaults 1 2 UUID=6578a451-4708-43da-b64c-376745edcefe swap swap defaults 0 0 tmpfs /dev/shm tmpfs defaults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defaults 0 0 proc /proc proc defaults 0 0 [root@localhost ~]# cat /etc/issue > /tmp/wanghongkai [root@localhost ~]# cat /tmp/wanghongkai CentOS release 6.4 (Final) Kernel \r on an \m >>:追加重定向,新內容會追加至目標文件尾部: [root@localhost ~]# cat /etc/fstab >> /tmp/wanghongkai [root@localhost ~]# cat /tmp/wanghongkai CentOS release 6.4 (Final) Kernel \r on an \m # # /etc/fstab # Created by anaconda on Sun Jul 10 08:30:21 2016 # # Accessible filesystems, by reference, are maintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # UUID=36e12a53-107b-4f2c-93d2-1a4836d67bab / ext4 defaults 1 1 UUID=b66484d9-582d-4371-b4d0-e4ced790c209 /boot ext4 defaults 1 2 UUID=001595df-98dd-441b-bb32-267407ff2e30 /home ext4 defaults 1 2 UUID=6578a451-4708-43da-b64c-376745edcefe swap swap defaults 0 0 tmpfs /dev/shm tmpfs defaults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defaults 0 0 proc /proc proc defaults 0 0 2>:覆蓋重定向錯誤輸出數據流: [root@localhost ~]# car /etc/passwd 2> /tmp/wanghongkai [root@localhost ~]# cat /tmp/wanghongkai -bash: car: command not found 2>>:追加重定向錯誤輸出數據流: [root@localhost ~]# cat /tmp/wanghongkai cat: /etc/rpm: Is a directory [root@localhost ~]# car /etc/ 2>> /tmp/wanghongkai [root@localhost ~]# cat /tmp/wanghongkai cat: /etc/rpm: Is a directory -bash: car: command not found 合并標準輸出和錯誤輸出為用一個數據流進行重定向; &>:覆蓋重定向 [root@localhost ~]# cat /etc/issue /etc/sddd &> /tmp/wanghongkai [root@localhost ~]# cat /tmp/wanghongkai CentOS release 6.4 (Final) Kernel \r on an \m cat: /etc/sddd: No such file or directory &>>:追加重定向
練習
將etc/passwd文件中的前5行內容轉換為大寫后保存至/tmp/passwd.out文件中: [root@localhost ~]# head -n5 /etc/passwd | tr 'a-z' 'A-Z' > /tmp/passwd.out [root@localhost ~]# cat /tmp/passwd.out ROOT:X:0:0:ROOT:/ROOT:/BIN/BASH BIN:X:1:1:BIN:/BIN:/SBIN/NOLOGIN DAEMON:X:2:2:DAEMON:/SBIN:/SBIN/NOLOGIN ADM:X:3:4:ADM:/VAR/ADM:/SBIN/NOLOGIN LP:X:4:7:LP:/VAR/SPOOL/LPD:/SBIN/NOLOGIN 將登陸至前系統上的用戶信息中的后3為的信息轉換為大寫后保存至/tmp/who.out [root@localhost ~]# w | tail -n3 | tr 'a-z' 'A-Z' > /tmp/who.out [root@localhost ~]# cat /tmp/who.out MAGEIA PTS/4 192.168.1.3 19:36 2:16M 0.02S 0.02S -BASH ROOT PTS/5 192.168.1.3 20:09 0.00S 0.13S 0.00S W USER1 PTS/6 192.168.1.3 20:10 26:13 0.02S 0.02S -BASH
文本處理工具:wc,cut,sort,uniq
這里就不一一介紹了下面用實驗來表達上述文本處理工具:
練習:
以冒號分隔,取出/etc/passwd文件的第6至第10行,并將這些信息按第3個字段的數值大小進行排序;最后僅顯示的各自的第1個字段; [root@localhost ~]# head -n10 /etc/passwd | tail -n5 | sort -t':' -k3 -n | cut -d':' -f1 sync shutdown halt mail uucp
grep:正則表達–>Linux上的文本處理三劍客
grep:文本過濾(模式:pattern)工具 grep,egrep,fgrep sed:stream editor:文本編輯工具; awk:Liunx上實現gwak 文本報告生成器: 模式:由正則表達式字符及文本字符所編寫的過濾條件; REGEXP:由一類特殊字符及文本字符所編寫的模式,其中有些字符不表示字符字面意義 而表示控制或同配的功能 使用nano 或vi編輯器:~/.bashrc中加入alias 定義的別名?。。。。。。。。?! 正則表達式: 基本正則表達式:BRE 擴展正則表達式:ERE grep -E=egrep
基本正則表達式元字符:
字符匹配: :匹配任意單個字符: []:指定范圍內的單個字符: [^]:匹配范圍之外的任意單個字符: 匹配次數:用在要指定次數的字符后面,用于指定前面的字符要出現的次數; *:匹配前面的字符任意次:不表示多余的意義 例如:grep ”x*y“ abxy xay xxxxy .*:任意長度的任意字符: \?:匹配其前面的字符0或1次:即前面的字符可有可無 \+:匹配其前面的字符至少1次; \{m\}:匹配前面的字符m次: \{m.n\}:匹配前面的字符至少m次,至多n次; \{0,n\}:匹配前面的字符至多n次: \{m,\}:匹配前面的字符至少m次: 位置錨定: ^:行首錨定 例如:^root 已root開頭:用于模式的最左側: $:行尾錨定 ^PATTERN$:用于模式匹配正行: ^$:空行: ^[[:space:]]*$:空格字符 \<或\b:詞首錨定:用于單詞模式的左側:例如:\<root \<或\b:詞尾錨定:用于單詞模式的右側: \<PATTERN\>:匹配整個單詞: [root@localhost etc]# grep "\<root\>" /etc/passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin 分組: \(\):將一個或多個字符捆綁在一起,當作一個整體進行處理: 例如:\(xy\)*ab:xy出現多次 Note:分組括號中的模式匹配到的內容會被正則表達式引擎記錄于內部的變量中,這些變量的命名方式為: \1, \2, \3, ... \1: 從左側起,第一個左括號以及與之匹配右括號之間的模式所匹配到的字符; \(ab\+\(xy\)*\): \1: ab\+\(xy\)* \2: xy
練習:
顯示/proc/meminfo文件中以大小s開頭的行;(要求:使用兩種方式) [root@localhost ~]# grep '^[Ss]' /proc/meminfo [root@localhost ~]# grep -i "^s" /proc/meminfo 顯示/etc/passwd文件中不以/bin/bash結尾的行; grep -v '/bin/bash$' /etc/passwd egrep -v "(/bin/bash)$" /etc/passwd 顯示/etc/passwd文件中ID號最大的用戶的用戶名; [root@localhost ~]# sort -t':' -k3 -n /etc/passwd | tail -n1 | cut -d':' -f1 nfsnobody 如果用戶root存在,顯示其默認的shell程序; [root@localhost ~]# id root >& /dev/null && grep "^root" /etc/passwd | cut -d ':' -f7 /bin/bash 找出/etc/passwd中的兩位或三位數; grep "\<[[:digit:]]\{2,3\}\>" /etc/passwd | wc -l 顯示/etc/rc.d/rc.sysinit文件中,至少以一個空白字符開頭的且后面存非空白字符的行; grep "^[[:space:]]\+[^[:space:]]" /etc/rc.d/rc.sysinit | wc -l 找出"netstat -tan"命令的結果中以'LISTEN'后跟0、1或多個空白字符結尾的行; netstat -tan | grep "LISTEN\>[[:space:]]\{0\}$" 添加用戶bash、testbash、basher以及nologin(其shell為/sbin/nologin);而后找出/etc/passwd文件中用戶名同shell名的行; [root@localhost ~]# grep "^\([[:alnum:]]\+\>\).*\1$" /etc/passwd sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt nologin:x:3009:3009::/home/nologin:/sbin/nologin
EGREP 擴展的正則表達式
egrep [OPTIONS] PATTERN [FILE...] 字符匹配:.[] [^] 次數匹配: * ?: 0或1次; +:1次或多次; {m}:匹配m次; {m,n}:至少m,至多n次; 錨定: ^ $ \<, \b \>, \b 分組: () 后向引用:\1, \2, ...
練習:
顯示當前系統root、centos或user1用戶的默認shell和UID; [root@localhost ~]# egrep "^(root|centos|user1)" /etc/passwd | cut -d':' -f1,3,7 root:0:/bin/bash user1:501:/bin/bash centos:3010:/bin/bash 找出/etc/rc.d/init.d/functions文件(centos6)中某單詞后面跟一個小括號的行; [root@localhost ~]# grep -E -o "^[_[:alpha:]]+\(\)" /etc/rc.d/init.d/functions 使用echo輸出一絕對路徑,使用egrep取出其基名; echo "/mnt/sdc" | grep -E -o "[^/]+/?$" | cut -d"/" -f1 找出ifconfig命令結果中1-255之間的數值; [root@localhost ~]# ifconfig | grep '[0-9]\{1,\}[/./][0-9]\{1,\}[/./][0-9]\{1,\}[/./][0-9]\{1,\}' inet addr:192.168.1.5 Bcast:192.168.1.255 Mask:255.255.255.0 inet addr:127.0.0.1 Mask:255.0.0.0 找出ifconfig命令結果中的IP地址; [root@localhost ~]# ifconfig | grep 'inet addr:[1-9]\{1,\}[/./][0-9]\{1,\}[/./][0-9]\{1,\}[/./][1-9]\{1,\}' inet addr:192.168.1.5 Bcast:192.168.1.255 Mask:255.255.255.0 inet addr:127.0.0.1 Mask:255.0.0.0 ifconfig | grep -o 'inet addr:[1-9]\{1,\}[/./][0-9]\{1,\}[/./][0-9]\{1,\}[/./][1-9]\{1,\}' **inet addr:192.168.1.5** **inet addr:127.0.0.1** ☆★☆★此處請老師幫忙看一下,我是否匹配對!!
原創文章,作者:wostop,如若轉載,請注明出處:http://www.www58058.com/24143
寫的很好,排版也很漂亮,ip地址的匹配不是太對,在匹配一下試試,加油
@馬哥教育:ifconfig | egrep -o “(\|\|\<2[0-5][0-4])[.](\|\|\<2[0-5][0-4])[.](\|\|\<2[0-5][0-4])[.](\|\|\<2[0-5][0-4])"
192.168.1.5
127.0.0.1