shell編程(六)

1、sed介紹
1.sed用來作啥?
2.sed如何處理文件?
2、sed使用方法介紹
1.命令行格式
1)語法格式
2)舉例說明
2.腳本格式
1)用法
2)注意事項
3)舉例說明
3.補充擴展總結
3、課堂練習
4、課後實戰mysql

1、sed介紹

1. sed用來作啥?

sed是Stream Editor(流編輯器)的縮寫,簡稱流編輯器;用來==處理文件==的。linux

2. sed如何處理文件?

sed是==一行一行讀取==文件內容並==按照要求==進行==處理==,把處理後的結果==輸出到屏幕==。正則表達式

sed

  1. 首先sed讀取文件中的一行內容,把其保存在一個==臨時緩存區中==(也稱爲模式空間)
  2. 而後==根據需求==處理臨時緩衝區中的行,完成後把該行==發送到屏幕上==

總結:sql

  1. 因爲sed把每一行都存在臨時緩衝區中,對這個副本進行編輯,因此==不會直接修改原文件==
  2. Sed主要用來自動編輯一個或多個文件;簡化對文件的反覆操做,對文件進行過濾和轉換操做

2、sed使用方法介紹

sed常見的語法格式有兩種,一種叫==命令行==模式,另外一種叫==腳本==模式。shell

1. 命令行格式

1)語法格式

sed [options] =='==處理動做=='== 文件名vim

  • 經常使用選項
選項 說明 備註
-e 進行多項(屢次)編輯
==-n== 取消默認輸出 不自動打印模式空間
==-r== 使用擴展==正則表達式==
==-i== 原地編輯(修改源文件)
-f 指定sed腳本的文件名
  • ==常見處理動做==

醜話說在前面:如下全部的==動做==都要在單引號裏,緩存

動做 說明 備註
'p' 打印
'i' 在指定行==以前==插入內容 相似vim裏的大寫O
'a' 在指定行==以後==插入內容 相似vim裏的小寫o
'c' 替換指定行全部內容
'd' 刪除指定行

2)舉例說明

  • 文件準備
# vim a.txt 
root:x:0:0:root:/root:/bin/bash
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
298374837483
172.16.0.254
10.1.1.1

① 對文件進行==增、刪、改、查==操做

語法:sed 選項 =='==定位+命令=='== 須要處理的文件bash

1)打印文件內容
[root@server ~]# sed ''  a.txt                      對文件什麼都不作
[root@server ~]# sed -n 'p'  a.txt                  打印每一行,並取消默認輸出
[root@server ~]# sed -n '1p'  a.txt                 打印第1行
[root@server ~]# sed -n '2p'  a.txt                 打印第2行
[root@server ~]# sed -n '1,5p'  a.txt               打印1到5行
[root@server ~]# sed -n '$p' a.txt                  打印最後1行
2)增長文件內容

i 地址定位的上面插入網絡

a 下面插入編輯器

[root@server ~]# sed '$a99999' a.txt                文件最後一行下面增長內容
[root@server ~]# sed 'a99999' a.txt                 文件每行下面增長內容
[root@server ~]# sed '5a99999' a.txt                文件第5行下面增長內容
[root@server ~]# sed '$i99999' a.txt                文件最後一行上一行增長內容
[root@server ~]# sed 'i99999' a.txt                 文件每行上一行增長內容
[root@server ~]# sed '6i99999' a.txt                文件第6行上一行增長內容
[root@server ~]# sed '/^uucp/ihello'                以uucp開頭行的上一行插入內容
3)修改文件內容

c 替換指定的==整行==內容

[root@server ~]# sed '5chello world' a.txt      替換文件第5行內容
[root@server ~]# sed 'chello world' a.txt       替換文件全部內容
[root@server ~]# sed '1,5chello world' a.txt    替換文件1到5號內容爲hello world
[root@server ~]# sed '/^user01/c888888' a.txt   替換以user01開頭的行
4)刪除文件內容
[root@server ~]# sed '1d' a.txt                         刪除文件第1行
[root@server ~]# sed '1,5d' a.txt                   刪除文件1到5行
[root@server ~]# sed '$d' a.txt                     刪除文件最後一行

② ==對文件進行搜索替換操做==

語法:sed 選項 '==s/搜索的內容/替換的內容/動做==' 須要處理的文件

其中,==s==表示search搜索;斜槓==/==表示分隔符,能夠本身定義;動做通常是打印==p==和全局替換==g==

[root@server ~]# sed -n 's/root/ROOT/p' 1.txt 
[root@server ~]# sed -n 's/root/ROOT/gp' 1.txt 
[root@server ~]# sed -n 's/^#//gp' 1.txt 
[root@server ~]# sed -n 's@/sbin/nologin@itcast@gp' a.txt
[root@server ~]# sed -n 's/\/sbin\/nologin/itcast/gp' a.txt
[root@server ~]# sed -n '10s#/sbin/nologin#itcast#p' a.txt 
uucp:x:10:14:uucp:/var/spool/uucp:itcast
[root@server ~]# sed -n 's@/sbin/nologin@itcastheima@p' 2.txt 
注意:搜索替換中的分隔符能夠本身指定

[root@server ~]# sed -n '1,5s/^/#/p' a.txt      註釋掉文件的1-5行內容
#root:x:0:0:root:/root:/bin/bash
#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

③ 其餘命令

命令 解釋 備註
r 從另外文件中讀取內容
w 內容另存爲
& 保存查找串以便在替換串中引用 和\(\)相同
= 打印行號
對所選行之外的全部行應用命令,放到行數以後 '1,5!'
q 退出

舉例說明:

r   從文件中讀取輸入行
w   將所選的行寫入文件
[root@server ~]# sed '3r /etc/hosts' 2.txt 
[root@server ~]# sed '$r /etc/hosts' 2.txt
[root@server ~]# sed '/root/w a.txt' 2.txt 
[root@server ~]# sed '/[0-9]{4}/w a.txt' 2.txt
[root@server ~]# sed  -r '/([0-9]{1,3}\.){3}[0-9]{1,3}/w b.txt' 2.txt

!   對所選行之外的全部行應用命令,放到行數以後
[root@server ~]# sed -n '1!p' 1.txt 
[root@server ~]# sed -n '4p' 1.txt 
[root@server ~]# sed -n '4!p' 1.txt 
[root@server ~]# cat -n 1.txt 
[root@server ~]# sed -n '1,17p' 1.txt 
[root@server ~]# sed -n '1,17!p' 1.txt 

&   保存查找串以便在替換串中引用   \(\)

[root@server ~]# sed -n '/root/p' a.txt 
root:x:0:0:root:/root:/bin/bash
[root@server ~]# sed -n 's/root/#&/p' a.txt 
#root:x:0:0:root:/root:/bin/bash

# sed -n 's/^root/#&/p' passwd   註釋掉以root開頭的行
# sed -n -r 's/^root|^stu/#&/p' /etc/passwd 註釋掉以root開頭或者以stu開頭的行
# sed -n '1,5s/^[a-z].*/#&/p' passwd  註釋掉1~5行中以任意小寫字母開頭的行
# sed -n '1,5s/^/#/p' /etc/passwd  註釋1~5行
或者
sed -n '1,5s/^/#/p' passwd 以空開頭的加上#
sed -n '1,5s/^#//p' passwd 以#開頭的替換成空

[root@server ~]# sed -n '/^root/p' 1.txt 
[root@server ~]# sed -n 's/^root/#&/p' 1.txt 
[root@server ~]# sed -n 's/\(^root\)/#\1/p' 1.txt 
[root@server ~]# sed -nr '/^root|^stu/p' 1.txt 
[root@server ~]# sed -nr 's/^root|^stu/#&/p' 1.txt 

=   打印行號
# sed -n '/bash$/=' passwd    打印以bash結尾的行的行號
# sed -ne '/root/=' -ne '/root/p' passwd 
# sed -n '/nologin$/=;/nologin$/p' 1.txt
# sed -ne '/nologin$/=' -ne '/nologin$/p' 1.txt

q   退出
# sed '5q' 1.txt
# sed '/mail/q' 1.txt
# sed -r '/^yunwei|^mail/q' 1.txt
[root@server ~]# sed -n '/bash$/p;10q' 1.txt
ROOT:x:0:0:root:/root:/bin/bash

綜合運用:
[root@server ~]# sed -n '1,5s/^/#&/p' 1.txt 
#root:x:0:0:root:/root:/bin/bash
#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

[root@server ~]# sed -n '1,5s/\(^\)/#\1/p' 1.txt 
#root:x:0:0:root:/root:/bin/bash
#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

④ 其餘選項

-e 多項編輯
-r  擴展正則
-i 修改原文件

[root@server ~]# sed -ne '/root/p' 1.txt -ne '/root/='
root:x:0:0:root:/root:/bin/bash
1
[root@server ~]# sed -ne '/root/=' -ne '/root/p' 1.txt 
1
root:x:0:0:root:/root:/bin/bash

在1.txt文件中的第5行的前面插入「hello world」;在1.txt文件的第8行下面插入「哈哈哈哈」

[root@server ~]# sed -e '5ihello world' -e '8a哈哈哈哈哈' 1.txt  -e '5=;8='

sed -n '1,5p' 1.txt
sed -ne '1p' -ne '5p' 1.txt
sed -ne '1p;5p' 1.txt

過濾vsftpd.conf文件中以#開頭和空行:
[root@server ~]# grep -Ev '^#|^$' /etc/vsftpd/vsftpd.conf
[root@server ~]# sed -e '/^#/d' -e '/^$/d' /etc/vsftpd/vsftpd.conf
[root@server ~]# sed '/^#/d;/^$/d' /etc/vsftpd/vsftpd.conf
[root@server ~]# sed -r '/^#|^$/d' /etc/vsftpd/vsftpd.conf

過濾smb.conf文件中生效的行:
# sed -e '/^#/d' -e '/^;/d' -e '/^$/d' -e '/^\t$/d' -e '/^\t#/d' smb.conf
# sed -r '/^(#|$|;|\t#|\t$)/d' smb.conf 

# sed -e '/^#/d' -e '/^;/d' -e '/^$/d' -e '/^\t$/d' -e '/^\t#/' smb.conf

[root@server ~]# grep '^[^a-z]' 1.txt

[root@server ~]# sed -n '/^[^a-z]/p' 1.txt

過濾出文件中的IP地址:
[root@server ~]# grep -E '([0-9]{1,3}\.){3}[0-9]{1,3}' 1.txt 
192.168.0.254
[root@server ~]# sed -nr '/([0-9]{1,3}\.){3}[0-9]{1,3}/p' 1.txt 
192.168.0.254

[root@server ~]# grep -o -E '([0-9]{1,3}\.){3}[0-9]{1,3}' 2.txt 
10.1.1.1
10.1.1.255
255.255.255.0

[root@server ~]# sed -nr '/([0-9]{1,3}\.){3}[0-9]{1,3}/p' 2.txt
10.1.1.1
10.1.1.255
255.255.255.0
過濾出ifcfg-eth0文件中的IP、子網掩碼、廣播地址
[root@server shell06]# grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}' ifcfg-eth0 
10.1.1.1
255.255.255.0
10.1.1.254
[root@server shell06]# sed -nr '/([0-9]{1,3}\.){3}[0-9]{1,3}/p' ifcfg-eth0|cut -d'=' -f2
10.1.1.1
255.255.255.0
10.1.1.254
[root@server shell06]# sed -nr '/([0-9]{1,3}\.){3}[0-9]{1,3}/p' ifcfg-eth0|sed -n 's/[A-Z=]//gp'
10.1.1.1
255.255.255.0
10.1.1.254

[root@server shell06]# ifconfig eth0|sed -n '2p'|sed -n 's/[:a-Z]//gp'|sed -n 's/ /\n/gp'|sed '/^$/d'
10.1.1.1
10.1.1.255
255.255.255.0
[root@server shell06]# ifconfig | sed -nr '/([0-9]{1,3}\.)[0-9]{1,3}/p' | head -1|sed -r 's/([a-z:]|[A-Z/t])//g'|sed 's/ /\n/g'|sed  '/^$/d'

[root@server shell06]# ifconfig eth0|sed -n '2p'|sed -n 's/.*addr:\(.*\) Bcast:\(.*\) Mask:\(.*\)/\1\n\2\n\3/p'
10.1.1.1 
10.1.1.255 
255.255.255.0

-i 選項  直接修改原文件
# sed -i 's/root/ROOT/;s/stu/STU/' 11.txt
# sed -i '17{s/YUNWEI/yunwei/;s#/bin/bash#/sbin/nologin#}' 1.txt
# sed -i '1,5s/^/#&/' a.txt
注意:
-ni  不要一塊兒使用
p命令 不要再使用-i時使用

⑤ ==sed結合正則使用==

sed 選項 =='==sed==命令==或者==正則表達式==或者==地址定位===='== 文件名

  1. 定址用於決定對哪些行進行編輯。地址的形式能夠是數字、正則表達式、或兩者的結合。
  2. 若是沒有指定地址,sed將處理輸入文件的全部行。
正則 說明 備註
/key/ 查詢包含關鍵字的行 sed -n '/root/p' 1.txt
/key1/,/key2/ 匹配包含兩個關鍵字之間的行 sed -n '/\^adm/,/^mysql/p' 1.txt
/key/,x 從匹配關鍵字的行開始到==文件第x行==之間的行(包含關鍵字所在行) sed -n '/^ftp/,7p'
x,/key/ 從文件的第x行開始到與關鍵字的匹配行之間的行
x,y! 不包含x到y行
/key/! 不包括關鍵字的行 sed -n '/bash$/!p' 1.txt

2. 腳本格式

1)用法

# sed -f scripts.sh  file       //使用腳本處理文件
建議使用   ./sed.sh   file

腳本的第一行寫上
#!/bin/sed -f
1,5d
s/root/hello/g
3i777
5i888
a999
p

2)注意事項

1) 腳本文件是一個sed的命令行清單。'commands'
2) 在每行的末尾不能有任何空格、製表符(tab)或其它文本。
3) 若是在一行中有多個命令,應該用分號分隔。
4) 不須要且不可用引號保護命令
5) #號開頭的行爲註釋

3)舉例說明

# cat passwd
stu3:x:509:512::/home/user3:/bin/bash
stu4:x:510:513::/home/user4:/bin/bash
stu5:x:511:514::/home/user5:/bin/bash

# cat sed.sh 
#!/bin/sed -f
2a\
******************
2,$s/stu/user/
$a\
we inster new line
s/^[a-z].*/#&/

[root@server ~]# cat 1.sed 
#!/bin/sed -f
3a**********************
$chelloworld
1,3s/^/#&/

[root@server ~]# sed -f 1.sed -i 11.txt 
[root@server ~]# cat 11.txt 
#root:x:0:0:root:/root:/bin/bash
#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
helloworld

3. 補充擴展總結

一、正則表達式必須以」/「先後規範間隔
例如:sed '/root/d' file
例如:sed '/^root/d' file

二、若是匹配的是擴展正則表達式,須要使用-r選來擴展sed
grep -E
sed -r
+ ? () {n,m} | \d

注意:         
在正則表達式中若是出現特殊字符(^$.*/[]),須要之前導 "\" 號作轉義
eg:sed '/\$foo/p' file

三、逗號分隔符
例如:sed '5,7d' file                  刪除5到7行
例如:sed '/root/,/ftp/d' file 
刪除第一個匹配字符串"root"到第一個匹配字符串"ftp"的全部行本行不找 循環執行

四、組合方式
例如:sed '1,/foo/d' file          刪除第一行到第一個匹配字符串"foo"的全部行
例如:sed '/foo/,+4d' file         刪除從匹配字符串」foo「開始到其後四行爲止的行
例如:sed '/foo/,~3d' file         刪除從匹配字符串」foo「開始刪除到3的倍數行(文件中)
例如:sed '1~5d' file              從第一行開始刪每五行刪除一行
例如:sed -nr '/foo|bar/p' file    顯示配置字符串"foo"或"bar"的行
例如:sed -n '/foo/,/bar/p' file   顯示匹配從foo到bar的行
例如:sed '1~2d'  file             刪除奇數行
例如:sed '0-2d'   file                刪除偶數行 sed '1~2!d'  file

五、特殊狀況
例如:sed '$d' file                    刪除最後一行
例如:sed '1d' file                    刪除第一行

六、其餘:
sed 's/.//' a.txt                       刪除每一行中的第一個字符
sed 's/.//2' a.txt                  刪除每一行中的第二個字符
sed 's/.//N' a.txt                  從文件中第N行開始,刪除每行中第N個字符(N>2)
sed 's/.$//' a.txt                  刪除每一行中的最後一個字符

[root@server ~]# cat 2.txt 
1 a
2 b
3 c
4 d
5 e
6 f
7 u
8 k
9 o
[root@server ~]# sed '/c/,~2d' 2.txt 
1 a
2 b
5 e
6 f
7 u
8 k
9 o

3、課堂練習

  1. 將任意數字替換成空或者製表符
  2. 去掉文件1-5行中的數字、冒號、斜槓
  3. 匹配root關鍵字替換成hello itcast,並保存到test.txt文件中
  4. 刪除vsftpd.conf、smb.conf、main.cf配置文件裏全部註釋的行及空行(不要直接修改原文件)
  5. 使用sed命令截取本身的ip地址
  6. 使用sed命令一次性截取ip地址、廣播地址、子網掩碼
  7. 註釋掉文件的2-3行和匹配到以root開頭或者以ftp開頭的行
一、將文件中任意數字替換成空或者製表符
二、去掉文件1-5行中的數字、冒號、斜槓
三、匹配root關鍵字的行替換成hello itcast,並保存到test.txt文件中
四、刪除vsftpd.conf、smb.conf、main.cf配置文件裏全部註釋的行及空行(不要直接修改原文件)
五、使用sed命令截取本身的ip地址
# ifconfig eth0|sed -n '2p'|sed -n 's/.*addr://pg'|sed -n 's/Bcast.*//gp'
10.1.1.1  
# ifconfig eth0|sed -n '2p'|sed 's/.*addr://g'|sed 's/ Bcast:.*//g'
六、使用sed命令一次性截取ip地址、廣播地址、子網掩碼
# ifconfig eth0|sed -n '2p'|sed -n 's#.*addr:\(.*\) Bcast:\(.*\) Mask:\(.*\)#\1\n\2\n\3#p'
10.1.1.1 
10.1.1.255 
255.255.255.0

七、註釋掉文件的2-3行和匹配到以root開頭或者以ftp開頭的行
# sed -nr '2,3s/^/#&/p;s/^ROOT|^ftp/#&/p' 1.txt
#ROOT:x:0:0:root:/root:/bin/bash
#bin:x:1:1:bin:/bin:/sbin/nologin
#3daemon:x:2:2:daemon:/sbin:/sbin/nologin

# sed -ne '1,2s/^/#&/gp' a.txt -nre 's/^lp|^mail/#&/gp'
# sed -nr '1,2s/^/#&/gp;s/^lp|^mail/#&/gp' a.txt

4、課後實戰

一、寫一個初始化系統的腳本
1)自動修改主機名(如:ip是192.168.0.88,則主機名改成server88.itcast.cc)

a. 更改文件非交互式 sed

/etc/sysconfig/network

b.將本主機的IP截取出來賦值給一個變量ip;再而後將ip變量裏以.分割的最後一位賦值給另外一個變量ip1

2)自動配置可用的yum源

3)自動關閉防火牆和selinux

二、寫一個搭建ftp服務的腳本,要求以下:
1)不支持本地用戶登陸 local_enable=NO
2) 匿名用戶能夠上傳 新建 刪除 anon_upload_enable=YES anon_mkdir_write_enable=YES
3) 匿名用戶限速500KBps anon_max_rate=500000

僅供參考:
#!/bin/bash
ipaddr=`ifconfig eth0|sed -n '2p'|sed -e 's/.*inet addr:\(.*\) Bcast.*/\1/g'`
iptail=`echo $ipaddr|cut -d'.' -f4`
ipremote=192.168.1.10
#修改主機名
hostname server$iptail.itcast.com
sed -i "/HOSTNAME/cHOSTNAME=server$iptail.itcast.com" /etc/sysconfig/network
echo "$ipaddr server$iptail.itcast.cc" >>/etc/hosts
#關閉防火牆和selinux
service iptables stop
setenforce 0 >/dev/null 2>&1
sed -i '/^SELINUX=/cSELINUX=disabled' /etc/selinux/config
#配置yum源(通常是內網源)
#test network
ping -c 1 $ipremote > /dev/null 2>&1
if [ $? -ne 0 ];then
    echo "你的網絡不通,請先檢查你的網絡"
    exit 1
else
    echo "網絡ok."
fi
cat > /etc/yum.repos.d/server.repo << end
[server]
name=server
baseurl=ftp://$ipremote
enabled=1
gpgcheck=0
end

#安裝軟件
read -p "請輸入須要安裝的軟件,多個用空格隔開:" soft
yum -y install $soft &>/dev/null

#備份配置文件
conf=/etc/vsftpd/vsftpd.conf
\cp $conf $conf.default
#根據需求修改配置文件
sed -ir '/^#|^$/d' $conf
sed -i '/local_enable/c\local_enable=NO' $conf
sed -i '$a anon_upload_enable=YES' $conf
sed -i '$a anon_mkdir_write_enable=YES' $conf
sed -i '$a anon_other_write_enable=YES' $conf
sed -i '$a anon_max_rate=512000' $conf
#啓動服務
service vsftpd restart &>/dev/null && echo"vsftpd服務啓動成功"

#測試驗證
chmod 777 /var/ftp/pub
cp /etc/hosts /var/ftp/pub
#測試下載
cd /tmp
lftp $ipaddr <<end
cd pub
get hosts
exit
end

if [ -f /tmp/hosts ];then
    echo "匿名用戶下載成功"
    rm -f /tmp/hosts
else
    echo "匿名用戶下載失敗"
fi
#測試上傳、建立目錄、刪除目錄等
cd /tmp
lftp $ipaddr << end
cd pub
mkdir test1
mkdir test2
put /etc/group
rmdir test2
exit
end

if [ -d /var/ftp/pub/test1 ];then
    echo "建立目錄成功"
    if [ ! -d /var/ftp/pub/test2 ];then
        echo "文件刪除成功"
        fi
else
    if [ -f /var/ftp/pub/group ];then
    echo "文件上傳成功"
        else
        echo "上傳、建立目錄刪除目錄部ok"
        fi 
fi   
[ -f /var/ftp/pub/group ] && echo "上傳文件成功"
相關文章
相關標籤/搜索