1. 程式人生 > >Sed 語法和基本命令 列印、刪除、和寫入(1)

Sed 語法和基本命令 列印、刪除、和寫入(1)

Sed 代表 Strem Editor(流編輯器),是操作、過濾和轉換文字內容的強大工具。Sed 可以從檔案和管道中讀取輸入。在你的 bash 啟動檔案中,就可能有不少用來設定各種環境的 sed 命令。

1.它打印出/etc/passwd 檔案中的所有行,使用p列印時,必須和-n引數一起用,否則會列印兩遍
sed –n ‘p’ /etc/passwd

[[email protected] /]# sed 'p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
[[email protected] ~]# sed -n 'p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin

2.把多個 sed 命令合併到一個檔案中,這個檔案被稱為 sed 指令碼,然後使用-f 選項呼叫它
列印/etc/passwd 問中以 root 和 nobody0開頭的行:
(使用呼叫的方法)
先編輯一個指令碼檔案
vi test-script.sed
/^root/ p
/^nobody/ p
sed –n –f test-script.sed /etc/passwd

[[email protected] ~]# sed -n -f test-script.sed /etc/passwd
root:x:0:0:root:/root:/bin/bash
nobody:x:99:99:Nobody:/:/sbin/nologin

3.-e引數 的使用方法,它列印/etc/passwd 中以 root 和 nobody 開頭的行
sed -n -e ‘/^root/p’ -e ‘/^nobody/p’ /etc/passwd

[[email protected] ~]# sed -n -e '/^root/p' -e '/^nobody/p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
nobody:X:99:99:Nobody:/:/sbin/nologin

也可以果使用-e 執行多個命令,也可以使用反斜槓\把它們分割到多行執行
sed -n \ 進入模式空間

[[email protected] ~]# sed -n \
> -e '/^root/p' \
> -e '/^nobody/p' \
> /etc/passwd
root:x:0:0:root:/root:/bin/bash
nobody:x:99:99:Nobody:/:/sbin/nologin

4.指定地址範圍
只打印第 2 行

[[email protected] /]# sed -n '2 p' employee.txt
102,Jason Smith,IT Manager

列印第 1 至第 4 行

[[email protected] /]# sed -n '1,4 p' employee.txt
101,John Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer

列印第 2 行至最後一行($代表最後一行

[[email protected] /]# sed -n '2,$ p' employee.txt
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager

5.修改地址範圍
波浪號~也可以指定地址範圍,它指定每次要跳過的行數。如 n~m 表示從第 n 行開始,每次
跳過 m 行。

只打印奇數行

[[email protected] /]# sed -n '1~2 p' employee.txt
101,John Doe,CEO
103,Raj Reddy,Sysadmin
105,Jane Miller,Sales Manager

6.模式匹配
列印匹配模式”Jane”的行

[[email protected] /]# sed -n '/Jane/p' employee.txt
105,Jane Miller,Sales Manager

列印第一次匹配 Jason 的行至第 4 行的內容

[[email protected] /]# sed -n '/Jason/,4 p' employee.txt
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer

列印從第一次匹配 Raj 的行到最後的所有行

[[email protected] /]# sed -n '/Raj/,$ p' employee.txt
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager

列印匹配 Jason 的行和其後面的兩行

[[email protected] /]# sed -n '/Jason/,+2 p' employee.txt
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin

7.刪除行
命令 d 用來刪除行,需要注意的是它只刪除模式空間的內容,和其他 sed 命令一樣,命令 d不會修改原始檔案的內容。

只刪除第 2 行

[[email protected] /]# sed '2d' employee.txt
101,John Doe,CEO
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager

刪除第 1 至第 4 行

[[email protected] /]# sed '2,4d' employee.txt
101,John Doe,CEO
105,Jane Miller,Sales Manager

刪除第 2 行至最後一行

[[email protected] /]# sed '2,$d' employee.txt
101,John Doe,CEO

只刪除奇數行

[[email protected] /]# sed '1~2 d' employee.txt
102,Jason Smith,IT Manager
104,Anand Ram,Developer

刪除從第一次匹配 Jason 的行至第 4 行

[[email protected] /]# sed '/Jason/,4 d' employee.txt
101,John Doe,CEO
105,Jane Miller,Sales Manager

刪除從第一次匹配 Raj 的行至最後一行

[[email protected] /]# sed '/Raj/,$ d' employee.txt
101,John Doe,CEO
102,Jason Smith,IT Manager

刪除第一次匹配 Jason 的行和緊跟著它後面的兩行

[[email protected] /]# sed '/Jason/,+2 d' employee.txt
101,John Doe,CEO
105,Jane Miller,Sales Manager

刪除所有空行
sed ‘/^$/ d’ employee.txt
刪除所有註釋行(假定註釋行以#開頭)
sed ‘/^#/ ‘ employee.txt
注意:如果有多個命令,sed 遇到命令 d 時,會刪除匹配到的整行資料,其餘的命令將無法
操作被刪除的行。

8.把模式空間內容寫到檔案中(w 命令)
把 employee.txt 的內容儲存到檔案 output.txt,同時顯示在螢幕上

[[email protected] /]# sed 'w output.txt' employee.txt
101,John Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager
[[email protected] /]# cat o
opt/        output.txt
[[email protected] /]# cat output.txt
101,John Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager

把 employee.txt 的內容儲存到檔案 output.txt,但不在螢幕上顯示

[[email protected] /]# rm -rf output.txt
[[email protected] /]# sed -n 'w output.txt' employee.txt
[[email protected] /]# cat output.txt
101,John Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager

只儲存第 2 行

[[email protected] /]# rm -rf output.txt
[[email protected] /]# sed -n '2w output.txt' employee.txt
[[email protected] /]# cat output.txt
102,Jason Smith,IT Manager

儲存第 2 行起至最後一行

[[email protected] /]# sed -n '2,$w output.txt' employee.txt
[[email protected] /]# cat output.txt
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager

儲存匹配 Raj 的行至匹配 Jane 的行

[[email protected] /]# sed -n '/Raj/,/Jane/ w  output.txt' employee.txt
[[email protected] /]# cat output.txt
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager

儲存匹配 Jason 的行以及緊跟在其後面的兩行

[[email protected] /]# rm -rf output.txt
[[email protected] /]# sed -n '/Raj/,+2 w  output.txt' employee.txt
[[email protected] /]# cat output.txt
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager