1. 程式人生 > >正則表達式多用法

正則表達式多用法

.sh auth 識字 conf 網卡 rom service 字串 abcd

案例1:字符串截取及切割 案例2:字符串初值的處理 案例3:expect預期交互 案例4:使用正則表達式

1 案例1:字符串截取及切割
1.1 問題

使用Shell完成各種Linux運維任務時,一旦涉及到判斷、條件測試等相關操作時,往往需要對相關的命令輸出進行過濾,提取出符合要求的字符串。

本案例要求熟悉字符串的常見處理操作,完成以下任務練習:

參考PPT示範操作,完成子串截取、替換等操作
根據課上的批量改名腳本,編寫改進版renfilex.sh:能夠批量修改當前目錄下所有文件的擴展名,修改前/後的擴展名通過位置參數$1、$2提供

1.2 方案

子串截取的三種用法:

${變量名:起始位置:長度}
expr substr "$變量名" 起始位置 長度
echo $變量名 | cut -b 起始位置-結束位置

子串替換的兩種用法:

只替換第一個匹配結果:${變量名/old/new}
替換全部匹配結果:${變量名//old/new}

字符串掐頭去尾:

從左向右,最短匹配刪除:${變量名#*關鍵詞}
從左向右,最長匹配刪除:${變量名##*關鍵詞}
從右向左,最短匹配刪除:${變量名%關鍵詞*}
從右向左,最長匹配刪除:${變量名%%關鍵詞*}

1.3 步驟

實現此案例需要按照如下步驟進行。

步驟一:字符串的截取

1)方法一,使用 ${}表達式

格式:${變量名:起始位置:長度}

使用${}方式截取字符串時,起始位置是從0開始的。

定義一個變量phone,並確認其字符串長度:

[root@svr5 ~]# phone="13788768897"
[root@svr5 ~]# echo ${#phone}
11                                         //包括11個字符

使用${}截取時,起始位置可以省略,省略時從第一個字符開始截。比如,以下操作都可以從左側開始截取前6個字符:

[root@svr5 ~]# echo ${phone:0:6}
137887

或者

[root@svr5 ~]# echo ${phone::6}
137887

因此,如果從起始位置1開始截取6個字符,那就變成這個樣子了:

[root@svr5 ~]# echo ${phone:1:6}
378876

2)方法二,使用 expr substr

格式:expr substr "$變量名" 起始位置 長度

還以前面的phone變量為例,確認原始值:

[root@svr5 ~]# echo $phone
13788768897

使用expr substr截取字符串時,起始編號從1開始,這個要註意與${}相區分。

從左側截取phone變量的前6個字符:

[root@svr5 ~]# expr substr "$phone" 1 6
137887

從左側截取phone變量,從第9個字符開始,截取3個字符:

[root@svr5 ~]# expr substr "$phone" 9 3
897

3)方式三,使用cut分割工具

格式:echo $變量名 | cut -b 起始位置-結束位置

選項 -b 表示按字節截取字符,其中起始位置、結束位置都可以省略。當省略起始位置時,視為從第1個字符開始(編號也是從1開始,與expr類似),當省略結束位置時,視為截取到最後。

還以前面的Phone變量為例,確認原始值:

[root@svr5 ~]# echo $phone
13788768897

從左側截取前6個字符,可執行以下操作:

[root@svr5 ~]# echo $phone | cut -b 1-6
137887

從第8個字符截取到末尾:

[root@svr5 ~]# echo $phone | cut -b 8-
8897

只截取單個字符,比如第9個字符:

[root@svr5 ~]# echo $phone | cut -b 9
8

截取不連續的字符,比如第3、5、8個字符:

[root@svr5 ~]# echo $phone | cut -b 3,5,8
788

4)一個隨機密碼的案例

版本1:

[root@svr5 ~]# vim rand.sh
#!/bin/bash
x=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
//所有密碼的可能性是26+26+10=62(0-61是62個數字)
num=$[RANDOM%62]
pass=${x:num:1}

版本2:

[root@svr5 ~]# vim rand.sh
#!/bin/bash
x=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
//所有密碼的可能性是26+26+10=62(0-61是62個數字)
pass=‘‘
for i in {1..8}
do
num=$[RANDOM%62]
tmp=${x:num:1}
pass=${pass}$tmp
done
echo $pass

步驟二:字符串的替換

1)只替換第1個子串

格式:${變量名/old/new}

還以前面的phone變量為例,確認原始值:

[root@svr5 ~]# echo $phone
13788768897

將字符串中的第1個8替換為X:

[root@svr5 ~]# echo  ${phone/8/X}
137X8768897

2)替換全部子串

格式:${變量名//old/new}

將phone字符串中的所有8都替換為X:

[root@svr5 ~]# echo  ${phone//8/X}
137XX76XX97

步驟三:字符串的匹配刪除

以處理系統默認的賬戶信息為例,定義變量A:

[root@svr5 ~]# A=`head -1 /etc/passwd`
[root@svr5 ~]# echo $A
root:x:0:0:root:/root:/bin/bash

1)從左向右,最短匹配刪除

格式:${變量名#*關鍵詞}

刪除從左側第1個字符到最近的關鍵詞“:”的部分,* 作通配符理解:

[root@svr5 ~]# echo ${A#*:}
x:0:0:root:/root:/bin/bash

2)從左向右,最長匹配刪除

格式:${變量名##*關鍵詞}

刪除從左側第1個字符到最遠的關鍵詞“:”的部分:

[root@svr5 ~]# echo $A                      //確認變量A的值
root:x:0:0:root:/root:/bin/bash
[root@svr5 ~]# echo ${A##*:}
/bin/bash

3)從右向左,最短匹配刪除

格式:${變量名%關鍵詞*}

刪除從右側最後1個字符到往左最近的關鍵詞“:”的部分,* 做通配符理解:

[root@svr5 ~]# echo ${A%:*}
root:x:0:0:root:/root

4)從右向左,最長匹配刪除

格式:${變量名%%關鍵詞*}

刪除從右側最後1個字符到往左最遠的關鍵詞“:”的部分:

[root@svr5 ~]# echo ${A%%:*}
root

步驟四:編寫renfilex.sh腳本

創建一個測試用的測試文件

[root@svr5 ~]# mkdir rendir
[root@svr5 ~]# cd rendir
[root@svr5 rendir]# touch {a,b,c,d,e,f,g,h,i}.doc
[root@svr5 rendir]# ls
a.doc  b.doc  c.doc  d.doc  e.doc  f.doc  g.doc  h.doc  i.doc

1)批量修改文件擴展模的腳本

腳本用途為:批量修改當前目錄下的文件擴展名,將.doc改為.txt。

腳本內容參考如下:

[root@svr5 rendir]# vim renfile.sh
#!/bin/bash
for i in `ls *.doc`            #註意這裏有反引號
do
    mv $i  ${i%.*}.txt
done
[root@svr5 ~]# chmod +x renfile.sh

測試腳本:

[root@svr5 rendir]# ./renfile.sh
[root@svr5 rendir]# ls
a.txt  b.txt  c.txt  d.txt  e.txt  f.txt  g.txt  h.txt  i.txt

2)改進版腳本(批量修改擴展名)

通過位置變量 $1、$2提供更靈活的腳本,改進的腳本編寫參考如下:

[root@svr5 rendir]# vim ./renfile.sh
#!/bin/bash
#version:2
for i in `ls *.$1`
do
    mv $i  ${i%.*}.$2
done

3)驗證、測試改進後的腳本

將 *.doc文件的擴展名改為.txt:

[root@svr5 rendir]# ./renfile.sh txt doc

將 *.doc文件的擴展名改為.mp4:

[root@svr5 rendir]# ./renfile.sh doc mp4

2 案例2:字符串初值的處理
2.1 問題

本案例要求編寫一個腳本sumx.sh,求從1-x的和,相關要求如下:

從鍵盤讀入x值
當用戶未輸入任何值時,默認按1計算

2.2 方案

通過${var:-word}判斷變量是否存在,決定是否給變量賦初始值。
2.3 步驟

實現此案例需要按照如下步驟進行。

步驟一:認識字符串初值的最常見處理方法

1)只取值,${var:-word}

若變量var已存在且非Null,則返回 $var 的值;否則返回字串“word”,原變量var的值不受影響。

變量值已存在的情況:

[root@svr5 ~]# XX=11
[root@svr5 ~]# echo $XX              //查看原變量值
11
[root@svr5 ~]# echo ${XX:-123}      //因XX已存在,輸出變量XX的值
11

變量值不存在的情況:

[root@svr5 ~]# echo ${YY:-123}      //因YY不存在,輸出“123”
123

編寫一個驗證知識點的參考示例腳本如下:

[root@svr5 ~]# cat /root/test.sh
#!/bin/bash
read  -p   "請輸入用戶名:"   user
read  -p   "請輸入用戶名:"   pass
[ -z $user ] && exit                    //如果無用戶名,則腳本退出
pass=${pass:-123456}                    //如果用戶沒有輸入密碼,則默認密碼為123456
useradd  $user
echo "$pass"  | passwd   --stdin   $pass

步驟二:編寫sumx.sh腳本,處理read輸入的初值

用來從鍵盤讀入一個正整數x,求從1到x的和;當用戶未輸入值(直接回車)時,為了避免執行出錯,應為x賦初值1 。

1)腳本編寫參考如下

[root@svr5 ~]# vim sumx.sh
#!/bin/bash
read -p "請輸入一個正整數:" x
x=${x:-1}
i=1; SUM=0
while [ $i -le $x ]
do
    let SUM+=i
    let i++
done
echo "從1到$x的總和是:$SUM"
[root@svr5 ~]# chmod +x sumx.sh

2)驗證、測試腳本執行效果:

[root@svr5 ~]# ./sumx.sh
請輸入一個正整數:25                          //輸入25,正常讀入並計算、輸出結果
從1到25的總和是:325
[root@svr5 ~]# ./sumx.sh
請輸入一個正整數:70                         //輸入70,正常讀入並計算、輸出結果
從1到70的總和是:2485
[root@svr5 ~]# ./sumx.sh
請輸入一個正整數:                          //直接回車,設x=1後計算、輸出結果
從1到1的總和是:1

3 案例3:expect預期交互
3.1 問題

本案例要求編寫一個expect腳本,實現SSH登錄的自動交互:

提前準備好目標主機,IP地址為192.168.4.5
執行腳本後自動登入,並且在目標主機建立測試文件 /tmp/mike.txt

3.2 方案

expect可以為交互式過程(比如FTP、SSH等登錄過程)自動輸送預先準備的文本或指令,而無需人工幹預。觸發的依據是預期會出現的特征提示文本。

儲備知識(發送郵件的幾種方式):

[root@svr5 ~]# echo "test mail" | mail -s test root
[root@svr5 ~]# mail -s test root < /etc/passwd
[root@svr5 ~]# mail -s test root << EOF
test mail
hell world
EOF

3.3 步驟

實現此案例需要按照如下步驟進行。

步驟一:準備expect及SSH測試環境

1)安裝expect工具

[root@svr5 ~]# yum  -y  install  expect                  //安裝expect
.. ..
Installed:
  expect.x86_64 0:5.44.1.15-5.el6_4                                                            
Dependency Installed:
  tcl.x86_64 1:8.5.7-6.el6 
[root@svr5 ~]# which expect                              //確認expect路徑
/usr/bin/expect

步驟二:編寫腳本,實現免交互登錄

1)任務需求及思路分析

在SSH登錄過程中,如果是第一次連接到該目標主機,則首先會被要求接受密鑰,然後才提示輸入密碼:

註意:不要照抄這裏的IP地址,需要根據自己的實際IP填寫!!!

[root@svr5 ~]# ssh [email protected]                              //連接目標主機
The authenticity of host ‘192.168.4.5 (192.168.4.5)‘ can‘t be established.
RSA key fingerprint is 58:a0:d6:00:c7:f1:34:5d:6c:6d:70:ce:e0:20:f8:f3.
Are you sure you want to continue connecting (yes/no)? yes          //接受密鑰
Warning: Permanently added ‘192.168.4.5‘ (RSA) to the list of known hosts.
[email protected]‘s password:                                   //驗證密碼
Last login: Thu May  7 22:05:44 2015 from 192.168.4.5
[root@svr5 ~]$ exit                                             //返回客戶端
logout
Connection to 192.168.4.5 closed.

當然,如果SSH登錄並不是第一次,則接受密鑰的環節就沒有了,而是直接進入驗證密碼的過程:

註意:不要照抄這裏的IP地址,需要根據自己的實際IP填寫!!!

[root@svr5 ~]# ssh [email protected]                              //連接目標主機
[email protected]‘s password:                                   //驗證密碼
Last login: Mon May 11 12:02:39 2015 from 192.168.4.5
[root@svr5 ~]$ exit                                             //返回客戶端
logout
Connection to 192.168.4.5 closed.

2)根據實現思路編寫腳本文件

腳本內容參考如下版本1:

註意:不要照抄腳本裏的IP地址與密碼,需要根據自己的實際情況填寫!!!

[root@svr5 ~]# vim  expect_ssh.sh 
#!/bin/bash
expect << EOF
spawn ssh 192.168.4.5                               #//創建交互式進程
expect "password:" { send "123456\r" }              #//自動發送密碼
expect "#"          { send "touch /tmp.txt\r" }     #//發送命令
expect "#"          { send "exit\r" }
EOF
[root@svr5 ~]# chmod  +x  expect_ssh.sh 

通過循環批量操作,版本2:

註意:不要照抄腳本裏的IP地址與密碼,需要根據自己的實際情況填寫!!!

[root@svr5 ~]# vim  expect_ssh.sh 
#!/bin/bash
for i in 10 11                                        #註意IP根據實際情況填寫
do
expect << EOF
spawn ssh 192.168.4.$i                               #//創建交互式進程
expect "password:" { send "123456\r" }              #//自動發送密碼
expect "#"          { send "touch /tmp.txt\r" }      #//發送命令
expect "#"          { send "exit\r" }
EOF
done
[root@svr5 ~]# chmod  +x  expect_ssh.sh 

註意事項:

expect腳本的最後一行默認不執行

如果不希望ssh時出現yes/no的提示,遠程時使用如下選項:# ssh -o StrictHostKeyChecking=no server0
4 案例4:使用正則表達式
4.1 問題

本案例要求熟悉正則表達式的編寫,完成以下任務:

利用egrep工具練習正則表達式的基本用法

4.2 方案

表-1 基本正則列表

表-1 擴展正則列表
4.3 步驟

實現此案例需要按照如下步驟進行。

步驟一:正則表達式匹配練習

1)典型的應用場合:grep、egrep檢索文本行

使用不帶-E選項的grep命令時,支持基本正則匹配模式。比如“word”關鍵詞檢索、“^word”匹配以word開頭的行、“word$”匹配以word結尾的行……等等。

輸出以“r”開頭的用戶記錄:

[root@svr5 ~]# grep ‘^r‘ /etc/passwd
root:x:0:0:root:/root:/bin/bash
rpc:x:32:32:Portmapper RPC user:/:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin

輸出以“localhost”結尾的行:

[root@svr5 ~]# grep ‘localhost$‘ /etc/hosts
127.0.0.1               localhost.localdomain localhost

若希望在grep檢索式同時組合多個條件,比如輸出以“root”或者以“daemon”開頭的行,這時候基本正則就不太方便了(“或者”必須轉義為“|”):

[root@svr5 ~]# grep ‘^root|^daemon‘ /etc/passwd          //搜索無結果
[root@svr5 ~]#
[root@svr5 ~]# grep ‘^root\|^daemon‘ /etc/passwd          //正確獲得結果
root:x:0:0:root:/root:/bin/bash
daemon:x:2:2:daemon:/sbin:/sbin/nologin

而若若使用grep -E或egrep命令,可支持擴展正則匹配模式,能夠自動識別 |、{ 等正則表達式中的特殊字符,用起來更加方便,比如:

[root@svr5 ~]# grep -E ‘^root|^daemon‘ /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:2:2:daemon:/sbin:/sbin/nologin

或者

[root@svr5 ~]# egrep ‘^root|^daemon‘ /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:2:2:daemon:/sbin:/sbin/nologin

使用grep -E 與 使用egrep命令完全等效,推薦使用後者,特別是涉及到復雜的正則表達式的時候。

2)grep、egrep命令的-q選項

選項 -q 表示 quiet(靜默)的意思,結合此選項可以只做檢索而並不輸出,通常在腳本內用來識別查找的目標是否存在,通過返回狀態 $? 來判斷,這樣可以忽略無關的文本信息,簡化腳本輸出。

比如,檢查/etc/hosts文件內是否存在192.168.4.4的映射記錄,如果存在則顯示“YES”,否則輸出“NO”,一般會執行:

[root@svr5 ~]# grep ‘^192.168.4.4‘ /etc/hosts && echo "YES" || echo "NO"
192.168.4.4     svr5.tarena.com svr5
YES

這樣grep的輸出信息和腳本判斷後的提示混雜在一起,用戶不易辨別,所以可以改成以下操作:

[root@svr5 ~]# grep -q ‘^192.168.4.4‘ /etc/hosts && echo "YES" || echo "NO"
YES

是不是清爽多了,從上述結果也可以看到,使用 -q 選項的效果與使用 &> /dev/null的效果類似。

3)基本元字符 ^、$ —— 匹配行首、行尾

輸出默認運行級別的配置記錄(以id開頭的行):

[root@svr5 ~]# egrep ‘^id‘ /etc/inittab
id:3:initdefault:

輸出主機名配置記錄(以HOSTNAME開頭的行):

[root@svr5 ~]# egrep ‘^HOSTNAME‘ /etc/sysconfig/network
HOSTNAME=svr5.tarena.com

統計本地用戶中登錄Shell為“/sbin/nologin”的用戶個數:

[root@svr5 ~]# egrep -m10 ‘/sbin/nologin$‘ /etc/passwd  //先確認匹配正確
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
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
[root@svr5 ~]# egrep -c ‘/sbin/nologin$‘ /etc/passwd
32                                      //結合 -c 選項輸出匹配的行數

使用 -c 選項可輸出匹配行數,這與通過管道再 wc -l的效果是相同的,但是寫法更簡便。比如,統計使用“/bin/bash”作為登錄Shell的正常用戶個數,可執行:

[root@svr5 ~]# egrep -c ‘/bin/bash$‘ /etc/passwd
26

或者

[root@svr5 ~]# egrep ‘/bin/bash$‘ /etc/passwd | wc -l
26

4)基本元字符 . —— 匹配任意單個字符

以/etc/rc.local文件為例,確認文本內容:

[root@svr5 ~]# cat /etc/rc.local
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don‘t
# want to do the full Sys V style init stuff.
touch /var/lock/subsys/local

輸出/etc/rc.local文件內至少包括一個字符(\n換行符除外)的行,即非空行:

[root@svr5 ~]# egrep ‘.‘ /etc/rc.local
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don‘t
# want to do the full Sys V style init stuff.
touch /var/lock/subsys/local

輸出/etc/rc.local文件內的空行(用 –v 選項將條件取反):

[root@svr5 ~]# egrep -v ‘.‘ /etc/rc.local
[root@svr5 ~]#

上述取空行的操作與下列操作效果相同:

[root@svr5 ~]# egrep ‘^$‘ /etc/rc.local
[root@svr5 ~]#

5)基本元字符 +、?、* —— 目標出現的次數

還以/etc/rc.local文件為例:

[root@svr5 ~]# cat /etc/rc.local
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don‘t
# want to do the full Sys V style init stuff.
touch /var/lock/subsys/local

輸出包括 f、ff、ff、……的行,即“f”至少出現一次:

[root@svr5 ~]# egrep ‘f+‘ /etc/rc.local
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don‘t
# want to do the full Sys V style init stuff.

輸出包括init、initial的行,即末尾的“ial”最多出現一次(可能沒有):

[root@svr5 ~]# egrep ‘init(ial)?‘ /etc/rc.local
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don‘t
# want to do the full Sys V style init stuff.

輸出包括stu、stuf、stuff、stufff、……的行,即末尾的“f”可出現任意多次,也可以沒有。重復目標只有一個字符時,可以不使用括號:

[root@svr5 ~]# egrep ‘stuf*‘ /etc/rc.local
# You can put your own initialization stuff in here if you don‘t
# want to do the full Sys V style init stuff.

輸出所有行,單獨的“.*”可匹配任意行(包括空行):

[root@svr5 ~]# egrep ‘.*‘ /etc/rc.local
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don‘t
# want to do the full Sys V style init stuff.
touch /var/lock/subsys/local

輸出/etc/passwd文件內“r”開頭且以“nologin”結尾的用戶記錄,即中間可以是任意字符:

[root@svr5 ~]# egrep ‘^r.*nologin$‘ /etc/passwd
rpc:x:32:32:Portmapper RPC user:/:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin

6)元字符 {} —— 限定出現的次數範圍

創建一個練習用的測試文件:

[root@svr5 ~]# vim brace.txt
ab def ghi abdr
dedef abab ghighi
abcab CD-ROM
TARENA IT GROUP
cdcd ababab
Hello abababab World

輸出包括ababab的行,即“ab”連續出現3次:

[root@svr5 ~]# egrep ‘(ab){3}‘ brace.txt
cdcd ababab
Hello abababab World

輸出包括abab、ababab、abababab的行,即“ab”連續出現2~4次:

[root@svr5 ~]# egrep ‘(ab){2,4}‘ brace.txt
dedef abab ghighi
cdcd ababab
Hello abababab World

輸出包括ababab、abababab、……的行,即“ab”最少連續出現3次:

[root@svr5 ~]# egrep ‘(ab){3,}‘ brace.txt
cdcd ababab
Hello abababab World

7)元字符 [] —— 匹配範圍內的單個字符

還以前面的測試文件bracet.txt為例:

[root@svr5 ~]# cat brace.txt
ab def ghi abdr
dedef abab ghighi
abcab CD-ROM
TARENA IT GROUP
cdcd ababab
Hello abababab World

輸出包括abc、abd的行,即前兩個字符為“ab”,第三個字符只要是c、d中的一個就符合條件:

[root@svr5 ~]# egrep ‘ab[cd]‘ brace.txt
ab def ghi abdr
abcab CD-ROM

輸出包括大寫字母的行,使用[A-Z]匹配連續範圍:

[root@svr5 ~]# egrep ‘[A-Z]‘ brace.txt
abcab CD-ROM
TARENA IT GROUP
Hello abababab World

輸出包括“非空格也非小寫字母”的其他字符的行,本例中大寫字母和 – 符合要求:

[root@svr5 ~]# egrep ‘[^ a-zA-Z]‘ brace.txt
abcab CD-ROM

8)單詞邊界匹配

以文件/etc/rc.local為例:

[root@svr5 ~]# cat /etc/rc.local
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don‘t
# want to do the full Sys V style init stuff.
touch /var/lock/subsys/local

輸出包括單詞“init”的行,文件中“initialization”不合要求:

[root@svr5 ~]# egrep ‘\binit\b‘ /etc/rc.local
# This script will be executed *after* all the other init scripts.
# want to do the full Sys V style init stuff.

或者:

[root@svr5 ~]# egrep ‘\<init\>‘ /etc/rc.local
# This script will be executed *after* all the other init scripts.
# want to do the full Sys V style init stuff.

輸出包括以“ll”結尾的單詞的行,使用 \> 匹配單詞右邊界:

[root@svr5 ~]# egrep ‘ll\>‘ /etc/rc.local
# This script will be executed *after* all the other init scripts.
# want to do the full Sys V style init stuff.

或者:

[root@svr5 ~]# egrep ‘ll\b‘ /etc/rc.local
# This script will be executed *after* all the other init scripts.
# want to do the full Sys V style init stuff.

9)多個條件的組合

通過dmesg啟動日誌查看藍牙設備、網卡設備相關的信息:

[root@svr5 ~]# egrep -i ‘eth|network|bluetooth‘ /var/log/dmesg
Initalizing network drop monitor service
Bluetooth: Core ver 2.10
Bluetooth: HCI device and connection manager initialized
Bluetooth: HCI socket layer initialized
Bluetooth: HCI USB driver ver 2.9
Intel(R) PRO/1000 Network Driver - version 7.3.21-k4-3-NAPI
e1000: eth0: e1000_probe: Intel(R) PRO/1000 Network Connection

正則表達式多用法