sed命令例項 shell指令碼入門

2021-09-26 14:58:41 字數 4654 閱讀 9549

什麼是 sed?

sed是乙個很好的檔案處理工具,本身是乙個管道命令,主要以行為單位進行處理,可以將資料行進行替換、刪除、新增、選取等特定工作。

sed [選項] [命令]

-n,使用安靜(silent)模式。在一般 sed 的用法中,所有來自 stdin 的資料一般都會被列出到終端上。但如果加上 -n 引數後,則只有經過sed特殊處理的那一行(或者動作)才會被列出來。

-e,直接在命令列模式上進行sed的動作編輯。

-f,直接將sed的動作寫在乙個檔案內。-f filename 則可以執行filename內的sed命令。

-r,sed 的動作支援的是延伸型正規表示法的語法。(預設是基礎正規表示法語法)

-i,直接修改讀取的檔案內容,而不是輸出到終端。

[n1[,n2]]function

n1, n2,不一定存在,一般代表「選擇進行動作的行數」,如果我的動作需要在10到20行之間進行,則有』10,20命令』

1.3 常用命令

a,新增,a的後面可以接字串,而這些字串會在新的一行出現(目前的下一行)

c,取代,c的後面可以接字串,這些字串可以取代n1,n2 之間的行

d,刪除,因為是刪除,所以d後面通常不接任何東西

i,插入,i的後面可以接字串,而這些字串會在新的一行出現(目前的上一行)

p,輸出,即將某個選擇的檔案輸出。通常p會與引數sed -n 一起使用

s,取代,直接對某些字串進行替換

# 假設我們有一檔名為ab.txt,內容如下

$ cat ab.txt

this is a newfile!

hello world

please

asd12

123asdasd

# 刪除某行

# 對ab.txt的行進行操作,將操作結果輸出到終端(只是做模擬操作,不改動原始檔)

$ sed '1d' ab.txt # 輸出刪除第一行後的檔案內容

hello world

please

asd12

123asdasd

# 檢視檔案,並沒有實際上刪除第一行

$ cat ab.txt

this is a newfile!

hello world

please

asd12

123asdasd

$ sed '$d' ab.txt # 輸出刪除最後一行後的檔案內容

...$ sed '1, 2d' ab.txt # 輸出刪除第一行到第二行後的檔案內容

...$ sed '2, $d' ab.txt # 輸出刪除第2行到最後1行後的檔案內容

...# 如果想要物理上刪除第一行

$ sed '1d' -i ab.txt

# 檢視檔案,真的把檔案的第一行刪除了

$ cat ab.txt

hello world

please

asd12

123asdasd

# 顯示某行

$ sed -n '1p' ab.txt # 只顯示檔案的第一行

$ sed -n '$p' ab.txt # 只顯示檔案的最後一行

$ sed -n '1, 2p' ab.txt # 只顯示檔案的第一行到第二行

$ sed -n '2, $p' ab.txt # 顯示檔案的第二行到最後一行

# 使用安靜模式進行查詢

$ cat ab.txt

this is a newfile!

hello world $

please

asd12

123asdasd

# 輸出關鍵字 please 所在行的內容;其中'/str/p',str為搜尋的文字內容

$ sed -n '/newfile/p' ab.txt

this is a newfile!

# 輸出關鍵字$所在行的內容,使用反斜線\遮蔽特殊含義

$ sed -n '/\$/p' ab.txt

hello world $

$ cat ab

this is a newfile!

hello world $

please

asd12

123asdasd

# 增加一行或多行字串

# 在第一行後增加字串"drink tea"

$ sed '1a drink tea' ab.txt

this is a newfile!

drink tea

hello world $

please

asd12

123asdasd

# 在第一行到第三行後增加字串"drink tea"

$ sed '1,3a drink tea' ab.txt

this is a newfile!

drink tea

hello world $

drink tea

please

drink tea

asd12

123asdasd

# 在第一行後增加兩行,換行使用\n,可多次使用\n新增多行

$ sed '1a drink tea\nor coffee' ab.txt

this is a newfile!

drink tea

or coffee

hello world $

please

asd12

123asdasd

# 替代一行或多行

$ sed '1c hi' ab.txt # 把ab.txt的第一行替換為hi

$ sed '1,2c hi' ab.txt # 把ab.txt的第一行到第二行替換為hi

# 替換一行中的某部分字串

# 格式:sed 's/要替換的字串/新的字串/g' ab.txt(要替換的字串可以用正規表示式)

sed 's/ruby/bird/g' ab.txt # 把全部的ruby替換為bird

sed 's/ruby//g' ab.txt # 把全部的ruby替換為空,即刪除ruby字串

# sed 加上 -i 引數直接對檔案進行操作

# 對每行匹配到的第乙個字串進行替換

sed -i 's/原字串/新字串/' ab.txt

# 對全域性匹配上的所有字串進行替換

sed -i 's/原字串/新字串/g' ab.txt

# 刪除所有匹配到字串的行

sed -i '/匹配字串/d' ab.txt

# 特定字串的 行後 (a) 插入新行

sed -i '/特定字串/a 新行字串' ab.txt

# 特定字串的 行前 (i) 插入新行

sed -i '/特定字串/i 新行字串' ab.txt

# 把匹配行中的某個字串替換為目標字串

sed -i '/匹配字串/s/源字串/目標字串/g' ab.txt

# 在檔案ab.txt中的末行之後,新增bye

sed -i '$a bye' ab.txt

# 對於檔案第3行,把匹配上的所有字串進行替換

sed -i '3s/原字串/新字串/g' ab.txt

sed -i "/734/c\hello" install.log ,這樣就把包含有734這樣字串的整個一行替換成了hello 

# 在使用c\的時候需要注意,如果我們在c\命令後面加的不是乙個常數字串,而是乙個變數,那麼,要再加乙個\符號來解引用,否則變數無法正常解析:

sed -i "/734/c\$hello" install.log -- 含有734的一行被替換成了$hello

備註:

在文中,我們都是用的 『/』 作為定界符,除了斜槓之外還可以使用其他符號來作為定界符。

$ cat ab.txt 

this is a newfile!

hello world $

please

asd12

123asdasd

# 例子中 '/' 作為定界符

$ sed 's/hello/hahahaha/' ab.txt

this is a newfile!

hahahaha world $

please

asd12

123asdasd

# 使用 ';' 作為定界符

$ sed 's;hello;hahahaha;' ab.txt

this is a newfile!

hahahaha world $

please

asd12

123asdasd

效果是一樣的,所以建議涉及到檔案路徑、正則表達的時候換乙個更加清晰的定界符。

參考:

Shell指令碼sed命令

1 p命令 命令p用於顯示模式空間的內容。預設情況下,sed把輸入行列印在螢幕上,選項 n用於取消預設的列印操作。當選項 n和命令p同時出現時,sed可列印選定的內容。例子 plain view plain copy 1 sed my p datafile 預設情況下,sed把所有輸入行都列印在標準...

shell指令碼工具之sed命令

sed就是批處理的流編輯器,可以對來自檔案或標準輸入的輸入流進行轉換,sed通常被用作管道中的過濾器.由於sed僅僅對其輸入進行一遍掃瞄,因此比其它互動式編輯器更加高效.檔案內容 root tong1 opt cat sed.txt 1tong 2 cheng 3 hellow 4word wu h...

Shell 指令碼 Sed命令的使用

sed是乙個很好的檔案處理工具,本身是乙個管道命令,主要是以行為單位進行處理,可以將資料行進行替換 刪除 新增 選取等特定工作,下面先了解一下sed的用法 sed命令列格式為 sed nefri command 輸入文字 常用選項 n 使用安靜 silent 模式。在一般 sed 的用法中,所有來自...