正則表達式三劍客之二sed工具

正則表達式三劍客之二sed工具

10年積累的成都做網(wǎng)站、網(wǎng)站建設(shè)經(jīng)驗,可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認識你,你也不認識我。但先網(wǎng)站策劃后付款的網(wǎng)站建設(shè)流程,更有天峨免費網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。

三劍客之二sed工具

sed(Stream EDitor)是一個強大而簡單的文本解析轉(zhuǎn)換工具,可以讀取文本,并根據(jù)指定的條件對文本內(nèi)容進行編輯(刪除、替換、添加、移動等),最后輸出所有行或者僅輸出處理的某些行。sed 也可以在無交互的情況下實現(xiàn)相當(dāng)復(fù)雜的文本處理操作,被廣泛應(yīng)用于 Shell 腳本中,用以完成各種自動化處理任務(wù)。

sed 的工作流程主要包括讀取、執(zhí)行和顯示三個過程。

?讀取:sed 從輸入流(文件、管道、標準輸入)中讀取一行內(nèi)容并存儲到臨時的緩

沖區(qū)中(又稱模式空間,pattern space)。
?執(zhí)行:默認情況下,所有的 sed 命令都在模式空間中順序地執(zhí)行,除非指定了行的地址,否則 sed 命令將會在所有的行上依次執(zhí)行。
?顯示:發(fā)送修改后的內(nèi)容到輸出流。再發(fā)送數(shù)據(jù)后,模式空間將會被清空。
在所有的文件內(nèi)容都被處理完成之前,上述過程將重復(fù)執(zhí)行,直至所有內(nèi)容被處理完。

注意:默認情況下,所有的 sed 命令都是在模式空間內(nèi)執(zhí)行的,因此輸入的文件并不會發(fā)生任何變化,除非是用重定向存儲輸出。

sed命令常見用法

調(diào)用 sed 命令有兩種格式。其中,“參數(shù)”是指操作的目標文件,當(dāng)存在多個操作對象時用,文件之間用逗號“,”分隔;而 scriptfile 表示腳本文件,需要用“-f”選項指定,當(dāng)腳本文件出現(xiàn)在目標文件之前時,表示通過指定的腳本文件來處理輸入的目標文件。

常見的sed命令選項主要包含以下幾種:

(1)?e 或–expression=:表示用指定命令或者腳本來處理輸入的文本文件。
(2)-f 或–file=:表示用指定的腳本文件來處理輸入的文本文件。
(3)-h 或–help:顯示幫助。
(4)-n、–quiet 或 silent:表示僅顯示處理后的結(jié)果。
(5)-i:直接編輯文本文件。
“操作”用于指定對文件操作的動作行為,也就是 sed 的命令。通常情況下是采用的“[n1[,n2]]”操作參數(shù)的格式。n1、n2 是可選的,不一定會存在,代表選擇進行操作的行數(shù),如操作需要在 5~20 行之間進行,則表示為“5,20 動作行為”。常見的操作包括以下幾種。
(1)a:增加,在當(dāng)前行下面增加一行指定內(nèi)容。
(2)c:替換,將選定行替換為指定內(nèi)容。
(3)d:刪除,刪除選定的行。
(4)i:插入,在選定行上面插入一行指定內(nèi)容。
(5)p:打印,如果同時指定行,表示打印指定行;如果不指定行,則表示打印所有內(nèi)容;如果有非打印字符,則以 ASCII 碼輸出。其通常與“-n”選項一起使用。
(6)s:替換,替換指定字符。
(7)y:字符轉(zhuǎn)換。

一.sed工具用法

首先我們準備一個測試文件

[root@localhost ~]# vim chen.txt

Use CDROM installation media
cdrom.
thethethe.
THE
THEASDHAS
 Use graphical install.
graphical.
best
test
ASSDJFXYxyzC
AxyzxyzxyzC
keyboard --vckeymap=cn --xlayouts='cn'
 System language
lang zh_CN.UTF-8

 Network information
network  --bootproto=dhcp --device=ens33 --onboot=off --ipv6=auto --no-activate
network  --hostname=localhost.localdomain

 Root password
rootpw --iscrypted $6$L.egxzosoP/0k9Nj$wna7vPXZjeH0jFcNZUymYKF8ySXq5HxQuvxTFxIpEAAxuDj7MQJtXBds5E0LxAftI1H5JbJuYpN44d5n6t1AZ.
 System services
services --enabled="chronyd"
#System timezone
tast

(1)輸出符合條件的文本(p表示正常輸出)

#version=DEVEL
#System authorization information
auth --enableshadow --passalgo=sha512
 Use CDROM installation media
cdrom.
thethethe.
THE
THEASDHAS
 Use graphical install.
graphical.
best
test
ASSDJFXYxyzC
AxyzxyzxyzC
keyboard --vckeymap=cn --xlayouts='cn'
 System language
lang zh_CN.UTF-8

 Network information
network  --bootproto=dhcp --device=ens33 --onboot=off --ipv6=auto --no-activate
network  --hostname=localhost.localdomain

 Root password
rootpw --iscrypted $6$L.egxzosoP/0k9Nj$wna7vPXZjeH0jFcNZUymYKF8ySXq5HxQuvxTFxIpEAAxuDj7MQJtXBds5E0LxAftI1H5JbJuYpN44d5n6t1AZ.
 System services
services --enabled="chronyd"
 System timezone
tast
bet
 Run the Setup Agent on first boot.
firstboot --enable
ignoredisk --only-use=sda
wood
wd
wod
woooooooood
dfsjdjoooooof
Foofddd
124153
3234
342222222
faasd11
2
ZASASDNA
short
shirt

(2)輸出第3行

[root@localhost ~]# sed -n '3p' chen.txt
auth --enableshadow --passalgo=sha512

(3)輸出3^5行

[root@localhost ~]# sed -n '3,5p' chen.txt
auth --enableshadow --passalgo=sha512
#Use CDROM installation media
cdrom.

(4)輸出所有奇數(shù)行,n表示讀入下一行資料,p在前面就是第一行顯示

[root@localhost ~]# sed -n 'p;n' chen.txt
#version=DEVEL
auth --enableshadow --passalgo=sha512
cdrom.
THE
 Use graphical install.
best
ASSDJFXYxyzC
keyboard --vckeymap=cn --xlayouts='cn'
lang zh_CN.UTF-8
 Network information
network  --hostname=localhost.localdomain
 Root password
 System services
 System timezone
bet
firstboot --enable
wood
wod
dfsjdjoooooof
124153
342222222
2
short

(5)輸出所有偶數(shù)行,n在前面就是第一行不顯示,直接從第二行開始

[root@localhost ~]# sed -n 'n;p' chen.txt
 System authorization information
 Use CDROM installation media
thethethe.
THEASDHAS
graphical.
test
AxyzxyzxyzC
#System language

network  --bootproto=dhcp --device=ens33 --onboot=off --ipv6=auto --no-activate

rootpw --iscrypted $6$L.egxzosoP/0k9Nj$wna7vPXZjeH0jFcNZUymYKF8ySXq5HxQuvxTFxIpEAAxuDj7MQJtXBds5E0LxAftI1H5JbJuYpN44d5n6t1AZ.
services --enabled="chronyd"
tast
 Run the Setup Agent on first boot.
ignoredisk --only-use=sda
wd
woooooooood
Foofddd
3234
faasd11
ZASASDNA
shirt

(6)輸出第 10 行至文件尾之間的偶數(shù)行

[root@localhost ~]# sed -n '10,${n;p}' chen.txt
best
ASSDJFXYxyzC
keyboard --vckeymap=cn --xlayouts='cn'
lang zh_CN.UTF-8
 Network information
network  --hostname=localhost.localdomain
 Root password# System services
#System timezone
bet
firstboot --enable
wood
wod
dfsjdjoooooof
124153
342222222
2
short

在執(zhí)行“sed –n‘10,${n;p}’test.txt”命令時,讀取的第 1 行是文件的第 10 行,
讀取的第 2 行是文件的第 11 行,依此類推,所以輸出的偶數(shù)行是文件的第 11 行、13 行直至文件結(jié)尾,其中包括空行。
以上是 sed 命令的基本用法,sed 命令結(jié)合正則表達式時,格式略有不同,正則表達式以“/”包圍。例如,以下操作是 sed 命令與正則表達式結(jié)合使用的示例。

(7)輸出包含the 的行

[root@localhost ~]# sed -n '/the/p' chen.txt
thethethe.
 Run the Setup Agent on first boot.

(8)輸出從第 4 行至第一個包含 the 的行

[root@localhost ~]# sed -n '4,/the/p' chen.txt
 Use CDROM installation media
cdrom.
thethethe.

(9)輸出包含the 的行所在的行號,等號(=)用來輸出行號

[root@localhost ~]# sed -n '/the/=' chen.txt
6
30

(10)輸出以fi開頭的行

[root@localhost ~]# sed -n '/^fi/p' chen.txt
firstboot --enable

(11) 輸出以數(shù)字結(jié)尾的行

[root@localhost ~]# sed -n '/[0-9]$/p' chen.txt
auth --enableshadow --passalgo=sha512
lang zh_CN.UTF-8
124153
3234
342222222
faasd11
2

(12)輸出包含單詞wood 的行,<、>代表單詞邊界

[root@localhost ~]# sed -n '/\<wood\>/P' chen.txt
wood

(二)刪除符合條件的文本(d)

因為后面的示例還需要使用測試文件 test.txt,所以在執(zhí)行刪除操作之前需要先將測試文件備份。以下示例分別演示了 sed 命令的幾種常用刪除用法。
下面命令中 nl 命令用于計算文件的行數(shù),結(jié)合該命令可以更加直觀地查看到命令執(zhí)行的結(jié)果。

(1)刪除第 3 行

[root@localhost ~]# nl chen.txt | sed '3d'
     1  #version=DEVEL
     2  # System authorization information
     4  # Use CDROM installation media
     5  cdrom.
     6  thethethe.

(2)刪除第 3~5 行

[root@localhost ~]# nl chen.txt | sed '3,5d'
     1  #version=DEVEL
     2  # System authorization information
     6  thethethe.

(3)//刪除包含cross 的行,原本的第 47行被刪除

//刪除不包含cross 的行,用!符號表示取反操作,如’/cross/!d’

[root@localhost ~]# nl chen.txt | sed '3,5d'
  45    short
    46  shirt

(4)刪除以小寫字母開頭的行

[root@localhost ~]# sed '/[^a-z]/d' chen.txt

best
test

crp
tast
cross
bet
wood
wd
wod
woooooooood
dfsjdjoooooof
short
shirt

(5)刪除以"."結(jié)尾的行

[root@localhost ~]# sed '/\.$/d' chen.txt

(6)刪除所有空行

[root@localhost ~]# sed '/^$/d' chen.txt
注意: 若是刪除重復(fù)的空行,即連續(xù)的空行只保留一個, 執(zhí)行“ sed –e ‘/^KaTeX parse error: Expected group after '^' at position 6: /{n;/^?/d}’test.txt”命令即可實現(xiàn)。其效果與“cat -s test.txt”相同,n 表示讀下一行數(shù)據(jù)。

(三)替換符合條件的文本

(1)在使用 sed 命令進行替換操作時需要用到 s(字符串替換)、c(整行/整塊替換)、y(字符轉(zhuǎn)換)命令選項

將每行中的第一個the 替換為 THE

[root@localhost ~]# sed 's/the/THE/' chen.txt
#version=DEVEL
 System authorization information
auth --enableshadow --passalgo=sha512
 Use CDROM installation media
cdrom.
THEthethe.
THE

(2)/將每行中的第 3 個l 替換為L

[root@localhost ~]# sed 's/l/L/' chen.txt
#version=DEVEL
 System authorization information
auLth --enableshadow --passalgo=sha512
 Use CDROM instaLlation media
cdLrom.

(3)將文件中的所有the 替換為THE

[root@localhost ~]# sed 's/the/THE/g' chen.txt
#version=DEVEL
 System authorization information
aulth --enableshadow --passalgo=sha512
 Use CDROM installation media
cdlrom.
THETHETHE.
THE
THEASDHAS

(4)將文件中的所有o 刪除(替換為空串)

[root@localhost ~]# sed 's/o//g' chen.txt

(5)在每行行首插入#號

[root@localhost ~]# sed 's/^/#/g' chen.txt
##version=DEVEL
##System authorization information
#aulth --enableshadow --passalgo=sha512
##Use CDROM installation media
#cdlrom.
#thethethe.
#THE
#THEASDHAS
##Use graphical install.

(6)在每行行尾插入字符串EOF

[root@localhost ~]# sed 's/$/EOF/g' chen.txt
ersion=DEVELEOF
ystem authorization informationEOF
aulth --enableshadow --passalgo=sha512EOF
se CDROM installation mediaEOF
cdlrom.EOF
thethethe.EOF
THEEOF
THEASDHASEOF
Use graphical install.EOF

(四)遷移符合條件的文本

H,復(fù)制到剪貼板;
g、G,將剪貼板中的數(shù)據(jù)覆蓋/追加至指定行;
w,保存為文件;
r,讀取指定文件;
a,追加指定內(nèi)容。

(1)將包含the 的行遷移至文件末尾,{;}用于多個操作

[root@localhost ~]# sed '/the/{H;d};$G' chen.txt
#version=DEVEL
 System authorization information
aulth --enableshadow --passalgo=sha512
 Use CDROM installation media
cdlrom.
THE
THEASDHAS

(2)將第 1~5 行內(nèi)容轉(zhuǎn)移至第 17 行后

[root@localhost ~]# sed '1,5{H;d};17G' chen.txt
thethethe.
THE
THEASDHAS
 Use graphical install.
graphical.
best

(3)將包含the 的行另存為文件/opt/abc.txt

[root@localhost ~]# sed '/the/w /opt/abc.txt' chen.txt
[root@localhost ~]# cd /opt
[root@localhost opt]# ls
abc.txt  rh
[root@localhost opt]# cat abc.txt
thethethe.
#Run the Setup Agent on first boot.

(4)將文件/etc/hostname 的內(nèi)容添加到包含the 的每行以后

[root@localhost ~]# sed '/the/r /etc/hostname' chen.txt
 Run the Setup Agent on first boot.
localhost.localdomain

(5)在第 3 行后插入一個新行,內(nèi)容為 New

[root@localhost ~]# sed '3aNEW' chen.txt
#version=DEVEL
 System authorization information
aulth --enableshadow --passalgo=sha512
NEW

(6)在包含the 的每行后插入一個新行,內(nèi)容為 New

[root@localhost ~]# sed '/the/aNEW' chen.txt
 Run the Setup Agent on first boot.
NEW

(7)在第 3 行后插入多行內(nèi)容,中間的\n 表示換行

[root@localhost ~]# sed '3aNEW\nNEW2\nNEW3' chen.txt

version=DEVEL
System authorization information
aulth --enableshadow --passalgo=sha512
NEW
NEW2
NEW3

(五)腳本編輯文件

root@localhost ~]# vim wq.com
#!/bin/bash
#指定樣本文件路徑、配置文件路徑
SAMPLE="/usr/share/doc/vsftpd-3.0.2/EXAMPLE/INTERNET_SITE/vsftpd.conf " CONFIG="/etc/vsftpd/vsftpd.conf"

#備份原來的配置文件,檢測文件名為/etc/vsftpd/vsftpd.conf.bak 備份文件是否存在, 若不存在則使用 cp 命令進行文件備份
[ ! -e "$CONFIG.bak" ] && cp $CONFIG $CONFIG.bak
#基于樣本配置進行調(diào)整,覆蓋現(xiàn)有文件
sed -e '/^anonymous_enable/s/YES/NO/g' $SAMPLE > $CONFIG
sed -i -e '/^local_enable/s/NO/YES/g' -e'/^write_enable/s/NO/YES/g' $CONFIG 

grep "listen" $CONFIG || sed -i '$alisten=YES' $CONFIG
#啟動vsftpd 服務(wù),并設(shè)為開機后自動運行systemctl restart vsftpd
systemctl enable vsftpd

[root@localhost ~]# chmod +x wq.sh
[root@localhost ~]# ./wq.sh

當(dāng)前標題:正則表達式三劍客之二sed工具
URL地址:http://muchs.cn/article46/ghgjeg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計公司服務(wù)器托管、微信小程序、網(wǎng)站設(shè)計、網(wǎng)站策劃、網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作