grep 選項git
-c :只輸出匹配行的計數。正則表達式
-i :忽略大小寫(只適用於單字符)centos
-n :顯示匹配行及行號bash
-v :取反(顯示不匹配的行)spa
-w :精確匹配某個字符3d
-o :只顯示被匹配到的字符串字符串
-A 顯示匹配的行以及以後的N行it
-B 顯示匹配的行以及以前的N行test
--no-group-separator:ftp
當使用'-A', '-B' or '-C'時,不輸出任何組分隔符,而是將不一樣組相鄰輸出。
--color :以顏色的形式顯示被匹配到字符
咱們能夠經過設置別名來方便咱們的操做
alias grep='grep -n --color'
再次使用grep 效果以下:
[root@centos5 ~]# grep "root" /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
11:operator:x:11:0:operator:/root:/sbin/nologin
關鍵詞已經被着色而且顯示行號
<1>[root@localhost etc]# grep -c "^root" passwd
1
這表示符合條件的行只有1行。
<2>[root@localhost etc]# grep -n "^root" passwd
1:root:x:0:0:root:/root:/bin/bash
顯示匹配行及行號
<3> -o -w的區別
[root@localhost pangbing]# cat 1
12
123
1234
134
-o 效果
[root@localhost pangbing]# grep -o '12' 1
12
12
12
他把含有12的都過濾出來來了,可是多餘的就去掉了
-w效果
[root@localhost pangbing]# grep -w '12' 1
12
他只匹配了12,精確匹配了
-A效果
[root@localhost pangbing]# grep -w -A 1 '12' 1
12
123
他將匹配的行的下一也打印出來
-B效果
[root@localhost pangbing]# grep -w -B 1 '123' 1
12
123
--no-group-separator:
[root@centos3 log]# cat abc
123abcakshdjh fff
qwe aaa
asd bbb
1111111111111
sssssssssssssss
123lzskjdfaklsjdkla fff
aaa 123
abv fvvv
222222222222222222222
aaaaaaaaaaaaaaaaaaaaa
123dddddsadasdasd fff
sdsdsd asd
sadsad asdasd
vvvvvvvvvvvvvvvvvv
[root@centos3 log]# grep -A 2 "123" abc
123abcakshdjh fff
qwe aaa
asd bbb
--
123lzskjdfaklsjdkla fff
aaa 123
abv fvvv
222222222222222222222
--
123dddddsadasdasd fff
sdsdsd asd
sadsad asdasd
此時過濾的結果當中有-- 分割符號,去掉這個符號加上--no-group-separator:
選項就沒有了。
grep -A 2 --no-group-separator "123" abc
通配符:
* :任意長度的任意字符
?:單個字符
[] :範圍以內
[^] :範圍以外的
2、正則表達式
(1). (點):匹配任意單個字符
匹配以r開頭,t結尾中間只有兩個字符的行
例子: [root@centos5 ~]# grep "r..t" /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
11:operator:x:11:0:operator:/root:/sbin/nologin
14:ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
(2)* :一個單字符後緊跟*,匹配0或個多個此單字符。(就是這個字符能夠出現屢次,也能夠不出現。)
(3).* ; 表示任意長度的任意字符
[root@centos5 ~]# cat abc
a
b
ab
aab
acb
adb
amnb
匹配a開頭b結尾,中間是任意字符的行
[root@centos5 ~]# grep "a.*b" abc
ab
aab
acb
adb
amnb
其中ab中間沒有字符也會被匹配到。任意長度的任意字符也包括空格
(4)a\{n\} : 用來匹配前面 a 的出現次數。n 爲次數。
[root@centos5 ~]# cat abc
ab
aab
aaab
aaaab
aaaaaaab
[root@centos5 ~]# grep --color "a\{1\}b" abc
ab
aab
aaab
aaaab
aaaaab
a只匹配了一次
(5)a\{n,\} : 用來匹配前面 a 的出現次數。可是次數至少爲n次。
[root@centos5 ~]# grep "a\{1,\}b" abc
ab
aab
aaab
aaaab
aaaaab
a至少出現1次,出現屢次也能夠被匹配到
(6)a\{n,m\} :用來匹配前面 a 的出現次數。可是a出現的次數在 n 和 m 之間
[root@centos5 ~]# grep "a\{1,3\}b" abc
ab
aab
aaab
aaaab
aaaaaaab
此次匹配到的內容,只匹配了a最多3次
3、正則中的字符集合
[:digit:] 表示數字
[:lower:] 表示小寫字母
[:upper:] 表示大寫字符
[:alpha:] 表示全部字符
[:punct:] 表示標點符號
[:alnum:] 表示全部數字和字符
在使用這些字符集合的時候,中括號外邊必須在加一對中括號
例子:[root@centos5 ~]# cat test
a
ab
abc
12
123
1234
a1
ab12
abc123
Abc
ABC123
要求1:過濾包含數字的行
[root@centos5 ~]# grep "[[:digit:]]" test
12
123
1234
a1
ab12
abc123
ABC123
或是用這種方法也能夠
grep -E "[0-9]" test
要求2:包含字符的行
[root@centos5 ~]# grep "[[:alpha:]]" test
a
ab
abc
a1
ab12
abc123
Abc
ABC123
或是用這種方法也能夠
grep -E "[a-z]|[A-Z]" test
在腳本中應用
#!/bin/bash
read -p "請輸入內容: " a
if [[ $a =~ [a-z]|[A-Z] ]];then
echo "字符"
else
echo "非字符"
fi
用字符集合的話將[a-z]|[A-Z]替換成[[:alpha:]]
#!/bin/bash
read -p "請輸入內容: " a
if [[ $a =~ [[:alpha:]] ]];then
echo "字符"
else
echo "非字符"
fi