文本查找工具:grep, egrep, fgrep
grep:
grep -E = egrep
正則表達式:regular expression, regex
由元字符組合的式子,可以當作pattern使用以匹配符合指定特徵的字符串
基本正則表達式
b
擴展正則表達式
e
元字符:
^PATTERN 行首錨定符 要求必須出現在行首
PATTERN$ 行尾錨定符 要求必須出現在行尾
^PATTERN$ PATTERN 要求必須出現在行首及行尾
“\<PATTERN” 詞首錨定符 要求PATTERN出現在詞首
“PATTERN\>” 詞尾錨定符 要求PATTERN必須出現在詞尾
“\<PATTERN\>” 單詞錨定符 要求精確匹配一個單詞
. 任意單個非空白字符,不能指定多個字符 #grep --color=auto ‘r….t’ file
星號* 匹配此前的字符0到任意次 a*b,ab,aab,acb,b *做次數匹配,而不是字符匹配
.* 陪陪任意長度的任意字符 #grep --color=auto ‘r.*t’ file 默認工作在貪婪模式下,儘可能長的匹配符合模式的字符串
? 匹配此前的字符0次或1次
PATTERN\{m,n\} 匹配此前的字符至少m次,之多n次
\{0,5\} 至多5次 #grep –color=auto “r.\{3,10\}t” file
\{5,\} 至少5次
\{5\} 匹配5次
[] 匹配指定範圍內的任意單個字符
[^] 匹配指定範圍外的任意單個字符
[:space:] #grep --color=auto “r[[:alpha:]]\{3,10\}t” file 指定以r開頭,t 結尾的中間以字母並且出現3到10次
\(\) \1,\2,\3
[root@station30 ~]# cat grep.txt
andyedu is a linux lover,www.andyedu.com
andyemu is a linux lover,www.andyemu.com
andyedu is a linux lover,www.andyemu.com
[root@station30 ~]# grep --color=auto "andye[dm]u.*andye[dm]u\.com" grep.txt
andyedu is a linux lover,www.andyedu.com
andyemu is a linux lover,www.andyemu.com
andyedu is a linux lover,www.andyemu.com
[root@station30 ~]# grep --color=auto "\(andye[dm]u\).*\1\.com" grep.txt
andyedu is a linux lover,www.andyedu.com
andyemu is a linux lover,www.andyemu.com
\(\) \1 將\前邊出現的字符出現 \1表示匹配第一個括號內的字符。
[root@station30 ~]# clear
[root@station30 ~]# cat grep2.txt
abc.111.222.333
12a.333.444.567
11.222.333.444
111.2.333.333
444.444.444.111
[root@station30 ~]# grep "[0-9]\{3\}\.[0-9]\{3\}\.[0-9]\{3\}\.[0-9]\{3\}" grep2.txt
444.444.444.111
Practice1: 查找當前系統上名字爲user1的用戶的帳號的相關信息, /etc/passwd, user11, myuser1
[root@station30 ~]# tail -2 /etc/passwd
user11:x:5001:5001::/home/user11:/bin/bash
myuser11:x:5002:5002::/home/myuser11:/bin/bash
[root@station30 ~]# grep "^user11:" /etc/passwd 以user11:爲行首錨定符
user11:x:5001:5001::/home/user11:/bin/bash
grep "^user11\>" /etc/passwd 以user11爲行首和詞尾的錨定符
user11:x:5001:5001::/home/user11:/bin/bash
practice2: 查找當前系統上以其爲附加組的用戶有兩個或兩個以上的組的相關信息, /etc/group, :,
practice3:查找當前系統上其用戶帳號密碼最長使用期爲99999天的用戶帳號的相關信息;99999
#grep --color=auto "\(.*:\)\{4\}99999" /etc/shadow
# grep "[^:]*:[^:]*:[^:]*:[^:]*:99999:" /etc/shadow
Practice4: 分析/etc/inittab文件中如下兩行的文本特徵,請寫出可以精確找到類似兩行的模式,而且要求每一行中出現在數字必須相同
grep --color=auto "l\([1-5]\):\1:wait:/etc/rc.d/rc[[:space:]]\{1,\}\1" /etc/inittab
l1:1:wait:/etc/rc.d/rc 1
l2:2:wait:/etc/rc.d/rc 2
l3:3:wait:/etc/rc.d/rc 3
l4:4:wait:/etc/rc.d/rc 4
l5:5:wait:/etc/rc.d/rc 5
grep的常用選項:
--color=(auto|always)
-v 取反 不匹配的顯示
-i 忽略大小寫 字母不區分大小寫
-r 表示搜索多個文件,在指定路徑下搜索某個遞歸的文件
-B 跟上一個數字,在匹配出來的字符串之前多少行顯示出來
-A 跟上一個數字,在匹配出來的字符串之後多少行顯示出來
-C 跟上一個數字,在匹配出來的字符串前後多少行顯示出來
-o 只顯示被模式匹配的字符串,而非顯示字符串所在的行
學習正則表達式的學習幫助:
#man 7 regex
擴展正則表達式
grep –E
\{m,n\} , {m,n} 匹配此前的字符至少m次,至多n次
\(\), ()
| 表示或的意思 #egrep –n ‘gd |good ‘ file
[root@station30 ~]# grep -E "^root|redhat" /etc/passwd
root:x:0:0:root:/root:/bin/bash
redhat:x:5005:5005::/home/redhat:/bin/bash
+ 重複一個或一個以上的RE字符 egrep –n ‘go+d’ file o+表示不止一個o
我們怎麼去grep一個IP地址:
[1-9] | [1-9][0-9] | 1[0-9][0-9] | 2[0-1][0-9] | 22[0-3]
第一位 |第二位 |第三位
如何搜尋IP地址範例:
[root@station30 ~]# cat grep2.txt
abc.111.222.333
12a.333.444.567
11.222.333.444
111.2.333.333
444.444.444.111
192.168.254.1
172.16.254.253
10.10.2.1
100.299.21.023
[root@station30~]#grep -E "\<([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-1][0-9]|22[0-3])(\.([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])){3}\>" grep2.txt
192.168.254.1
172.16.254.253
10.10.2.1
輸入輸出重定向,管道的實現
標準輸入:鍵盤 0
標準輸出:monitor 1
錯誤輸出:--跟標準輸入、輸出沒有關係 2
重定向:
覆蓋輸出重定向:> 範例:#ls /etc > /tmp/etc.out 輸出重定向每次都要覆蓋原輸出結果
注:我們可以使用#set –c 來拒絕覆蓋輸出重定向;#set +c開啓覆蓋重定向
這個時候,如果我們需要確定覆蓋的話,則我們可以在 >| 來實現
追加輸出重定向:>>
錯誤輸出重定向:2> 只定向錯誤輸出,不定向正常輸出。
2>> 錯誤追加輸出重定向,不覆蓋
&>整合標準輸出和錯誤輸出保存到同一個文件中去;
2>&1 將錯誤的輸出信息整合輸出到正確輸出中去;
命令執行結束後,都有一個推出碼
成功執行:0
錯誤執行:1-255
獲取返回值:#echo $?
[root@station30 ~]# date TT &> /dev/null
[root@station30 ~]# echo $?
1 這個時候的返回碼是1,表示執行錯誤。
/dev/null 這是一個模擬出來的設備,bit bucket, 位桶
輸入重定向:
<將數據流從一個文件來進行導入
<< Here Document 用來生成文檔 --在腳本中使用可以用來生成一個文檔
[root@station30 ~]# cat > /tmp/a.out << END
> I am andy
> You are steve
> you are bill
> END
[root@station30 ~]# cat /tmp/a.out
I am andy
You are steve
you are bill
管道:|管道是將前一個命令的輸出通過一個內部通道送給後一個命令,被後一個命令到做輸入。
COMMAND1 | COMMAND2 | COMMAND3 連接多個小程序完成一個複雜的任務。
[root@station30 ~]# ifconfig | less 可以分頁查看IP地址信息
[root@station30 ~]# ifconfig | grep "inet addr:" |cut -d: -f2 |cut -d' ' -f1 |grep -v "127.0.0.1"
172.16.31.1
Tee 命令 “read from standard input and write to standard output and files”
既可以顯示到屏幕,有可以輸出
[root@station30 ~]# ls /var | tee /tmp/varout.2
account
cache
cvs
…