linux之SED行編輯器

2021-10-01 14:47:57 字數 2125 閱讀 1336

(-n):靜默模式,不再預設顯示模式空間中的內容

(-i):直接修改原檔案

(-e script -e script):可以同時執行多個指令碼

(-f /path/to/sed_script)

(-r):表示使用擴充套件正規表示式

(d):刪除符合條件的行

(p):顯示符合條件的行

(a\string):在指定的行後面追加新行,內容為string

(\n):可以用於換行

(i\string):在指定的行前面新增新行,內容為string

(r file):將指定的檔案的內容新增到符合條件的行處

(s/pattern/string/修飾符):查詢並替換,預設只替換每行中第一次被模式匹配到的字串

.g:行內全域性替換

.i:忽略字元大小寫

.s//

/:s###,s@@@

.\(\)

,\1,\2

[root@localhost a]# cat b.txt

banana 12

orange 8

(複製第二行的內容,不修改原檔案)

[root@localhost a]# sed '2p' b.txt

banana 12

orange 8

(複製第二行的內容,並只顯示複製的內容,不修改原檔案)

[root@localhost a]# sed -n '2p' b.txt

(刪除第三行的內容,不修改原檔案)

[root@localhost a]# sed '3d' b.txt

banana 12

(在第2行下新增一行內容,不修改原檔案)

[root@localhost a]# sed '2a\wukong' b.txt

banana 12

wukong

orange 8

(在第2行下新增一行內容,修改原檔案)

[root@localhost a]# sed -i '2a\wukong' b.txt

[root@localhost a]# cat b.txt

banana 12

wukong

orange 8

(替換wukong字元為tianxia,不修改原檔案)

[root@localhost a]# sed 's/wukong/tianxia/' b.txt

banana 12

tianxia

orange 8

(刪除wukong字元,不修改原檔案)

[root@localhost a]# sed '/wukong/d' b.txt

banana 12

orange 8

[root@localhost a]# cat b.txt

banana 12

wukong 9 wujing

orange 8

(替換wukong 9 wujing中的數值,不修改原檔案)

[root@localhost a]# sed 's/\(wukong \)[0-9]\( wujing\)/\1555\2/' b.txt

banana 12

wukong 555 wujing

orange 8

[root@localhost a]# cat c.txt 

ipaddr=192.168.17.101

netmask=255.255.255.0

gateway=192.168.917.1

dns1=8.8.8.8

dns2=114.114.114.114

[root@localhost a]# sed "s/\(ipaddr=\(\([0-9]\|[1-9][0-9]\|1[0-9][0-9]\|2[0-4][0-9]\|[25[0-5]\)\.\)\\).*/\188/" c.txt

ipaddr=192.168.17.88

netmask=255.255.255.0

gateway=192.168.917.1

dns1=8.8.8.8

dns2=114.114.114.114

Shell指令碼之Sed編輯器

插入sed是一種流編輯器,流編輯器會在編輯器處理資料之前基於預先提供的一組規則來編輯資料流。可以根據命令來處理資料流中的資料,這些命令要麼從命令列中輸入,要麼儲存在乙個命令文字檔案中。主要包括讀取 執行和顯示三個過程。sed e 操作 檔案1 檔案2 sed n e 操作 檔案1 檔案2 sed f...

Linux基礎 sed 流編輯器

sed stream editer 流編輯器 sed r 擴充套件正澤 可以用小括號 n 靜默模式 不但輸出 i 直接寫入 sed 3d test 第三行 sed 3c 11111 test 第三行改成11111 sed 1d 3d test 刪除第一行 第三行 sed 1,3d test 刪除第一...

sed編輯器 二

更改行 工作方式與插入命令相同。1 sed 3c this is a test.更改第三行中的文字。2 sed number 3 c this is a changed line of text.fiel 定址匹配文字模式 3 sed 2,3c this is a new line of text....