總結基本正則表達式及擴展正則表達式
基本正則表達式的元字符:
字符匹配
:
.: 匹配任意單個字符;
[]:匹配指定範圍內的任意單個字符;
[^]:匹配指定範圍外的任意單個字符;
[:lower:], [:upper:], ...
次數匹配
:用於要指定其次數的字符的後面;
*: 任意次;
\?:0或1次;
grep "x\?y"
\+:1或多次;
\{m\}:精確限制爲m次;
\{m,n\}: 至少m次,至多n次,[m,n]
\{0,n\}:至多n次;
\{m,\}:至少m次;
.*: 匹配任意長度的任意字符;
位置錨定:
^: 行首錨定;用於模式的最左側;
$: 行尾錨定;用於模式的最右側;
\<, \b: 詞首錨定;用於表示單詞的模式的左側;
\>, \b:詞尾錨定;用於表示單詞的模式的右側;
^$: 空白行;
分組:\(\)
分組的小括號中的模式匹配到的內容,會在執行過程中被正則表達式引擎記錄下來,並保存內置的變量中;這些變量分別是\1, \2, ...
\1: 從左側起,第一個左括號,以及與之配對的右括號中間的模式所匹配到的內容;
\2:如上類推
...
後向引用:使用變量引用前面的分組括號中的模式所匹配到的字符;
擴展的正則表達式:
grep家庭有三個命令:
grep:基本正則表達式
-E: 擴展正則表達式
-F:不支持正則表達式
egrep:擴展正則表達式
fgrep:不支持正則表達式
擴展正則表達式的元字符
:
字符匹配:
.: 任意單個字符
[]:
[^]:
次數匹配:
*
?: 0次或1次;
+: 1次以上;
{m}: 精確匹配m次;
{m,n}: 至少m次,至多n次;
錨定:
^: 錨定行首
$: 錨定行尾
\<:詞首錨定
\>:詞尾錨定
分組:()
後向引用:\1, \2, ...
或者:
a|b
C|cat: 不表示Cat或cat,而表示C或cat;
要寫成(C|c)at
練習:1、顯示/etc/passwd文件中以bash結尾的行。
cat /etc/passwd |grep 'bash$'
2、顯示/etc/passwd文件中的兩位數或三位數。
cat /etc/passwd |grep '[[:digit:]]\{2,3}\'
3、顯示`netstat -tan`命令結果中以‘LISTEN’後跟0個、1個或者多個空白字符結尾的行。
cat /etc/passwd |grep 'LISTEN[[:space:]]\{0,\}$'
4、添加用戶bash、testbash、basher以及nologin用戶(nologin用戶的shell爲/sbin/nologin);而後找出/etc/passwd文件中用戶名與其shell名相同的行。
useradd bash
useradd testbash
useradd basher
useradd nologin -s /bin/nologin
[root@bogon ~]
# grep '\(bash\).*\1' /etc/passwd
bash
:x:500:506::
/home/bash
:
/bin/bash
testbash:x:501:507::
/home/testbash
:
/bin/bash
basher:x:502:508::
/home/basher
:
/bin/bash
[root@bogon ~]
# grep '^\(nologin\).*\1' /etc/passwd
nologin:x:503:509::
/home/nologin
:
/sbin/nologin
(注:這一部分沒搞明白,是抄別人的)
5、顯示當前系統上root、centos或者user1用戶的默認shell和UID (請事先創建這些用戶,若不存在)
useradd centos
[root@bogon ~]
# grep -E '^root|^centos|^user1' /etc/passwd | cut -d: -f1,3,7
root:0:
/bin/bash
centos:504:
/bin/bash
user1:505:
/bin/bash
(注:這一部分也沒搞明白,是抄別人的)
6、找出/etc/rc.d/init.d/functions文件中某單詞(單詞中間可以存在下劃線)後面跟着一組小括號的行。
grep -n '^[[:alpha:]].*()' /etc/rc.d/init.d/functions
執行結果如下:
25:systemctl_redirect () {
97:checkpid() {
143:daemon() {
233:killproc() {
325:pidfileofproc() {
340:pidofproc() {
366:status() {
426:echo_success() {
437:echo_failure() {
448:echo_passed() {
459:echo_warning() {
471:update_boot_stage() {
479:success() {
485:failure() {
493:passed() {
500:warning() {
507:action() {
520:strstr() {
526:is_ignored_file() {
536:is_true() {
546:is_false() {
556:apply_sysctl() {
7、使用echo輸出一個路徑,而後egrep找出其路徑基名;進一步的使用egrep取出其目錄名。
echo
"/etc/sysconfig/network"
|
egrep
-o
'\<network\>'
network
echo
"/etc/sysconfig/network"
|
egrep
-o
'/etc/sysconfig/'
/etc/sysconfig/
(注:此部分沒搞明白,抄的)
8.找出ifconfig命令執行結果中1-255之間的數字。
ifconfig |grep -E '\<([1-9]|[1-9][0-9]|[1][0-9][0-9]|2[0-4][0-9]|25[0-5])\>'
執行結果:
inet 192.168.80.134 netmask 255.255.255.0 broadcast 192.168.80.255
inet6 fe80::20c:29ff:fe36:92b4 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:36:92:b4 txqueuelen 1000 (Ethernet)
TX packets 2814 bytes 479932 (468.6 KiB)
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
RX packets 48 bytes 4176 (4.0 KiB)
TX packets 48 bytes 4176 (4.0 KiB)