sed
sed - stream editor for filtering and transforming text;
sed
:利用指令碼來處理、編輯文字檔案;
格式
:sed [OPTION]... {script-only-if-no-other-script} [input-file]...
常用引數:
OPTIONS | 意義 |
---|---|
-e或--expression=<script檔案> | 以選項中指定的script來處理輸入的文字檔案 |
-f<script檔案>或--file=<script檔案> | 以選項中指定的script檔案來處理輸入的文字檔案 |
-h或--help | 顯示幫助 |
-n或--quiet或--silent | 僅顯示script處理後的結果 |
-V或--version | 顯示版本資訊 |
-n | 用於禁止列印所有內容 |
G | 在每行資料後插入換行 |
-i | 將sed修改後內容寫入檔案 |
參考案例:
# 案例資料 使用ctrl+c結束輸出
$ cat > content.out
lorem ipsum is a dummy text. lorem ipsum has been the industry's standard dummy text.lorem ipsum has been the industry's standard dummy text.
lorem ipsum is highly used by designers. lorem ipsum is great for developers.
lorem ipsum is used for dummy text. lorem ipsum doesn't have meaning.
learn more about dummy text.
- 單個字串替換(詞或符號)
# 替換匹配的第一個word or characters
# 預設只會替換第一個匹配的lorem,後面的lorem不再替換
# lorem是匹配的詞,Lorem是替換的詞
$ sed 's/lorem/Lorem/' content.out
Lorem ipsum is a dummy text. lorem ipsum has been the industry's standard dummy text.lorem ipsum has been the industry's standard dummy text.
Lorem ipsum is highly used by designers. lorem ipsum is great for developers.
Lorem ipsum is used for dummy text. lorem ipsum doesn't have meaning.
learn more about dummy text.
- 多個字串替換
# 替換匹配的全部word or characters
# 若需要替換匹配的全部則在''之間使用/g
$ sed 's/lorem/Lorem/g' content.out
Lorem ipsum is a dummy text. Lorem ipsum has been the industry's standard dummy text.Lorem ipsum has been the industry's standard dummy text.
Lorem ipsum is highly used by designers. Lorem ipsum is great for developers.
Lorem ipsum is used for dummy text. Lorem ipsum doesn't have meaning.
learn more about dummy text.
- 替換指定位置的單個字串
# 將位置為第二的lorem替換為Lorem
# 在每一行中替換第n個出現的word or characters
# 使用 /1,/2 or n (any number) 指定替換位置
$ sed 's/lorem/Lorem/2' content.out
lorem ipsum is a dummy text. Lorem ipsum has been the industry's standard dummy text.lorem ipsum has been the industry's standard dummy text.
lorem ipsum is highly used by designers. Lorem ipsum is great for developers.
lorem ipsum is used for dummy text. Lorem ipsum doesn't have meaning.
learn more about dummy text.
- 多個字元替換 (類似tr命令)
# 將set1中出現的所有字元替換為set2中對應的字元
# 在''之間使用y引數
$ echo 'a for apple' | sed -e 'y/af/AF/'
A For Apple
- 替換指定開始位置的字串
# 從指定位置2開始往後的lerem都被替換成Lorem
$ sed 's/lorem/Lorem/2g' content.out
lorem ipsum is a dummy text. Lorem ipsum has been the industry's standard dummy text.Lorem ipsum has been the industry's standard dummy text.
lorem ipsum is highly used by designers. Lorem ipsum is great for developers.
lorem ipsum is used for dummy text. Lorem ipsum doesn't have meaning.
learn more about dummy text.
- 指定行替換字串
# 在s前面使用數字 表示指定行替換
# 在下面的命令中,我們可以發現只有第二行被字串'Lorem'取代
$ sed '2 s/lorem/LOREM/' content.out
lorem ipsum is a dummy text. lorem ipsum has been the industry's standard dummy text.lorem ipsum has been the industry's standard dummy text.
LOREM ipsum is highly used by designers. lorem ipsum is great for developers.
lorem ipsum is used for dummy text. lorem ipsum doesn't have meaning.
learn more about dummy text.
- 顯示文字的部分內容
# -n: 用於禁止顯示全部內容
# p: 用於列印特定行
# 顯示2到4行的內容
$ sed -n 2,4p content.out
lorem ipsum is highly used by designers. lorem ipsum is great for developers.
lorem ipsum is used for dummy text. lorem ipsum doesn't have meaning.
learn more about dummy text.
- 顯示指定行外的文字內容
# 顯示除去1到2行外的內容
$ sed 1,2d content.out
lorem ipsum is used for dummy text. lorem ipsum doesn't have meaning.
learn more about dummy text.
- 顯示匹配行外的文字內容
$ sed '/learn/d' content.out
lorem ipsum is a dummy text. lorem ipsum has been the industry's standard dummy text.lorem ipsum has been the industry's standard dummy text.
lorem ipsum is highly used by designers. lorem ipsum is great for developers.
lorem ipsum is used for dummy text. lorem ipsum doesn't have meaning.
- 顯示替換的行
# 顯示被替換的行
# 最後一行learn more about dummy text.沒有匹配lorem就不顯示出來
$ sed -n 's/lorem/Lorem/p' content.out
Lorem ipsum is a dummy text. lorem ipsum has been the industry's standard dummy text.lorem ipsum has been the industry's standard dummy text.
Lorem ipsum is highly used by designers. lorem ipsum is great for developers.
Lorem ipsum is used for dummy text. lorem ipsum doesn't have meaning.
- 'p'和'-n'
# 列印第一行文字內容
$ sed -n '1p' content.out
lorem ipsum is a dummy text. lorem ipsum has been the industry's standard dummy text.lorem ipsum has been the industry's standard dummy text.
# 列印指定範圍行文字內容
# 列印第一行到第四行文字內容
$ sed -n '1,4p' content.out
lorem ipsum is a dummy text. lorem ipsum has been the industry's standard dummy text.lorem ipsum has been the industry's standard dummy text.
lorem ipsum is highly used by designers. lorem ipsum is great for developers.
lorem ipsum is used for dummy text. lorem ipsum doesn't have meaning.
learn more about dummy text.
# 列印多行文字內容
# 列印第一行和第四行文字內容
$ sed -n '1p;4p' content.out
# 列印一個檔案內容
$ sed -n 'p' content.out
lorem ipsum is a dummy text. lorem ipsum has been the industry's standard dummy text.lorem ipsum has been the industry's standard dummy text.
lorem ipsum is highly used by designers. lorem ipsum is great for developers.
lorem ipsum is used for dummy text. lorem ipsum doesn't have meaning.
learn more about dummy text.
# 顯示匹配字串的行
$ sed -n '/lorem/p' content.out
# 輸出包含數字的行
$ sed -n '/[0-9]/p' content.out
# 顯示匹配的行並在前面新增`Matched--`內容
$ sed -n 's/^l/Matched--&/p' content.out
Matched--lorem ipsum is a dummy text. lorem ipsum has been the industry's standard dummy text.lorem ipsum has been the industry's standard dummy text.
Matched--lorem ipsum is highly used by designers. lorem ipsum is great for developers.
Matched--lorem ipsum is used for dummy text. lorem ipsum doesn't have meaning.
Matched--learn more about dummy text.
- 使用sed替代grep
# 使用sed完成grep的功能
$ sed -n '/root/p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
- sed多引數
# 將所有出現的“lorem”替換為“lorem”,並刪除與/learn/搜尋模式匹配的行;
$ sed -e 's/lorem/Lorem/g' -e '/learn/d' content.out
Lorem ipsum is a dummy text. Lorem ipsum has been the industry's standard dummy text.Lorem ipsum has been the industry's standard dummy text.
Lorem ipsum is highly used by designers. Lorem ipsum is great for developers.
Lorem ipsum is used for dummy text. Lorem ipsum doesn't have meaning.
- 插入換行符
# 插入單行換行符
$ sed G content.out
# 插入多行換行符
$ sed 'G;G' content.out
- 備份文字並修改文字內容
# use 'i.<file extension>' for backup file name and -e editing.
# 建立一個原始檔案'content.txt'的備份為'content.txt.bak'
$ sed -i.bak -e 's/lorem/LOREM/g' content.out
# Results
# 修改的檔案
$ cat content.out
LOREM ipsum is a dummy text. LOREM ipsum has been the industry's standard dummy text.LOREM ipsum has been the industry's standard dummy text.
LOREM ipsum is highly used by designers. LOREM ipsum is great for developers.
LOREM ipsum is used for dummy text. LOREM ipsum doesn't have meaning.
learn more about dummy text.
# 備份檔案
$ cat content.out.bak
lorem ipsum is a dummy text. lorem ipsum has been the industry's standard dummy text.lorem ipsum has been the industry's standard dummy text.
lorem ipsum is highly used by designers. lorem ipsum is great for developers.
lorem ipsum is used for dummy text. lorem ipsum doesn't have meaning.
learn more about dummy text.
- 刪除匹配的行
# In the following output, the line with starting 'lorem' and ending with 'text.' is deleted
# 刪除以lerem且test.結尾的行
$ sed -e 's/^lorem.*text.$//g' content.out
LOREM ipsum is a dummy text. LOREM ipsum has been the industry's standard dummy text.LOREM ipsum has been the industry's standard dummy text.
LOREM ipsum is highly used by designers. LOREM ipsum is great for developers.
LOREM ipsum is used for dummy text. LOREM ipsum doesn't have meaning.
learn more about dummy text.
- 新增資料到指定位置
# 可以看到“Here”被新增在每一行的前面
# 新增資料到行首
$ sed -e 's/.*/Here &/' content.out
Here LOREM ipsum is a dummy text. LOREM ipsum has been the industry's standard dummy text.LOREM ipsum has been the industry's standard dummy text.
Here LOREM ipsum is highly used by designers. LOREM ipsum is great for developers.
Here LOREM ipsum is used for dummy text. LOREM ipsum doesn't have meaning.
Here learn more about dummy text.
# 插入字串在每行開始並換行
$ sed 'i \inserted line' content.out
inserted line
LOREM ipsum is a dummy text. LOREM ipsum has been the industry's standard dummy text.LOREM ipsum has been the industry's standard dummy text.
inserted line
LOREM ipsum is highly used by designers. LOREM ipsum is great for developers.
inserted line
LOREM ipsum is used for dummy text. LOREM ipsum doesn't have meaning.
inserted line
learn more about dummy text.
# 插入字串在每行末尾並換行
$ sed 'a \Appended line' content.out
LOREM ipsum is a dummy text. LOREM ipsum has been the industry's standard dummy text.LOREM ipsum has been the industry's standard dummy text.
Appended line
LOREM ipsum is highly used by designers. LOREM ipsum is great for developers.
Appended line
LOREM ipsum is used for dummy text. LOREM ipsum doesn't have meaning.
Appended line
learn more about dummy text.
Appended line
- 提取資料
# 將提取所有可用的linux使用者名稱
$ sed 's/\([^:]*\).*/\1/' /etc/passwd
- 列印不帶註釋#和空行的資料
# 列印不帶註釋和空行的資料
$ sed -e 's/#.*//;/^$/d' content.out
LOREM ipsum is a dummy text. LOREM ipsum has been the industry's standard dummy text.LOREM ipsum has been the industry's standard dummy text.
LOREM ipsum is highly used by designers. LOREM ipsum is great for developers.
LOREM ipsum is used for dummy text. LOREM ipsum doesn't have meaning.
learn more about dummy text.
- 移除帶有註釋#的行
$ sed 's/#.*//' /etc/yum.repos.d/CentOS-Base.repo
- 從文字種提取ip地址
# 新增測試資料
$ vi content.txt
lorem ipsum is a dummy text. lorem ipsum has been the industry's standard dummy text.
lorem ipsum is highly used by designers. lorem ipsum is great for developers.
lorem ipsum is used for dummy text. lorem ipsum doesn't have meaning.
learn more about dummy text.
122.65.49.2
221.11.165.233
219.158.9.97
219.158.19.137
# 複雜版 - 提取ip地址
$ sed '/\n/!s/[0-9.]\+/\n&\n/;/^\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\n/P;D' content.txt
# 簡化版 - 提取ip地址
$ sed -n '/[0-9]/p' content.txt
# 使用管道(|)將sed與其他命令組合使用
$ ip addr | sed -n '/inet/p' |sed -e 's/ */ /g'|cut -d ' ' -f3
127.0.0.1/8
::1/128
192.168.188.188/24
fe80::400e:cc35:e4ab:8c3f/64
172.17.0.1/16
fe80::42:65ff:fee9:cfb0/64
fe80::4cf2:b7ff:feb4:44fa/64
fe80::4478:34ff:fe5b:8d9a/64
- 資料提取並重定向並寫到新的檔案中
$ sed -n '/root/w rootpwd.txt' /etc/passwd
$ cat rootpwd.txt
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin