sed 命令詳解及示例
sed是一種流編輯器,能高效地完成各種替換、刪除、插入等操作,按照文件數(shù)據(jù)行順序,重復(fù)處理滿足條件的每一行數(shù)據(jù),然后把結(jié)果展示打印,且不會(huì)改變?cè)募?nèi)容。
sed會(huì)逐行掃描輸入的數(shù)據(jù),并將讀取的數(shù)據(jù)內(nèi)容復(fù)制到臨時(shí)緩沖區(qū)中,稱為“模式空間”(pattern space),然后拿模式空間中的數(shù)據(jù)與給定的條件進(jìn)行匹配,如果匹配成功,則執(zhí)行特定的sed指令,否則跳過(guò)輸入的數(shù)據(jù)行,繼續(xù)讀取后面的數(shù)據(jù)。
一、命令介紹
1.1 命令語(yǔ)法
| | | | --- | --- | | | sed [OPTION]... {script-only-if-no-other-script} [input-file]... | | | | | |#如: | | | sed [選項(xiàng)]'匹配條件和操作指令'文件名 | | |cat文件名 | sed [選項(xiàng)]'匹配條件和操作指令'|
1.2 選項(xiàng)參數(shù)
| | | | --- | --- | | | -n, --quiet, --silent | | |#suppress automatic printing of pattern space | | | --debug #annotate program execution | | | -e script, --expression=script | | |#add the script to the commands to be executed | | | -f script-file, --file=script-file | | |#add the contents of script-file to the commands to be executed | | | --follow-symlinks | | |#follow symlinks when processing in place | | | -i[SUFFIX], --in-place[=SUFFIX] | | |#edit files in place (makes backup if SUFFIX supplied) | | | -l N, --line-length=N | | |#specify the desired line-wrap length for the 'l' command | | | --posix #disable all GNU extensions. | | | -E, -r, --regexp-extended | | |#use extended regular expressions in the script (for portability use POSIX -E). | | | -s, --separate | | |#consider files as separate rather than as a single, continuous long stream. | | | --sandbox | | |#operate in sandbox mode (disable e/r/w commands). | | | -u, --unbuffered | | |#load minimal amounts of data from the input files and flush the output buffers more often | | | -z, --null-data | | |#separate lines by NUL characters | | | --help #display this help and exit | | | --version #output version information and exit |
選項(xiàng) | 例子 |
-n, --quiet, --silent 禁止自動(dòng)打印模式(常配合'p'使用,僅顯示處理后的結(jié)果) |
sed -n '/hello/p' filename 使用 /hello/ 匹配含有 "hello" 的行,p 打印匹配的行 |
--debug 以注解的方式顯示 sed 的執(zhí)行過(guò)程,幫助調(diào)試腳本 |
sed --debug 's/foo/bar/' filename 當(dāng)你使用 sed 修改內(nèi)容時(shí),它會(huì)顯示調(diào)試信息,以便你了解腳本是如何執(zhí)行的 |
-e script, --expression=script 在命令行中直接指定 sed 腳本(允許多個(gè) sed 表達(dá)式) |
sed -e 's/foo/bar/' -e 's/hello/world/' filename 將文件中的 foo 替換為 bar,然后將 hello 替換為 world |
-f script-file, --file=script-file 從指定的腳本文件中讀取 sed 命令 |
sed -f script.sed filename script.sed 是包含多個(gè) sed 命令的腳本文件,sed 會(huì)按順序執(zhí)行這些命令 |
--follow-symlinks 當(dāng)指定 -i 時(shí),sed 會(huì)跟隨符號(hào)鏈接(symlink)指向的實(shí)際文件進(jìn)行編輯 |
sed -i --follow-symlinks 's/foo/bar/' symlink.txt 如果 symlink.txt 是一個(gè)符號(hào)鏈接文件,sed 會(huì)編輯它指向的實(shí)際文件 |
-i[SUFFIX], --in-place[=SUFFIX] 直接編輯文件(如果提供 SUFFIX,則進(jìn)行備份) |
sed -i.bak 's/foo/bar/' filename 直接在 filename 中將 foo 替換為 bar,并創(chuàng)建一個(gè)備份文件 filename.bak |
-l N, --line-length=N 當(dāng)使用 l 命令(列出行內(nèi)容)時(shí),指定輸出的行寬(N 表示字符數(shù)) |
echo 'hello world' | sed -l 5 'l' 使用 l 命令顯示 "hello world",但每行最多顯示 5 個(gè)字符 |
--posix 禁用 GNU 擴(kuò)展,使 sed 遵循 POSIX 標(biāo)準(zhǔn)語(yǔ)法 |
sed --posix 's/foo/bar/' filename 這將禁用 sed 的一些非標(biāo)準(zhǔn)特性,確保腳本在 POSIX 環(huán)境下工作 |
-E, -r, --regexp-extended 使用擴(kuò)展的正則表達(dá)式(ERE),這與基本正則表達(dá)式(BRE)相比,簡(jiǎn)化了一些語(yǔ)法(例如不用轉(zhuǎn)義括號(hào)和 +) |
echo "abc123" | sed -E 's/[a-z]+([0-9]+)/1/' 使用擴(kuò)展正則表達(dá)式,匹配并提取字母后面的數(shù)字 |
-s, --separate 將多個(gè)輸入文件視為獨(dú)立的流,而不是作為一個(gè)連續(xù)的流處理 |
sed -s 's/foo/bar/' file1.txt file2.txt sed 會(huì)分別處理 file1.txt 和 file2.txt,而不是將它們作為一個(gè)整體處理 |
--sandbox 以沙盒模式運(yùn)行,禁止使用 e, r, w 命令,防止 sed 修改文件或執(zhí)行外部命令 |
sed --sandbox 's/foo/bar/' filename 啟用沙盒模式,防止 sed 腳本執(zhí)行危險(xiǎn)的操作 |
-u, --unbuffered 減少?gòu)妮斎胛募x取數(shù)據(jù)時(shí)的緩沖區(qū)大小,并更頻繁地刷新輸出 |
sed -u 's/foo/bar/' filename 立即將處理結(jié)果輸出到標(biāo)準(zhǔn)輸出,而不是等到處理大量數(shù)據(jù)后再輸出 |
-z, --null-data 將輸入中的行分隔符從換行符 改為 NUL 字符 ?,這在處理二進(jìn)制數(shù)據(jù)或以 NUL 作為分隔符的文本時(shí)很有用 |
sed -z 's/foo/bar/' filename 使用 NUL 字符作為行分隔符處理文本 |
1.3 匹配條件
格式 | 描述 |
/regexp/ | 使用 "正則表達(dá)式",匹配數(shù)據(jù)行 |
n(數(shù)字) | 使用 "行號(hào)" 匹配,范圍是1-$($ 表示最后一行) |
addr1,addr2 | 使用 "行號(hào)或正則" 定位,匹配從 addr1 到 addr2 的所有行 |
addr1,+n | 使用 "行號(hào)或正則" 定位,匹配從 addr1 開(kāi)始及后面的 n 行 |
n~step | 使用 "行號(hào)",匹配從行號(hào) n 開(kāi)始,步長(zhǎng)為 step 的所有數(shù)據(jù)行 |
1.3.1 定界符
/ 在sed中作為定界符使用,也可以使用任意的定界符。
| | | | --- | --- | | | sed's|foo|bar|g'filename | | | sed'sbar:g'filename | | | | | |#定界符出現(xiàn)在樣式內(nèi)部時(shí),需要進(jìn)行轉(zhuǎn)義: | | | sed's//bin//usr/bin/g'filename |
1.3.2 變量引用
sed表達(dá)式使用單引號(hào)來(lái)引用,但是如果表達(dá)式內(nèi)部包含"變量"字符串,則需要使用雙引號(hào)。
| | | | --- | --- | | | foo="world"| | |echo"hello world"| sed"s/$foo/librarookie"| | | hello librarookie |
1.4 操作指令
指令 | 描述 | 例子 |
! |