正則表達式多用法

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

1 案例1:字符串截取及切割
1.1 問題正則表達式

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

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

參考PPT示範操做,完成子串截取、替換等操做
根據課上的批量更名腳本,編寫改進版renfilex.sh:可以批量修改當前目錄下全部文件的擴展名,修改前/後的擴展名經過位置參數$一、$2提供

1.2 方案app

子串截取的三種用法:運維

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

子串替換的兩種用法:dom

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

字符串掐頭去尾:ssh

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

1.3 步驟socket

實現此案例須要按照以下步驟進行。ide

步驟一:字符串的截取工具

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

截取不連續的字符,好比第三、五、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)改進版腳本(批量修改擴展名)

經過位置變量 $一、$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 root@192.168.4.5                              //鏈接目標主機
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.
root@192.168.4.5'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 root@192.168.4.5                              //鏈接目標主機
root@192.168.4.5'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
相關文章
相關標籤/搜索