溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

正則表達(dá)式三劍客之二sed工具

發(fā)布時(shí)間:2020-06-14 14:56:52 來(lái)源:網(wǎng)絡(luò) 閱讀:862 作者:23trl 欄目:云計(jì)算

正則表達(dá)式三劍客之二sed工具

三劍客之二sed工具

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

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

?讀?。簊ed 從輸入流(文件、管道、標(biāo)準(zhǔn)輸入)中讀取一行內(nèi)容并存儲(chǔ)到臨時(shí)的緩

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

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

sed命令常見(jiàn)用法

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

常見(jiàn)的sed命令選項(xiàng)主要包含以下幾種:

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

一.sed工具用法

首先我們準(zhǔn)備一個(gè)測(cè)試文件

[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在前面就是第一行不顯示,直接從第二行開(kāi)始

[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”命令時(shí),讀取的第 1 行是文件的第 10 行,
讀取的第 2 行是文件的第 11 行,依此類推,所以輸出的偶數(shù)行是文件的第 11 行、13 行直至文件結(jié)尾,其中包括空行。
以上是 sed 命令的基本用法,sed 命令結(jié)合正則表達(dá)式時(shí),格式略有不同,正則表達(dá)式以“/”包圍。例如,以下操作是 sed 命令與正則表達(dá)式結(jié)合使用的示例。

(7)輸出包含the 的行

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

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

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

(9)輸出包含the 的行所在的行號(hào),等號(hào)(=)用來(lái)輸出行號(hào)

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

(10)輸出以fi開(kāi)頭的行

[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)

因?yàn)楹竺娴氖纠€需要使用測(cè)試文件 test.txt,所以在執(zhí)行刪除操作之前需要先將測(cè)試文件備份。以下示例分別演示了 sed 命令的幾種常用刪除用法。
下面命令中 nl 命令用于計(jì)算文件的行數(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 的行,用!符號(hào)表示取反操作,如’/cross/!d’

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

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

[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ù)的空行只保留一個(gè), 執(zhí)行“ sed –e ‘/^KaTeX parse error: Expected group after '^' at position 6: /{n;/^?/d}’test.txt”命令即可實(shí)現(xiàn)。其效果與“cat -s test.txt”相同,n 表示讀下一行數(shù)據(jù)。

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

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

將每行中的第一個(gè)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 個(gè)l 替換為L(zhǎng)

[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)在每行行首插入#號(hào)

[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 的行遷移至文件末尾,{;}用于多個(gè)操作

[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 行后插入一個(gè)新行,內(nèi)容為 New

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

(6)在包含the 的每行后插入一個(gè)新行,內(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"

#備份原來(lái)的配置文件,檢測(cè)文件名為/etc/vsftpd/vsftpd.conf.bak 備份文件是否存在, 若不存在則使用 cp 命令進(jìn)行文件備份
[ ! -e "$CONFIG.bak" ] && cp $CONFIG $CONFIG.bak
#基于樣本配置進(jìn)行調(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
#啟動(dòng)vsftpd 服務(wù),并設(shè)為開(kāi)機(jī)后自動(dòng)運(yùn)行systemctl restart vsftpd
systemctl enable vsftpd

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

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI