日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > linux >内容正文

linux

Linux学习笔记Sed最全整理

發(fā)布時間:2025/3/15 linux 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Linux学习笔记Sed最全整理 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

本文分為四部分,前兩部分都是比較基礎(chǔ)的用法。
如果你對Sed感興趣的話,可以去嘗試讀一下后面幾個章節(jié)。
你完全可以根據(jù)你的水平去選擇其中的某一個章節(jié)閱讀。

目錄

    • 目錄
  • 初級入門
    • 主要應(yīng)用場景
    • 刪除
    • 查找替換
    • 字符轉(zhuǎn)換
    • 插入文本
  • 鳥哥私房菜
  • Sed到底是如何工作的
  • 高級用法
    • 高級應(yīng)用實例
    • 編號
    • 文本轉(zhuǎn)換和替代
    • 選擇性地顯示特定行
    • 選擇性地刪除特定行
    • 特殊應(yīng)用
  • BSD版本Sed的文檔

初級入門

sed工具是一種非交互式的流編輯器。默認(rèn)情況下只會影響輸出,不會改變輸入。sed處理文檔時是以行為單位的。功能有:刪除、查找替換、添加、插入、從其他文件讀取。

其實這些功能看起來都可以用vim等編輯器來實現(xiàn)。那么,為什么要有sed呢?

主要應(yīng)用場景

  • 太過龐大的文本
  • 有規(guī)律的文本修改

sed的命令格式

sed [option] command [file ...]

刪除

#刪除第一行 sed '1d' file

注意,這只影響到輸出流。如果想保存的話

sed -i '1d' filename

或者

#輸出到新文件 sed '1d'>newfilename

其中1d命令中,我們把1稱為地址,這里指代的是第一行。
刪除第一行到最后一行

sed '1,$d' filename

刪除包含了某個pattern的行

sed '/pattern/d' filename #例如 sed '/^$/d' filename

查找替換

#普通替換 將每行的第一個line替換成LINE sed 's/line/LINE/' filename

sed ‘s/line/LINE/[number]
表示對這一行來說至多替換number個line,如果number為g,則全部替換


字符轉(zhuǎn)換

現(xiàn)在還沒有見過重要的用法

插入文本

#在第二行前插入一行 sed '2 i insert_context' filename #在第二行之后插入一行 sed '2 a insert_context' filename #在匹配的行之前插入一行 sed '/pattern/i\new_word' filename

打印

#只打印出第一行 ,不加n的話會默認(rèn)輸出每一行 sed -n '1p' filename #只打印出被修改的一行 sed -n 's/the/THE/p' filename

鳥哥私房菜

sed 是一種在線編輯器,它一次處理一行內(nèi)容。處理時,把當(dāng)前處理的行存儲在臨時緩沖區(qū)中,稱為“模式空間”(pattern space),接著用sed命令處理緩沖區(qū)中的內(nèi)容,處理完成后,把緩沖區(qū)的內(nèi)容送往屏幕。接著處理下一行,這樣不斷重復(fù),直到文件末尾。文件內(nèi)容并沒有 改變,除非你使用重定向存儲輸出。Sed主要用來自動編輯一個或多個文件;簡化對文件的反復(fù)操作;編寫轉(zhuǎn)換程序等。


[root@www ~]# sed [-nefr] [動作]
選項與參數(shù):
-n :使用安靜(silent)模式。在一般 sed 的用法中,所有來自 STDIN 的數(shù)據(jù)一般都會被列出到終端上。但如果加上 -n 參數(shù)后,則只有經(jīng)過sed 特殊處理的那一行(或者動作)才會被列出來。
-e :直接在命令列模式上進(jìn)行 sed 的動作編輯;
-f :直接將 sed 的動作寫在一個文件內(nèi), -f filename 則可以運(yùn)行 filename 內(nèi)的 sed 動作;
-r :sed 的動作支持的是延伸型正規(guī)表示法的語法。(默認(rèn)是基礎(chǔ)正規(guī)表示法語法)
-i :直接修改讀取的文件內(nèi)容,而不是輸出到終端。

動作說明: [n1[,n2]]function
n1, n2 :不見得會存在,一般代表『選擇進(jìn)行動作的行數(shù)』,舉例來說,如果我的動作是需要在 10 到 20 行之間進(jìn)行的,則『 10,20[動作行為] 』

function:
a :新增, a 的后面可以接字串,而這些字串會在新的一行出現(xiàn)(目前的下一行)~
c :取代, c 的后面可以接字串,這些字串可以取代 n1,n2 之間的行!
d :刪除,因為是刪除啊,所以 d 后面通常不接任何咚咚;
i :插入, i 的后面可以接字串,而這些字串會在新的一行出現(xiàn)(目前的上一行);
p :列印,亦即將某個選擇的數(shù)據(jù)印出。通常 p 會與參數(shù) sed -n 一起運(yùn)行~
s :取代,可以直接進(jìn)行取代的工作哩!通常這個 s 的動作可以搭配正規(guī)表示法!例如 1,20s/old/new/g 就是啦!
sed使用參數(shù)

#以行為單位的新增/刪除#將 /etc/passwd 的內(nèi)容列出并且列印行號,同時,請將第 2~5 行刪除! [root@www ~]# nl /etc/passwd | sed '2,5d' 1 root:x:0:0:root:/root:/bin/bash 6 sync:x:5:0:sync:/sbin:/bin/sync 7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown .....(后面省略)..... #只要刪除第 2 行 nl /etc/passwd | sed '2d' #要刪除第 3 到最后一行 nl /etc/passwd | sed '3,$d' #在第二行后(亦即是加在第三行)加上『drink tea?』字樣! nl /etc/passwd | sed '2 a drink tea' #如果是要增加兩行以上,在第二行后面加入兩行字,例如『Drink tea or .....』與『drink beer?』 nl /etc/passwd | sed '2 a drind tea or\ >drink beer?' #將第2-5行的內(nèi)容取代成為『No 2-5 number』呢? nl /etc/passwd|sed '2,5c No 2-5 number' #數(shù)據(jù)的搜尋并刪除 #刪除/etc/passwd所有包含root的行,其他行輸出 nl /etc/passwd | sed -n '/root/d'

重點,命令的拼接

#數(shù)據(jù)的搜尋并執(zhí)行命令 #找到匹配模式eastern的行后,#搜索/etc/passwd,找到root對應(yīng)的行,執(zhí)行后面花括號中的一組命令,每個命令之間用分號分隔,這里把bash替換為blueshell,再輸出這行: nl /etc/passwd | sed '/root/{s/bash/blueshell;p}'

多點編輯

#多點編輯 #一條sed命令,刪除/etc/passwd第三行到末尾的數(shù)據(jù),并把bash替換為#blueshell nl /etc/passwd | sed -e '3,$d' -e 's/bash/blueshell'

直接修改文件內(nèi)容(危險動作)

sed 可以直接修改文件的內(nèi)容,不必使用管道命令或數(shù)據(jù)流重導(dǎo)向! 不過,由於這個動作會直接修改到原始的文件,所以請你千萬不要隨便拿系統(tǒng)配置來測試! 我們還是使用下載的 regular_express.txt 文件來測試看看吧!

利用 sed 將 regular_express.txt 內(nèi)每一行結(jié)尾若為 . 則換成 !

#注意,由于.和!都是正則表達(dá)符,所以需要轉(zhuǎn)義 sed -i 's/\.$/\!/g regular_express.txt'

利用 sed 直接在 regular_express.txt 最后一行加入『# This is a test』

sed -i '$i #this is a test' regular_express.txt

Sed到底是如何工作的?

這里有一個比較不錯的文章,可以看到Linux和Unix版本之間的區(qū)別以及簡略的用法。
如果沒有耐心看看上面的內(nèi)容是完全足夠了的。

本文作者的水平實在平平。只能試著翻譯一下文檔。但是如果沒有辦法讀原始文檔,那么我就只能永遠(yuǎn)看翻譯過來的不知所云的文章。我認(rèn)為這一步是必須的。

可能文檔也并不足夠,有些概念沒有解釋清楚。先看看這段話再讀文檔好了:)

How `sed' Works ===================`sed' maintains two data buffers: the active _pattern_ space, and the auxiliary _hold_ space. Both are initially empty.`sed' operates by performing the following cycle on each lines of input: first, `sed' reads one line from the input stream, removes any trailing newline, and places it in the pattern space. Then commands are executed; each command can have an address associated to it:addresses are a kind of condition code, and a command is only executed if the condition is verified before the command is to be executed.When the end of the script is reached, unless the `-n' option is in use, the contents of pattern space are printed out to the output stream, adding back the trailing newline if it was removed.(1) Then the next cycle starts for the next input line.Unless special commands (like `D') are used, the pattern space is deleted between two cycles. The hold space, on the other hand, keeps its data between cycles (see commands `h', `H', `x', `g', `G' to move data between both buffers).---------- Footnotes ----------(1) Actually, if `sed' prints a line without the terminating newline, it will nevertheless print the missing newline as soon as more text is sent to the same output stream, which gives the "least expected surprise" even though it does not make commands like `sed -n p' exactly identical to `cat'.

簡單地說就是:

先讀入一行,去掉尾部換行,把處理過的這行存入pattern space這個變量,再當(dāng)空間里的某些地址存在時,執(zhí)行命令 這兩步執(zhí)行完畢,把現(xiàn)在的pattern space打印出來,在后邊打印曾去掉的換行 把pattern space內(nèi)容給hold space,把pattern space置空 讀下一行

有了pattern space和hold space的基礎(chǔ),讀下面這篇文檔就會輕松很多。

高級用法

如果你沒有看上面的文檔,那么看看這篇《【轉(zhuǎn)】sed命令n,N,d,D,p,P,h,H,g,G,x解析》

1、sed執(zhí)行模板=sed '模式{命令1;命令2}'即逐行讀入模式空間,執(zhí)行命令,最后輸出打印出來2、為方便下面,先說下p和P,p打印當(dāng)前模式空間內(nèi)容,追加到默認(rèn)輸出之后,P打印當(dāng)前模式空間開端至\n的內(nèi)容,并追加到默認(rèn)輸出之前。sed并不對每行末尾\n進(jìn)行處理,但是對N命令追加的行間\n進(jìn)行處理,因為此時sed將兩行看做一行。2-1、n命令n命令簡單來說就是提前讀取下一行,覆蓋模型空間前一行(并沒有刪除,因此依然打印至標(biāo)準(zhǔn)輸出),如果命令未執(zhí)行成功(并非跳過:前端條件不匹配),則放棄之后的任何命令,并對新讀取的內(nèi)容,重頭執(zhí)行sed。2-2N命令N命令簡單來說就是追加下一行到模式空間,同時將兩行看做一行,但是兩行之間依然含有\(zhòng)n換行符,如果命令未執(zhí)行成功(并非跳過:前端條件不匹配),則放棄之后任何命令,并對新讀取的內(nèi)容,重頭執(zhí)行sed。2-3、d命令d命令是刪除當(dāng)前模式空間內(nèi)容(不在傳至標(biāo)準(zhǔn)輸出),并放棄之后的命令,并對新讀取的內(nèi)容,重頭執(zhí)行sed。2-4D命令D命令是刪除當(dāng)前模式空間開端至\n的內(nèi)容(不在傳至標(biāo)準(zhǔn)輸出),放棄之后的命令,但是對剩余模式空間重新執(zhí)行sed。2-5、y命令y命令的作用在于字符轉(zhuǎn)換2-6、h命令,H命令,g命令,G命令h命令是將當(dāng)前模式空間中內(nèi)容覆蓋至保持空間,H命令是將當(dāng)前模式空間中的內(nèi)容追加至保持空間g命令是將當(dāng)前保持空間中內(nèi)容覆蓋至模式空間,G命令是將當(dāng)前保持空間中的內(nèi)容追加至模式空間2-7、x命令x命令是將當(dāng)前保持空間和模式空間內(nèi)容互換

高級應(yīng)用實例

# 在每一行后面增加一空行# 因為holdspace一直為空,所以G命令只是在加空行罷了sed G# 將原來的所有空行刪除并在每一行后面增加一空行。# 這樣在輸出的文本中每一行后面將有且只有一空行。sed '/^$/d;G'# 在每一行后面增加兩行空行sed 'G;G'# 將第一個腳本所產(chǎn)生的所有空行刪除(即刪除所有偶數(shù)行)sed 'n;d'# 在匹配式樣“regex”的行之前插入一空行sed '/regex/{x;p;x;}'# 在匹配式樣“regex”的行之后插入一空行sed '/regex/G'# 在匹配式樣“regex”的行之前和之后各插入一空行sed '/regex/{x;p;x;G;}'

編號

N
Append the next line of input to the pattern space, using an embedded newline character to separate the appended material from the original contents. Note that the current line number changes.

# 為文件中的每一行進(jìn)行編號(簡單的左對齊方式)。這里使用了“制表符”# (tab,見本文末尾關(guān)于'\t'的用法的描述)而不是空格來對齊邊緣。sed = filename | sed 'N;s/\n/\t/'# 對文件中的所有行編號(行號在左,文字右端對齊)。sed = filename | sed 'N; s/^/ /; s/ *\(.\{6,\}\)\n/\1 /'# 對文件中的所有行編號,但只顯示非空白行的行號。sed '/./=' filename | sed '/./N; s/\n/ /'# 計算行數(shù) (模擬 "wc -l")sed -n '$='

文本轉(zhuǎn)換和替代:

--------# Unix環(huán)境:轉(zhuǎn)換DOS的新行符(CR/LF)為Unix格式。sed 's/.$//' # 假設(shè)所有行以CR/LF結(jié)束sed 's/^M$//' # 在bash/tcsh中,將按Ctrl-M改為按Ctrl-Vsed 's/\x0D$//' # ssed、gsed 3.02.80,及更高版本# Unix環(huán)境:轉(zhuǎn)換Unix的新行符(LF)為DOS格式。sed "s/$/`echo -e \\\r`/" # 在ksh下所使用的命令sed 's/$'"/`echo \\\r`/" # 在bash下所使用的命令sed "s/$/`echo \\\r`/" # 在zsh下所使用的命令sed 's/$/\r/' # gsed 3.02.80 及更高版本# DOS環(huán)境:轉(zhuǎn)換Unix新行符(LF)為DOS格式。sed "s/$//" # 方法 1sed -n p # 方法 2# DOS環(huán)境:轉(zhuǎn)換DOS新行符(CR/LF)為Unix格式。# 下面的腳本只對UnxUtils sed 4.0.7 及更高版本有效。要識別UnxUtils版本的# sed可以通過其特有的“--text”選項。你可以使用幫助選項(“--help”)看# 其中有無一個“--text”項以此來判斷所使用的是否是UnxUtils版本。其它DOS# 版本的的sed則無法進(jìn)行這一轉(zhuǎn)換。但可以用“tr”來實現(xiàn)這一轉(zhuǎn)換。sed "s/\r//" infile >outfile # UnxUtils sed v4.0.7 或更高版本tr -d \r infile >outfile # GNU tr 1.22 或更高版本# 將每一行前導(dǎo)的“空白字符”(空格,制表符)刪除# 使之左對齊sed 's/^[ \t]*//' # 見本文末尾關(guān)于'\t'用法的描述# 將每一行拖尾的“空白字符”(空格,制表符)刪除sed 's/[ \t]*$//' # 見本文末尾關(guān)于'\t'用法的描述# 將每一行中的前導(dǎo)和拖尾的空白字符刪除sed 's/^[ \t]*//;s/[ \t]*$//'# 在每一行開頭處插入5個空格(使全文向右移動5個字符的位置)sed 's/^/ /'# 以79個字符為寬度,將所有文本右對齊sed -e :a -e 's/^.\{1,78\}$/ &/;ta' # 78個字符外加最后的一個空格# 以79個字符為寬度,使所有文本居中。在方法1中,為了讓文本居中每一行的前# 頭和后頭都填充了空格。 在方法2中,在居中文本的過程中只在文本的前面填充# 空格,并且最終這些空格將有一半會被刪除。此外每一行的后頭并未填充空格。sed -e :a -e 's/^.\{1,77\}$/ & /;ta' # 方法1sed -e :a -e 's/^.\{1,77\}$/ &/;ta' -e 's/\( *\)\1/\1/' # 方法2# 在每一行中查找字串“foo”,并將找到的“foo”替換為“bar”sed 's/foo/bar/' # 只替換每一行中的第一個“foo”字串sed 's/foo/bar/4' # 只替換每一行中的第四個“foo”字串sed 's/foo/bar/g' # 將每一行中的所有“foo”都換成“bar”sed 's/\(.*\)foo\(.*foo\)/\1bar\2/' # 替換倒數(shù)第二個“foo”sed 's/\(.*\)foo/\1bar/' # 替換最后一個“foo”# 只在行中出現(xiàn)字串“baz”的情況下將“foo”替換成“bar”sed '/baz/s/foo/bar/g'# 將“foo”替換成“bar”,并且只在行中未出現(xiàn)字串“baz”的情況下替換sed '/baz/!s/foo/bar/g'# 不管是“scarlet”“ruby”還是“puce”,一律換成“red”sed 's/scarlet/red/g;s/ruby/red/g;s/puce/red/g' #對多數(shù)的sed都有效gsed 's/scarlet\|ruby\|puce/red/g' # 只對GNU sed有效# 倒置所有行,第一行成為最后一行,依次類推(模擬“tac”)。# 由于某些原因,使用下面命令時HHsed v1.5會將文件中的空行刪除sed '1!G;h;$!d' # 方法1sed -n '1!G;h;$p' # 方法2# 將行中的字符逆序排列,第一個字成為最后一字,……(模擬“rev”)sed '/\n/!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/.//'# 將每兩行連接成一行(類似“paste”)sed '$!N;s/\n/ /'# 如果當(dāng)前行以反斜杠“\”結(jié)束,則將下一行并到當(dāng)前行末尾# 并去掉原來行尾的反斜杠sed -e :a -e '/\\$/N; s/\\\n//; ta'# 如果當(dāng)前行以等號開頭,將當(dāng)前行并到上一行末尾# 并以單個空格代替原來行頭的“=”sed -e :a -e '$!N;s/\n=/ /;ta' -e 'P;D'# 為數(shù)字字串增加逗號分隔符號,將“1234567”改為“1,234,567”gsed ':a;s/\B[0-9]\{3\}\>/,&/;ta' # GNU sedsed -e :a -e 's/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta' # 其他sed# 為帶有小數(shù)點和負(fù)號的數(shù)值增加逗號分隔符(GNU sed)gsed -r ':a;s/(^|[^0-9.])([0-9]+)([0-9]{3})/\1\2,\3/g;ta'# 在每5行后增加一空白行 (在第5,10,15,20,等行后增加一空白行)gsed '0~5G' # 只對GNU sed有效sed 'n;n;n;n;G;' # 其他sed

選擇性地顯示特定行

q命令
退出循環(huán)

--------# 顯示文件中的前10行 (模擬“head”的行為)sed 10q# 顯示文件中的第一行 (模擬“head -1”命令)sed q# 顯示文件中的最后10行 (模擬“tail”)sed -e :a -e '$q;N;11,$D;ba'# 顯示文件中的最后2行(模擬“tail -2”命令)sed '$!N;$!D'# 顯示文件中的最后一行(模擬“tail -1”)sed '$!d' # 方法1sed -n '$p' # 方法2# 顯示文件中的倒數(shù)第二行sed -e '$!{h;d;}' -e x # 當(dāng)文件中只有一行時,輸入空行sed -e '1{$q;}' -e '$!{h;d;}' -e x # 當(dāng)文件中只有一行時,顯示該行sed -e '1{$d;}' -e '$!{h;d;}' -e x # 當(dāng)文件中只有一行時,不輸出# 只顯示匹配正則表達(dá)式的行(模擬“grep”)sed -n '/regexp/p' # 方法1sed '/regexp/!d' # 方法2# 只顯示“不”匹配正則表達(dá)式的行(模擬“grep -v”)sed -n '/regexp/!p' # 方法1,與前面的命令相對應(yīng)sed '/regexp/d' # 方法2,類似的語法# 查找“regexp”并將匹配行的上一行顯示出來,但并不顯示匹配行sed -n '/regexp/{g;1!p;};h'# 查找“regexp”并將匹配行的下一行顯示出來,但并不顯示匹配行sed -n '/regexp/{n;p;}'# 顯示包含“regexp”的行及其前后行,并在第一行之前加上“regexp”所# 在行的行號 (類似“grep -A1 -B1”)sed -n -e '/regexp/{=;x;1!p;g;$!N;p;D;}' -e h# 顯示包含“AAA”、“BBB”或“CCC”的行(任意次序)sed '/AAA/!d; /BBB/!d; /CCC/!d' # 字串的次序不影響結(jié)果# 顯示包含“AAA”、“BBB”和“CCC”的行(固定次序)sed '/AAA.*BBB.*CCC/!d'# 顯示包含“AAA”“BBB”或“CCC”的行 (模擬“egrep”)sed -e '/AAA/b' -e '/BBB/b' -e '/CCC/b' -e d # 多數(shù)sedgsed '/AAA\|BBB\|CCC/!d' # 對GNU sed有效# 顯示包含“AAA”的段落 (段落間以空行分隔)# HHsed v1.5 必須在“x;”后加入“G;”,接下來的3個腳本都是這樣sed -e '/./{H;$!d;}' -e 'x;/AAA/!d;'# 顯示包含“AAA”“BBB”和“CCC”三個字串的段落 (任意次序)sed -e '/./{H;$!d;}' -e 'x;/AAA/!d;/BBB/!d;/CCC/!d'# 顯示包含“AAA”、“BBB”、“CCC”三者中任一字串的段落 (任意次序)sed -e '/./{H;$!d;}' -e 'x;/AAA/b' -e '/BBB/b' -e '/CCC/b' -e dgsed '/./{H;$!d;};x;/AAA\|BBB\|CCC/b;d' # 只對GNU sed有效# 顯示包含65個或以上字符的行sed -n '/^.\{65\}/p'# 顯示包含65個以下字符的行sed -n '/^.\{65\}/!p' # 方法1,與上面的腳本相對應(yīng)sed '/^.\{65\}/d' # 方法2,更簡便一點的方法# 顯示部分文本——從包含正則表達(dá)式的行開始到最后一行結(jié)束sed -n '/regexp/,$p'# 顯示部分文本——指定行號范圍(從第8至第12行,含8和12行)sed -n '8,12p' # 方法1sed '8,12!d' # 方法2# 顯示第52行sed -n '52p' # 方法1sed '52!d' # 方法2sed '52q;d' # 方法3, 處理大文件時更有效率# 從第3行開始,每7行顯示一次gsed -n '3~7p' # 只對GNU sed有效sed -n '3,${p;n;n;n;n;n;n;}' # 其他sed# 顯示兩個正則表達(dá)式之間的文本(包含)sed -n '/Iowa/,/Montana/p' # 區(qū)分大小寫方式

選擇性地刪除特定行

--------# 顯示通篇文檔,除了兩個正則表達(dá)式之間的內(nèi)容sed '/Iowa/,/Montana/d'# 刪除文件中相鄰的重復(fù)行(模擬“uniq”)# 只保留重復(fù)行中的第一行,其他行刪除sed '$!N; /^\(.*\)\n\1$/!P; D'# 刪除文件中的重復(fù)行,不管有無相鄰。注意hold space所能支持的緩存# 大小,或者使用GNU sed。sed -n 'G; s/\n/&&/; /^\([ -~]*\n\).*\n\1/d; s/\n//; h; P'# 刪除除重復(fù)行外的所有行(模擬“uniq -d”)sed '$!N; s/^\(.*\)\n\1$/\1/; t; D'# 刪除文件中開頭的10行sed '1,10d'# 刪除文件中的最后一行sed '$d'# 刪除文件中的最后兩行sed 'N;$!P;$!D;$d'# 刪除文件中的最后10行sed -e :a -e '$d;N;2,10ba' -e 'P;D' # 方法1sed -n -e :a -e '1,10!{P;N;D;};N;ba' # 方法2# 刪除8的倍數(shù)行gsed '0~8d' # 只對GNU sed有效sed 'n;n;n;n;n;n;n;d;' # 其他sed# 刪除匹配式樣的行sed '/pattern/d' # 刪除含pattern的行。當(dāng)然pattern# 可以換成任何有效的正則表達(dá)式# 刪除文件中的所有空行(與“grep '.' ”效果相同)sed '/^$/d' # 方法1sed '/./!d' # 方法2# 只保留多個相鄰空行的第一行。并且刪除文件頂部和尾部的空行。# (模擬“cat -s”)sed '/./,/^$/!d' #方法1,刪除文件頂部的空行,允許尾部保留一空行sed '/^$/N;/\n$/D' #方法2,允許頂部保留一空行,尾部不留空行# 只保留多個相鄰空行的前兩行。sed '/^$/N;/\n$/N;//D'# 刪除文件頂部的所有空行sed '/./,$!d'# 刪除文件尾部的所有空行sed -e :a -e '/^\n*$/{$d;N;ba' -e '}' # 對所有sed有效sed -e :a -e '/^\n*$/N;/\n$/ba' # 同上,但只對 gsed 3.02.*有效# 刪除每個段落的最后一行sed -n '/^$/{p;h;};/./{x;/./p;}'

特殊應(yīng)用

--------# 移除手冊頁(man page)中的nroff標(biāo)記。在Unix System V或bash shell下使# 用'echo'命令時可能需要加上 -e 選項。sed "s/.`echo \\\b`//g" # 外層的雙括號是必須的(Unix環(huán)境)sed 's/.^H//g' # 在bash或tcsh中, 按 Ctrl-V 再按 Ctrl-Hsed 's/.\x08//g' # sed 1.5,GNU sed,ssed所使用的十六進(jìn)制的表示方法# 提取新聞組或 e-mail 的郵件頭sed '/^$/q' # 刪除第一行空行后的所有內(nèi)容# 提取新聞組或 e-mail 的正文部分sed '1,/^$/d' # 刪除第一行空行之前的所有內(nèi)容# 從郵件頭提取“Subject”(標(biāo)題欄字段),并移除開頭的“Subject:”字樣sed '/^Subject: */!d; s///;q'# 從郵件頭獲得回復(fù)地址sed '/^Reply-To:/q; /^From:/h; /./d;g;q'# 獲取郵件地址。在上一個腳本所產(chǎn)生的那一行郵件頭的基礎(chǔ)上進(jìn)一步的將非電郵# 地址的部分剃除。(見上一腳本)sed 's/ *(.*)//; s/>.*//; s/.*[:# 在每一行開頭加上一個尖括號和空格(引用信息)sed 's/^/> /'# 將每一行開頭處的尖括號和空格刪除(解除引用)sed 's/^> //'# 移除大部分的HTML標(biāo)簽(包括跨行標(biāo)簽)sed -e :a -e 's/]*>//g;/# 將分成多卷的uuencode文件解碼。移除文件頭信息,只保留uuencode編碼部分。# 文件必須以特定順序傳給sed。下面第一種版本的腳本可以直接在命令行下輸入;# 第二種版本則可以放入一個帶執(zhí)行權(quán)限的shell腳本中。(由Rahul Dhesi的一# 個腳本修改而來。)sed '/^end/,/^begin/d' file1 file2 ... fileX | uudecode # vers. 1sed '/^end/,/^begin/d' "$@" | uudecode # vers. 2# 將文件中的段落以字母順序排序。段落間以(一行或多行)空行分隔。GNU sed使用# 字元“\v”來表示垂直制表符,這里用它來作為換行符的占位符——當(dāng)然你也可以# 用其他未在文件中使用的字符來代替它。sed '/./{H;d;};x;s/\n/={NL}=/g' file | sort | sed '1s/={NL}=//;s/={NL}=/\n/g'gsed '/./{H;d};x;y/\n/\v/' file | sort | sed '1s/\v//;y/\v/\n/'# 分別壓縮每個.TXT文件,壓縮后刪除原來的文件并將壓縮后的.ZIP文件# 命名為與原來相同的名字(只是擴(kuò)展名不同)。(DOS環(huán)境:“dir /b”# 顯示不帶路徑的文件名)。echo @echo off >zipup.batdir /b *.txt | sed "s/^\(.*\)\.TXT/pkzip -mo \1 \1.TXT/" >>zipup.bat使用SED:Sed接受一個或多個編輯命令,并且每讀入一行后就依次應(yīng)用這些命令。當(dāng)讀入第一行輸入后,sed對其應(yīng)用所有的命令,然后將結(jié)果輸出。接著再讀入第二行輸入,對其應(yīng)用所有的命令……并重復(fù)這個過程。上一個例子中sed由標(biāo)準(zhǔn)輸入設(shè)備(即命令解釋器,通常是以管道輸入的形式)獲得輸入。在命令行給出一個或多個文件名作為參數(shù)時,這些文件取代標(biāo)準(zhǔn)輸入設(shè)備成為sed的輸入。sed的輸出將被送到標(biāo)準(zhǔn)輸出(顯示器)。因此:cat filename | sed '10q' # 使用管道輸入sed '10q' filename # 同樣效果,但不使用管道輸入sed '10q' filename > newfile # 將輸出轉(zhuǎn)移(重定向)到磁盤上 要了解sed命令的使用說明,包括如何通過腳本文件(而非從命令行)來使用這些命 令,請參閱《sed & awk》第二版,作者Dale Dougherty和Arnold Robbins (O'Reilly,1997;http://www.ora.com),《UNIX Text Processing》,作者 Dale Dougherty和Tim O'Reilly(Hayden Books,1987)或者是Mike Arst寫的教程——壓縮包的名稱是“U-SEDIT2.ZIP”(在許多站點上都找得到)。要發(fā)掘sed的潛力,則必須對“正則表達(dá)式”有足夠的理解。正則表達(dá)式的資料可以看《Mastering Regular Expressions》作者Jeffrey Friedl(O'reilly 1997)。 Unix系統(tǒng)所提供的手冊頁(“man”)也會有所幫助(試一下這些命令 “man sed”、“man regexp”,或者看“man ed”中關(guān)于正則表達(dá)式的部分),但手冊提供的信息比較“抽象”——這也是它一直為人所詬病的。不過,它本來就不 是用來教初學(xué)者如何使用sed或正則表達(dá)式的教材,而只是為那些熟悉這些工具的人提供的一些文本參考。 括號語法:前面的例子對sed命令基本上都使用單引號('...')而非雙引號("...")這是因為sed通常是在Unix平臺上使用。單引號下,Unix的shell(命令解釋器)不會對美元符($)和后引號(`...`)進(jìn)行解釋和執(zhí)行。而在雙引號下美元符會被展開為變量或參數(shù)的值,后引號中的命令被執(zhí)行并以輸出的結(jié)果代替后引號中的內(nèi)容。而在“csh”及其衍生的shell中使用感嘆號(!)時需要在其前面加上轉(zhuǎn)義用的反斜杠(就像這樣:\!)以保證上面所使用的例子能正常運(yùn)行(包括使用單引號的情況下)。DOS版本的Sed則一律使用雙引號("...")而不是引號來圈起命令。 '\t'的用法:為了使本文保持行文簡潔,我們在腳本中使用'\t'來表示一個制表符。但是現(xiàn)在大部分版本的sed還不能識別'\t'的簡寫方式,因此當(dāng)在命令行中為腳本輸入制表符時,你應(yīng)該直接按TAB鍵來輸入制表符而不是輸入'\t'。下列的工具軟件都支持'\t'做為一個正則表達(dá)式的字元來表示制表符:awk、perl、HHsed、sedmod以及GNU sed v3.02.80。 不同版本的SED:不同的版本間的sed會有些不同之處,可以想象它們之間在語法上會有差異。具體而言,它們中大部分不支持在編輯命令中間使用標(biāo)簽(:name)或分支命令(b,t),除非是放在那些的末尾。這篇文檔中我們盡量選用了可移植性較高的語法,以使大多數(shù)版本的sed的用戶都能使用這些腳本。不過GNU版本的sed允許使 用更簡潔的語法。想像一下當(dāng)讀者看到一個很長的命令時的心情:sed -e '/AAA/b' -e '/BBB/b' -e '/CCC/b' -e d 好消息是GNU sed能讓命令更緊湊:sed '/AAA/b;/BBB/b;/CCC/b;d' # 甚至可以寫成sed '/AAA|BBB|CCC/b;d' 此外,請注意雖然許多版本的sed接受象“/one/ s/RE1/RE2/”這種在's'前帶有空格的命令,但這些版本中有些卻不接受這樣的命令:“/one/! s/RE1/RE2/”。這時只需要把中間的空格去掉就行了。 速度優(yōu)化:當(dāng)由于某種原因(比如輸入文件較大、處理器或硬盤較慢等)需要提高 命令執(zhí)行速度時,可以考慮在替換命令(“s/.../.../”)前面加上地址表達(dá)式來 提高速度。舉例來說:sed 's/foo/bar/g' filename # 標(biāo)準(zhǔn)替換命令sed '/foo/ s/foo/bar/g' filename # 速度更快sed '/foo/ s//bar/g' filename # 簡寫形式 當(dāng)只需要顯示文件的前面的部分或需要刪除后面的內(nèi)容時,可以在腳本中使用“q” 命令(退出命令)。在處理大的文件時,這會節(jié)省大量時間。因此:sed -n '45,50p' filename # 顯示第45到50行sed -n '51q;45,50p' filename # 一樣,但快得多 如果你有其他的單行腳本想與大家分享或者你發(fā)現(xiàn)了本文檔中錯誤的地方,請發(fā)電 子郵件給本文檔的作者(Eric Pement)。郵件中請記得提供你所使用的sed版本、 該sed所運(yùn)行的操作系統(tǒng)及對問題的適當(dāng)描述。本文所指的單行腳本指命令行的長 度在65個字符或65個以下的sed腳本〔譯注1〕。本文檔的各種腳本是由以下所列作 者所寫或提供:Al Aab # 建立了“seders”郵件列表Edgar Allen # 許多方面Yiorgos Adamopoulos # 許多方面Dale Dougherty # 《sed & awk》作者Carlos Duarte # 《do it with sed》作者Eric Pement # 本文檔的作者Ken Pizzini # GNU sed v3.02 的作者S.G. Ravenhall # 去html標(biāo)簽?zāi)_本Greg Ubben # 有諸多貢獻(xiàn)并提供了許多幫助 ------------------------------------------------------------------------- 譯注1:大部分情況下,sed腳本無論多長都能寫成單行的形式(通過`-e'選項和`;' 號)——只要命令解釋器支持,所以這里說的單行腳本除了能寫成一行還對長度有 所限制。因為這些單行腳本的意義不在于它們是以單行的形式出現(xiàn)。而是讓用戶能 方便地在命令行中使用這些緊湊的腳本才是其意義所在。

BSD版本Sed的文檔

BSDGeneralCommandsManual

NAME
sed – stream editor

SYNOPSIS【簡略】
sed [-Ealn] command [file …]
【即為】sed [option] command [file…]
sed [-Ealn] [-e command] [-f command_file] [-i extension] [file …]


DESCRIPTION【描述】
The sed utility reads the specified files【指定的文件】, or the standard input if no
files are specified, modifying the input as specified by a list of com-
mands. The input is then written to the standard output.


A single command may be specified as the first argument to sed. Multiple
commands may be specified by using the -e or -f options. All commands
are applied to the input in the order they are specified regardless of
their origin.


【可選的option】The following options are available:

-E【使用擴(kuò)展正則表達(dá)式】 Interpret regular expressions as extended (modern) regularexpressions rather than basic regular expressions (BRE's). There_format(7) manual page fully describes both formats.-a The files listed as parameters for the ``w'' functions are cre-ated (or truncated) before any processing begins, by default.The -a option causes sed to delay opening each file until a com-mand containing the related ``w'' function is applied to a lineof input.-e command【使用多個command】Append the editing commands specified by the command argument tothe list of commands.-f command_file【指令文件】Append the editing commands found in the file command_file to thelist of commands. The editing commands should each be listed ona separate line.-i extensionEdit files in-place, saving backups with the specified extension.If a zero-length extension is given, no backup will be saved. Itis not recommended to give a zero-length extension when in-placeediting files, as you risk corruption or partial content in situ-ations where disk space is exhausted, etc.-l Make output line buffered.-n By default, each line of input is echoed to the standard outputafter all of the commands have been applied to it. The -n optionsuppresses(壓制,阻礙) this behavior.

【command中所可選的限定符】
The form of a sed command is as follows:
[address[,address]]function[arguments]

Whitespace may be inserted before the first address and the function por-tions of the command.Normally, sed cyclically copies a line of input, not including its termi-nating newline character, into a pattern space, (unless there is some-thing left after a ), applies all of the commands withaddresses that select that pattern space, copies the pattern space to thestandard output, appending a newline, and deletes the pattern space.Some of the functions use a hold space to save all or part of the patternspace for subsequent retrieval.

Sed Addresses
An address is not required, but if specified must be a number (that
counts input lines cumulatively across input files), a dollar (“$”)
character that addresses the last line of input, or a context address
(which consists of a regular expression preceded and followed by a delimiter).

A command line with no addresses selects every pattern space.A command line with one address selects all of the pattern spaces thatmatch the address.A command line with two addresses selects an inclusive range. This range starts with the first pattern space that matches the first address.The end of the range is the next following pattern space that matches the second address. If the second address is a number less than or equal to the line number first selected, only that line is selected. In the case when the second address is a context address, sed does not re-match the second address against the pattern space that matched the first address.Starting at the first line following the selected range, sed starts looking again for the first address.Editing commands can be applied to non-selected pattern spaces by use ofthe exclamation character (``!'') function.

【sed中的正則表達(dá)式】
Sed Regular Expressions
The regular expressions used in sed, by default, are basic regular
expressions (BREs, see re_format(7) for more information), but extended
(modern) regular expressions can be used instead if the -E flag is given.
In addition, sed has the following two additions to regular expressions:

1. In a context address, any character other than a backslash (``\'') or newline character may be used to delimit the regular expression.Also, putting a backslash character before the delimiting character causes the character to be treated literally. For example, in the context address \xabc\xdefx, the RE delimiter is an ``x'' and the second ``x'' stands for itself, so that the regular expression is ``abcxdef''.2. The escape sequence \n matches a newline character embedded in the pattern space. You cannot, however, use a literal newline character in an address or in the substitute command.One special feature of sed regular expressions is that they can default to the last regular expression used. If a regular expression is empty, i.e., just the delimiter characters are specified, the last regular expression encountered is used instead. The last regular expression is defined as the last regular expression used as part of an address or sub- stitute command, and at run-time, not compile-time. For example, the command ``/abc/s//XXX/'' will substitute ``XXX'' for the pattern ``abc''.

Sed Functions
In the following list of commands, the maximum number of permissible【允許的】 addresses for each command is indicated【指示】 by [0addr], [1addr], or [2addr],representing zero, one, or two addresses.

The argument text consists of one or more lines. To embed【嵌入】 a newline in the text, 【在之前】precede it with a backslash. Other backslashes in text are deleted and the following character taken literally.The ``r'' and ``w'' functions【讀取、寫入函數(shù)】 take an optional file parameter, which should be separated from the function letter by white space. Each file given as an argument to sed is created (or its contents truncated) before any input processing begins.The ``b'', ``r'', ``s'', ``t'', ``w'', ``y'', ``!'', and ``:'' functions all accept additional arguments. The following synopses【概要】 indicate which arguments have to be separated from the function letters by white space characters.Two of the functions take a function-list. This is a list of sed functions separated by newlines, as follows:{ functionfunction...function}The ``{'' can be preceded by white space and can be followed by whitespace. The function can be preceded by white space. The terminating ``}'' must be preceded by a newline or optional white space.[2addr] function-listExecute function-list only when the pattern space is selected.[1addr]a\text Write text to standard output immediately before each attempt to read a line of input, whether by executing the ``N'' function or by beginning a new cycle.[2addr]b[label]Branch to the ``:'' function with the specified label. If the label is not specified, branch to the end of the script.[2addr]c\text Delete the pattern space. With 0 or 1 address or at the end of a 2-address range, text is written to the standard output.[2addr]dDelete the pattern space and start the next cycle.[2addr]DDelete the initial segment of the pattern space through the first newline character and start the next cycle.[2addr]gReplace the contents of the pattern space with the contents of the hold space.[2addr]GAppend a newline character followed by the contents of the hold space to the pattern space.[2addr]hReplace the contents of the hold space with the contents of the pattern space.[2addr]HAppend a newline character followed by the contents of the pattern space to the hold space.[1addr]i\text Write text to the standard output.[2addr]l(The letter ell.) Write the pattern space to the standard output in a visually unambiguous form. This form is as follows:backslash \\alert \aform-feed \fcarriage-return \rtab \tvertical tab \vNonprintable characters are written as three-digit octal numbers(with a preceding backslash) for each byte in the character (most significant byte first). Long lines are folded, with the point of folding indicated by displaying a backslash followed by a newline. The end of each line is marked with a ``$''.[2addr]nWrite the pattern space to the standard output if the default output has not been suppressed, and replace the pattern space with the next line of input.[2addr]NAppend the next line of input to the pattern space, using an embedded newline character to separate the appended material from the original contents. Note that the current line number changes.[2addr]pWrite the pattern space to standard output.[2addr]PWrite the pattern space, up to the first newline character to the standard output.[1addr]qBranch to the end of the script and quit without starting a new cycle.[1addr]r fileCopy the contents of file to the standard output immediately before the next attempt to read a line of input. If file cannot be read for any reason, it is silently ignored and no error condition is set.[2addr]s/regular expression/replacement/flagsSubstitute the replacement string for the first instance of the regular expression in the pattern space. Any character other than backslash or newline can be used instead of a slash to delimit the RE and the replacement. Within the RE and the replacement, the RE delimiter itself can be used as a literal character if it is preceded by a backslash.An ampersand (``&'') appearing in the replacement is replaced by the string matching the RE. The special meaning of ``&'' in this context can be suppressed by preceding it by a backslash. The string ``\#'', where ``#'' is a digit, is replaced by the text matched by the corresponding back reference expression (see re_format(7)).A line can be split by substituting a newline character into it.To specify a newline character in the replacement string, precede it with a backslash.The value of flags in the substitute function is zero or more of the following:N Make the substitution only for the N'th occurrence of the regular expression in the pattern space.g Make the substitution for all non-overlapping matches of the regular expression, not just the first one.p Write the pattern space to standard output if a replacement was made. If the replacement string is identical to that which it replaces, it is still considered to have been a replacement.w file Append the pattern space to file if a replacement was made. If the replacement string is identical to that which it replaces, it is still considered to have been a replacement.[2addr]t [label]Branch to the ``:'' function bearing the label if any substitutions have been made since the most recent reading of an input line or execution of a ``t'' function. If no label is specified, branch to the end of the script.[2addr]w fileAppend the pattern space to the file.[2addr]xSwap the contents of the pattern and hold spaces.[2addr]y/string1/string2/Replace all occurrences of characters in string1 in the pattern space with the corresponding characters from string2. Any character other than a backslash or newline can be used instead of a slash to delimit the strings. Within string1 and string2, a backslash followed by an ``n'' is replaced by a newline character. A pair of backslashes is replaced by a literal backslash.Finally, a backslash followed by any other character (except a newline) is that literal character.[2addr]!function[2addr]!function-listApply the function or function-list only to the lines that are not selected by the address(es).[0addr]:labelThis function does nothing; it bears a label to which the ``b'' and ``t'' commands may branch.[1addr]=Write the line number to the standard output followed by a newline character.[0addr]Empty lines are ignored.[0addr]#The ``#'' and the remainder of the line are ignored (treated as a comment), with the single exception that if the first two characters in the file are ``#n'', the default output is suppressed.This is the same as specifying the -n option on the command line.

ENVIRONMENT
The COLUMNS, LANG, LC_ALL, LC_CTYPE and LC_COLLATE environment variables
affect the execution of sed as described in environ(7).


EXIT STATUS
The sed utility exits 0 on success, and >0 if an error occurs.


LEGACY DESCRIPTION
Warnings are not generated for unused labels. In legacy mode, they are.
In the -y function, doubled backslashes are not converted to single ones.
In legacy mode, they are.
For more information about legacy mode, see compat(5).


SEE ALSO
awk(1), ed(1), grep(1), regex(3), compat(5), re_format(7)


STANDARDS
The sed utility is expected to be a superset of the IEEE Std 1003.2
(“POSIX.2”) specification.
The -E, -a and -i options are non-standard FreeBSD extensions and may not
be available on other operating systems.


HISTORY
A sed command, written by L. E. McMahon, appeared in Version 7 AT&T UNIX.


AUTHORS
Diomidis D. Spinellis dds@FreeBSD.org


BUGS
Multibyte characters containing a byte with value 0x5C (ASCII `\’) may be
incorrectly treated as line continuation characters in arguments to the
a'',c” and i'' commands. Multibyte characters cannot be used as
delimiters with thes” and “y” commands.

BSD May 10, 2005 BSD


版權(quán)聲明:本文為博主原創(chuàng)文章,轉(zhuǎn)載請標(biāo)明出處。

轉(zhuǎn)載于:https://www.cnblogs.com/fridge/p/4861888.html

總結(jié)

以上是生活随笔為你收集整理的Linux学习笔记Sed最全整理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。