grep&正則表達式
grep(global search regular expression(RE) and print out the line,全面搜索正則表達式并把行打印出來)是一種強大的文本搜索工具,它能使用正則表達式搜索文本,并把匹配的行打印出來。
語法
grep [選項]... PATTERN [FILE]...
選項
-a 不要忽略二進制數據。
-A<顯示列數> 除了顯示符合范本樣式的那一行之外,并顯示該行之后的內容。
-b 在顯示符合范本樣式的那一行之外,并顯示該行之前的內容。
-c 計算符合范本樣式的列數。
-C<顯示列數>或-<顯示列數> 除了顯示符合范本樣式的那一列之外,并顯示該列之前后的內容。
-d<進行動作> 當指定要查找的是目錄而非文件時,必須使用這項參數,否則grep命令將回報信息并停止動作。
-e<范本樣式> 指定字符串作為查找文件內容的范本樣式。
-E 將范本樣式為延伸的普通表示法來使用,意味著使用能使用擴展正則表達式。
-f<范本文件> 指定范本文件,其內容有一個或多個范本樣式,讓grep查找符合范本條件的文件內容,格式為每一列的范本樣式。
-F 將范本樣式視為固定字符串的列表。
-G 將范本樣式視為普通的表示法來使用。
-h 在顯示符合范本樣式的那一列之前,不標示該列所屬的文件名稱。
-H 在顯示符合范本樣式的那一列之前,標示該列的文件名稱。
-i 忽略字符大小寫的差別。
-l 列出文件內容符合指定的范本樣式的文件名稱。
-L 列出文件內容不符合指定的范本樣式的文件名稱。
-n 在顯示符合范本樣式的那一列之前,標示出該列的編號。
-q 不顯示任何信息。
-R/-r 此參數的效果和指定“-d recurse”參數相同。
-s 不顯示錯誤信息。
-v 反轉查找。
-w 只顯示全字符合的列。
-x 只顯示全列符合的列。
-y 此參數效果跟“-i”相同。
-o 只輸出文件中匹配到的部分。
正則表達式
^word 搜尋以word開頭的行。 例如:搜尋以#開頭的腳本注釋行 grep –n '^#' /PATH/TO/FILENAME
word$ 搜尋以word結束的行
. 匹配任意一個字符。 例如:grep –n 'e.e regular.txt 匹配e和e之間有任意一個字符,可以匹配eee,eae,eve,但是不匹配ee。
\ 轉義字符。 例如:搜尋’,’是一個特殊字符,在正則表達式中有特殊含義。必須要先轉義。grep –n '\,' /PATH/TO/FILENAME
* 前面的字符重復0到多次。 例如匹配gle,gogle,google,gooogle等等 grep –n 'go*gle' /PATH/TO/FILENAME
[list] 匹配一系列字符中的一個。 例如:匹配gl,gf。grep –n 'g[lf]' /PATH/TO/FILENAME
[n1-n2] 匹配一個字符范圍中的一個字符。 例如:匹配數字字符 grep –n '[0-9]' /PATH/TO/FILENAME
[^list] 匹配字符集以外的字符 例如:grep –n '[^o]' /PATH/TO/FILENAME 匹配非o字符
\<word 匹配單詞開頭。 例如:匹配以g開頭的單詞 grep –n '\<g' /PATH/TO/FILENAME
word\> 匹配單詞結尾 例如:匹配以tion結尾的單詞 grep –n 'tion\>' /PATH/TO/FILENAME
word\{n1\} 前面的字符重復n1 例如:匹配google。 grep –n 'go\{2\}gle' /PATH/TO/FILENAME
word\{n1,\} 前面的字符至少重復n1 例如:匹配google,gooogle。 grep –n 'go\{2\}gle' /PATH/TO/FILENAME
word\{n1,n2\} 前面的字符重復n1,n2次 例如:匹配google,gooogle。 grep –n 'go\{2,3\}gle' /PATH/TO/FILENAME
擴展正則表達式
? 匹配0個或1個在其之前的那個普通字符。
例如,匹配gd,god grep –nE 'go?d' /PATH/TO/FILENAME
+ 匹配1個或多個在其之前的那個普通字符,重復前面字符1到多次。
例如:匹配god,good,goood等等字符串。
grep –nE 'go+d' /PATH/TO/FILENAME
() 表示一個字符集合或用在expr中,匹配整個括號內的字符串,
原來都是匹配單個字符。 例如:搜尋good或者glad
grep –nE 'g(oo|la)' /PATH/TO/FILENAME
| 表示“或”,匹配一組可選的字符,或(or)的方式匹配多個字串。
例如:grep –nE 'god|good' /PATH/TO/FILENAME 匹配god或者good。
常用的集合方法
[:alnum:] 匹配任意一個字母或數字字符
[:alpha:] 匹配任意一個字母字符(包括大小寫字母)
[:blank:] 空格與制表符(橫向和縱向)
[:digit:] 匹配任意一個數字字符
[:lower:] 匹配小寫字母
[:upper:] 匹配大寫字母
[:punct:] 匹配標點符號
[:space:] 匹配一個包括換行符、回車等在內的所有空白符
[:graph:] 匹配任何一個可以看得見的且可以打印的字符
[:xdigit:] 任何一個十六進制數(即:0-9,a-f,A-F)
[:cntrl:] 任何一個控制字符(ASCII字符集中的前32個字符)
[:print:] 任何一個可以打印的字符
實例
[root@localhost tmp]# cat test
hello world
HELLO everyone
helLo everyone hello world
HELLO EVERYONE HELLO WORLD
#I LIKE EVERYTHING
- 統計符合范本樣式的列數
[root@localhost tmp]# cat test | grep -c "hello" 2
- 統計時忽略字符大小寫的差別
[root@localhost tmp]# cat test | grep -i "hello" hello world HELLO everyone helLo everyone hello world HELLO EVERYONE HELLO WORLD
- 統計時顯示行數
[root@localhost tmp]# cat test | grep -n "hello" 1:hello world 3:helLo everyone hello world
- 反向統計,不包含統計字符的所有行
[root@localhost tmp]# cat test | grep -v "hello" HELLO everyone HELLO EVERYONE HELLO WORLD #I LIKE EVERYTHING
- 統計以”hel”開通第四個字符是”l”或”L”的所有行
[root@localhost tmp]# cat test | grep "hel[lL]o" hello world helLo everyone hello world
- 統計以hello開頭的所有行
[root@localhost tmp]# cat test | grep "^hello" hello world
- 統計開通不是“h”或“I”的所有行,注意[^list]list是字符集,不是world
[root@localhost tmp]# cat test | grep "^[^hI]" HELLO everyone HELLO EVERYONE HELLO WORLD
- 統計以字符w開頭,ld結尾,中間任意兩個字符的所有行
[root@localhost tmp]# cat test | grep "w..ld" hello world helLo everyone hello world
- 統計以字符h開頭,ld結尾,中間任意字符的所有行
[root@localhost tmp]# cat test | grep "h.*ld" hello world helLo everyone hello world
- 統計以匹配單詞統計的所有行
[root@localhost tmp]# cat test | grep "\<hello\>" hello world helLo everyone hello world
- 統計“l”重復2次的所有行
[root@localhost tmp]# cat test | grep "l\{2\}" hello world helLo everyone hello world
- 統計“l”重復1或2次的所有行
[root@localhost tmp]# cat test | grep "l\{1,2\}" hello world helLo everyone hello world
- 統計“l”重復2次或2次以上以上的所有行
[root@localhost tmp]# cat test | grep "l\{2,\}" hello world helLo everyone hello world
- 統計顯示空白行的行號
[root@localhost tmp]# cat test | grep -n "^$" 6: 7: 8: 9:
- 統計空白行和注釋行,帶行號
[root@localhost tmp]# cat test | grep -nE "^$|#" 5:#I LIKE EVERYTHING 6: 7: 8: 9:
綜合實例
- 復制/etc/skel目錄為/home/tuser1,要求/home/tuser1及其內部文件的屬組和其它用戶均沒有任何訪問權限。
[root@localhost ~]# useradd tuser1;chmod -R go=--- /home/tuser1 [root@localhost ~]# ll -a /home/tuser1 總用量 12 drwx------. 2 tuser1 tuser1 62 3月 22 10:05 . drwxr-xr-x. 10 root root 124 3月 22 10:05 .. -rw-------. 1 tuser1 tuser1 18 8月 3 2016 .bash_logout -rw-------. 1 tuser1 tuser1 193 8月 3 2016 .bash_profile -rw-------. 1 tuser1 tuser1 231 8月 3 2016 .bashrc
- 編輯/etc/group文件,添加組hadoop。
[root@localhost ~]# let groupid=$( cat /etc/group | grep "\<hadoop\>" || cat /etc/group | tail -1 | cut -d: -f3 )+1 && echo "hadoop:x:$groupid" >>/etc/group && cat /etc/group | grep "\<hadoop\>" hadoop:x:1009
- 手動編輯/etc/passwd文件新增一行,添加用戶hadoop,其基本組ID為hadoop組的id號;其家目錄為/home/hadoop
#!/bin/bash #20180322 by eighteenxu uid=$(cat /etc/group | grep "\<hadoop\>" | cut -d: -f3) id $uid &> /dev/null if [ $? -eq 0 ];then echo "該ID用戶已存在" else echo "hadoop:x:$uid:$uid::/home/hadoop:/bin/bash" >> /etc/passwd && cat /etc/passwd | grep "\<hadoop\>" fi
- 復制/etc/skel目錄為/home/hadoop,要求修改hadoop目錄的屬組和其它用戶沒有任何訪問權限。
修改/home/hadoop目錄及其內部所有文件的屬主為hadoop,屬組為hadoop。#!/bin/bash #20180322 by eighteenxu #檢查目錄/home/hadoop是否存在,如果存在直接復制,不存在新建目錄并復制 cd /home/hadoop if [ $? -eq 0 ];then cp -R /etc/skel/. /home/hadoop else mkdir /home/hadoop && cp -R /etc/skel/. /home/hadoop fi #更改目錄屬住和屬組,更改目錄和子目錄的權限 chown -R hadoop:hadoop /home/hadoop && chmod -R go=--- /home/hadoop [root@localhost ~]# ll /home | grep hadoop && ll -a /home/hadoop drwx------. 2 hadoop hadoop 62 4月 11 2017 hadoop 總用量 12 drwx------. 2 hadoop hadoop 62 4月 11 2017 . drwxr-xr-x. 9 root root 106 3月 22 14:13 .. -rw-------. 1 hadoop hadoop 18 3月 22 14:08 .bash_logout -rw-------. 1 hadoop hadoop 193 3月 22 14:08 .bash_profile -rw-------. 1 hadoop hadoop 231 3月 22 14:08 .bashrc
- 顯示/proc/meminfo文件中以大寫或小寫S開頭的行;用兩種方式;
[root@localhost ~]# cat /proc/meminfo | grep "^[sS]" SwapCached: 0 kB SwapTotal: 2097148 kB SwapFree: 2097148 kB Shmem: 6868 kB Slab: 64376 kB SReclaimable: 28160 kB SUnreclaim: 36216 kB
- 顯示/etc/passwd文件中其默認shell為非/sbin/nologin的用戶;
[root@localhost ~]# cat /etc/passwd | grep -v "/bin/nologin$" | cut -d: -f1 root bin daemon adm lp sync shutdown halt mail operator games ftp nobody systemd-bus-proxy systemd-network dbus polkitd tss postfix sshd chrony linux centos test tuser1 hadoop
- 顯示/etc/passwd文件中其默認shell為/bin/bash的用戶;
[root@localhost ~]# cat /etc/passwd | grep "/bin/bash$" | cut -d: -f1 root linux centos test tuser1 hadoop
- 找出/etc/passwd文件中的一位數或兩位數;
[root@localhost ~]# cat /etc/passwd | grep "\<[[:digit:]]\{1,2\}\>" 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 sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
- 顯示/boot/grub2/grub.cfg中以至少一個空白字符開頭的行;
[root@localhost ~]# cat /boot/grub2/grub.cfg | grep -E "^[[:space:]]+"
- 顯示/etc/rc.d/rc.sysinit文件中以#開頭,后面跟至少一個空白字符,而后又有至少一個非空白字符的行;
[root@localhost ~]# cat /etc/rc.d/rc.sysinit | grep "^#[[:space:]][[:graph:]]"
- 打出netstat -tan命令執行結果中以‘LISTEN’,后或跟空白字符結尾的行;
[root@localhost ~]# netstat -tan | grep "LISTEN[[:space:]]*$" tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN tcp6 0 0 :::22 :::* LISTEN tcp6 0 0 ::1:25 :::* LISTEN
- 添加用戶bash, testbash, basher, nologin (此一個用戶的shell為/sbin/nologin),而后找出當前系統上其用戶名和默認shell相同的用戶的信息;
[root@localhost ~]# useradd bash && useradd testbash && useradd basher && useradd nologin -s /sbin/nologin [root@localhost cript]# cat /etc/passwd | grep "\(\<[[:alnum:]]\+\>\).*\1$" sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt bash:x:1010:1010::/home/bash:/bin/bash nologin:x:1013:1013::/home/nologin:/sbin/nologin
本文來自投稿,不代表Linux運維部落立場,如若轉載,請注明出處:http://www.www58058.com/92847