linux sed命令

sed編輯器簡介

sed是linux下一種常用的非交互式編輯器,不同于常用的交互式的vim編輯器,我們只能在command line輸入命令并得到輸出。如果不對結果進行重定向,sed命令一般不會對文件造成修改。

sed實現原理

image

如圖所示,sed在得到文件后,會將文件逐行進行處理,且把處理中的行保存在臨時緩存中,當sed處理完最后一行后,會將結果由緩存輸出至屏幕,且因為是在緩存中處理,sed并不會對源文件造成任何的破壞。

sed常用參數與命令

sed命令使用格式為: sed [參數] '命令' 處理文本

如: sed -i 's#hello#world#g' test.txt

參數 說明
-n 取消默認輸出,只打印被sed處理后的行
-e 多重編輯,且命令順序會影響輸出結果
-f 指定一個sed腳本文件到命令行執行
-r sed使用擴展正則
-i 直接修改文檔讀取內容,不在屏幕上輸出
命令 說明
a\ 追加,在當前行后添加一行或多行
c\ 用新文本修改(替換)當前行中的文本
d 刪除行
i\ 在當前行之前插入文本
l 列出非打印字符
p 打印行
n 讀取下一輸入行,并從下一條命令而不是第一條命令開始處理
q 結束或退出sed
r 從文件中讀取輸入行
! 對所選行以外的所有行應用命令
s 用一個字符替換另一個

sed命令常用使用方法

sed一般用于行處理,可以配合各類參數,操作命令與正則表達式進行復雜的文本處理。

例:當前有一個文本,包含了我們care的字段‘bingo’

[root@localhost ~]# cat test.txt 
hello
world
hello bingo
world bingoes
bing1
not bingo tho
a test that is not exist
this is a test about bingossssss
sed bingo
go

打印:p

p是最常用的命令之一,通print,可以顯示當前sed緩存中的內容,參數-n可以取消默認打印操作,當-n與p同時使用時,即可打印指定的內容。

[root@localhost ~]# sed -n '/bingo/p' test.txt 
hello bingo
world bingoes
not bingo tho
this is a test about bingossssss
sed bingo

此操作匹配了test.txt文本中的bingo,當一行有'bingo'時,打印該行。

刪除:d

d命令用于刪除該行,sed先將此行復制到緩存區中,然后執行sed刪除命令,最后將緩存區中的結果輸出至屏幕。

[root@localhost ~]# sed '/bingo/d' test.txt 
hello
world
bing1
a test that is not exist
go

屏幕得到的結果為不包含bingo的行。
注意:此命令并不會實際刪除包含bingo字段的行。

替換:s

s命令是替換命令,可以替換并取代文件中的文本,s后的斜杠中的文本可以是正則表達式,后面跟著需要替換的文本。如果在后面增加g(global)命令代表進行全局替換。

[root@localhost ~]# sed 's/bingo/bango/g' test.txt
hello
world
hello bango
world bangoes
bing1
not bango tho
a test that is not exist
this is a test about bangossssss
sed bango
go

注意:此命令也不會對實際文件造成修改。如果想要修改,可加'-i'參數。如sed -i 's#bingo#bango#g' test.txt, 此命令就實際對test.txt中的文本形成了替換,且修改結果不會輸出至屏幕。

指定行:逗號

sed可以選取從某行開始到某行結束的范圍。行的范圍可以為行號、正則表達式、或者兩者的結合。

(1)行號:

[root@localhost ~]# sed -n '1,4p' test.txt
hello
world
hello bingo
world bingoes

(2)正則表達式:

[root@localhost ~]# sed '/hello/,/sed bingo/s/$/**sub**/' test.txt
hello**sub**
world**sub**
hello bingo**sub**
world bingoes**sub**
bing1**sub**
not bingo tho**sub**
a test that is not exist**sub**
this is a test about bingossssss**sub**
sed bingo**sub**
go

這條命令的意思是從hello行開始到sed bingo行結束,在每行的結尾($)替換為sub。

追加:a命令

a(append)命令為追加,可以追加新文本于選取行的后面。

[root@localhost ~]# sed '/^hello/a ***appendtext***' test.txt
hello
***appendtext***
world
hello bingo
***appendtext***
world bingoes
bing1
not bingo tho
a test that is not exist
this is a test about bingossssss
sed bingo
go

插入:i命令

i(insert)命令類似于追加,可以在選取行前插入文本。

[root@localhost ~]# sed '/^hello/i ***inserttext***' test.txt
***inserttext***
hello
world
***inserttext***
hello bingo
world bingoes
bing1
not bingo tho
a test that is not exist
this is a test about bingossssss
sed bingo
go

sed通常用于文本文件的行處理,以上只是介紹了幾種常用的sed使用方法,具體的其它方法詳見sed, a stream editor。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Linux sed命令是利用script來處理文本文件。sed可依照script的指令,來處理、編輯文本文件。Se...
    姜淑均閱讀 738評論 1 2
  • linux sed命令詳解 1. Sed簡介 sed 是一種在線編輯器,它一次處理一行內容。處理時,把當前處理的行...
    很少更新了閱讀 2,170評論 0 6
  • ??sed是Linux下一款功能強大的文本處理工具,可以替換、刪除、追加文件內容,支持正則表達式使用。 文本替換將...
    你東哥呀閱讀 1,155評論 0 12
  • linux資料總章2.1 1.0寫的不好抱歉 但是2.0已經改了很多 但是錯誤還是無法避免 以后資料會慢慢更新 大...
    數據革命閱讀 12,239評論 2 33
  • 本文承接之前寫的三十分鐘學會AWK一文,在學習完AWK之后,趁熱打鐵又學習了一下SED,不得不說這兩個工具真的堪稱...
    mylxsw閱讀 4,417評論 3 74