sed編輯器的使用以及詳細解讀
目錄
- 一、sed編輯器
- 1、sed 的作業流程
- (1)、讀取
- (2)、執行
- (3)、顯示
- 2、命令講解
- (1)、使用地址:
- (2)、洗掉行
- (3)、替換
一、sed編輯器
sed是一種流編輯器,流編輯器會在編輯器處理資料之前基于預先提供的一組規則來編輯資料流,
sed編輯器可以根據命令來處理資料流中的資料,這些命令要么從命令列中輸入,要么存盤在一個命令文本檔案中
1、sed 的作業流程
(1)、讀取
sed 從輸入流(檔案、管道、標準輸入)中讀取一行內容并存盤到臨時的緩沖區中(又稱模式空間,pattern space),
(2)、執行
默認情況下,所有的sed 命令都在模式空間中順序地執行,除非指定了行的地址,否則sed 命令 將會在所有的行上依次執行,
(3)、顯示
發送修改后的內容到輸出流,在發送資料后,模式空間將會被清空,在所有的檔案內容都被處理完成之前,上述程序將重復執行,直至所有內容被處理完,
在所有的檔案內容都被處理完成之前,上述程序將重復執行,直至所有內容被處理完,
注意:默認情況下所有的sed命令都是在模式空間內執行的,因此輸入的檔案并不會發生任何變化,除非是用重定向存盤輸出,
2、命令講解
命令格式:
sed -e '操作' 檔案1 檔案2 ...
sed -n -e '操作' 檔案1 檔案2 ...
sed -f 腳本檔案 檔案1 檔案2 ...
sed -i -e '操作' 檔案1 檔案2 .
…
sed -e 'n{
操作1
操作2
...
}' 檔案1 檔案2 ...
常用選項:
-e 或--expression=:表示用指定命令來處理輸入的文本檔案,只有一個操作命令時可省略,一般在執行多個操作命令使用
-f 或--file=:表示用指定的腳本檔案來處理輸入的文本檔案,
-h 或--help:顯示幫助,
-n、--quiet 或 silent:禁止sed編輯器輸出,但可以與p命令一起使用完成輸出,
-i:直接修改目標文本檔案,
常用操作:
s:替換,替換指定字符,
d:洗掉,洗掉選定的行,
a:增加,在當前行下面增加一行指定內容,
i:插入,在選定行上面插入一行指定內容,
c:替換,將選定行替換為指定內容,
y:字符轉換,轉換前后的字符長度必須相同,
p:列印,如果同時指定行,表示列印指定行;如果不指定行,則表示列印所有內容;如果有非列印字符,則以 ASCII 碼輸出,其通常與“-n”選項一起使用,
=:列印行號,
l(小寫L):列印資料流中的文本和不可列印的ASCII字符(比如結束符$、制表符\t)
列印內容:
sed -n -e 'p' test1.txt
#不指定行 默認列印所有內容
[root@localhost ~]#sed -n -e 'p' test1.txt
one
two
three
four
five
six
sed -n -e '=' test1.txt
#列印檔案內的行號
[root@localhost ~]#sed -n -e '=' test1.txt
1
2
3
4
5
6
sed -n -e 'l' test1.txt
#列印文本和ASCII碼
[root@localhost ~]#sed -n -e 'l' test1.txt
one$
two$
three$
four$
five$
six$
sed -n -e '=;p'test1.txt 或
sed -n -e '=' -e 'p' test1.txt
sed -n '
> =
> p
> ' test1.txt
#列印檔案中的文本和行號
[root@localhost ~]#sed -n -e '=;p' test1.txt
1
one
2
two
3
three
4
four
5
five
6
six
(1)、使用地址:
sed編輯器有2種尋址方式:
1、以數字形式表示行區間
2、用文本模式來過濾出行
sed -n '1p' test1.txt #列印第一行
[root@localhost ~]#sed -n '1p' test1.txt
one
sed -n '$p' test1 #列印最后一行
[root@localhost ~]#sed -n '$p' test1.txt
six
sed -n '1,3p' test1.txt #列印1到3行
[root@localhost ~]#sed -n '1,3p' test1.txt
one
two
three
sed -n '3,$p' test1.txt #列印3行到最后一行
[root@localhost ~]#sed -n '3,$p' test1.txt
three
four
five
six
sed -n '1,+3p' test1.txt #列印1之后的連續3行,即1-4行
[root@localhost ~]#sed -n '1,+3p' test1.txt
one
two
three
four
s
ed '5q' test1.txt #列印前5行資訊后退出,q表示退出
[root@localhost ~]#sed '5q' test1.txt
one
two
three
four
five
sed -n 'p;n' test1.txt #列印奇數行(先從第一行列印,然后隔一行列印一次);n表示移動到下一行
[root@localhost ~]#sed -n 'p;n' test1.txt
one
three
five
sed -n 'n;p' test1.txt #列印偶數行 (先跳到第二行,然后列印,然后在跳過)
[root@localhost ~]#sed -n 'n;p' test1.txt
two
four
six
sed -n '2,${n;p}' test1.txt #先跳到第二行,然后跳到下一行開始列印,從第三行開始列印奇數行
[root@localhost ~]#sed -n '2,${n;p}' test1.txt
three
five
sed -n '/user/p' /etc/passwd #列印/etc/passwd 檔案內有user的行
[root@localhost ~]#sed -n '/user/p' /etc/passwd
saslauth:x:996:76:Saslauthd user:/run/saslauthd:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
qemu:x:107:107:qemu user:/:/sbin/nologin
radvd:x:75:75:radvd user:/:/sbin/nologin
sed -n '/^a/p' /etc/passwd #列印以a開頭的行
[root@localhost ~]#sed -n '/^a/p' /etc/passwd
adm:x:3:4:adm:/var/adm:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
sed -n '/bash$/p' /etc/passwd #列印以bash結尾的行

sed -n '/ftp\|root/p' /etc/passwd # 列印包含ftp或者root的行

sed -n '2,/nobody/p' /etc/passwd #從第二行開始列印出第一個包含nobody的行


sed -n '/nobody/=’ /etc/passwd #列印包含nobody的行號

sed -nr '/ro{1,}t/p' /etc/passwd #-r表示支持正則運算式
#匹配已ro開頭,已t結尾,中間至少包含一個o的行

(2)、洗掉行
sed 'd' test.txt #全刪
[root@localhost ~]#sed -i 'd' test.txt
[root@localhost ~]#cat test.txt
sed ‘3d’ test.txt #洗掉第三行
[root@localhost ~]#sed '3d' test.txt
one
two
four
five
six
seven #可以看到第三行沒有了
sed ‘2,4d’ test.txt #洗掉2到4行
[root@localhost ~]#sed '2,4d' test.txt
one
five
six
seven #可以看到234行都沒有了
sed '$d' test.txt #洗掉最后一行
[root@localhost ~]#sed '$d' test.txt
one
two
three
four
five
six #第七行seven沒有了
sed '/^$/d' .test.txt #洗掉空行

sed '/bash$/d' /etc/passwd #洗掉已bash結尾的行

sed '/nologin$/!d' /etc/passwd #“!”表示取反操作 不洗掉nologin結尾的行,也就是除了nologin結尾的行都洗掉

sed '/2/,/3/d' test.txt #從第一個位置打開行洗掉功能,到第二個位置關閉行洗掉功能
sed '/1/,/3/d' test.txt

(3)、替換
行范圍 s/舊字串/新字串/替換標記
4種替換標記:
數字:表明新字串將替換第幾處匹配的地方
g:表明新字串將會替換所有匹配的地方
p:列印與替換命令匹配的行,與-n一起使用
w 檔案:將替換的結果寫到檔案中
sed -n 's/root/admin/p' /etc/passwd #把root替換成admin 默認第一個root


sed -n 's/root/admin/2p' /etc/passwd #把每一行的第二個root替換成admin

sed -n 's/root/admin/gp' /etc/passwd #把root全部替換成admin

sed 's/root//g' /etc/passwd #把所有的root換成空,就是洗掉的意思

sed '1,20 s/^/#/' /etc/passwd #在1-20行的行首添加#號

sed '/^root/ s/$/#/' /etc/passwd #找到root開頭的行,在結尾加上#

sed '1,20w out.txt' /etc/passwd #把1-20行輸出保存到out.txt檔案內
sed '1,20 s/^/#/w out.txt' /etc/passwd #把1-20行的開頭加上#號 輸出到out.txt檔案內
```handlebars
sed -n 's/\/bin\/bash/\/bin\/csh/p' /etc/passwd #
sed -n 's!/bin/bash!/bin/csh!p' /etc/passwd #使用“!”作為字串分隔符
把bash替換成csh

sed '/three/c ABC' test.txt#搜索到包含45的行,整行替換成ABC

sed '/3/ y/3/A/' test.txt #搜索到3的行,把3替換成相同字串長度的A

sed '1,3a ABC' testfile2 # 在1-3行下面分別插入ABC

sed '1i ABC' test.txt #在1行上面加ABC

sed '5r /etc/resolv.conf' test.txt # 把/etc/resolv檔案匯入test第五行

sed '/root/{H;d};$G' /etc/passwd #將包含root的行剪切到末尾,H表示復制到剪切板,G表示粘貼到指定行后


sed '1,5H;15G' /etc/passwd #把1-5行復制到15行后面

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/241444.html
標籤:其他
