linux sed高级用法,sed 高级用法
首先,應(yīng)該明白模式空間的定義。模式空間就是讀入行所在的緩存,sed對(duì)文本行進(jìn)行的處理都是在這個(gè)緩存中進(jìn)行的。這對(duì)接下來的學(xué)習(xí)是有幫助的。
在正常情況下,sed將待處理的行讀入模式空間,腳本中的命令就一條接著一條的對(duì)該行進(jìn)行處理,直到腳本執(zhí)行完畢,然后該行被輸出,模式空間請空;然后重復(fù)剛才的動(dòng)作,文件中的新的一行被讀入,直到文件處理完備。
但是,各種各樣的原因,比如用戶希望在某個(gè)條件下腳本中的某個(gè)命令被執(zhí)行,或者希望模式空間得到保留以便下一次的處理,都有可能使得sed在處理文件的時(shí)候不按照正常的流程來進(jìn)行。這個(gè)時(shí)候,sed設(shè)置了一些高級(jí)命令來滿足用戶的要求。
總的來說,這些命令可以劃分為以下三類:
1. N、D、P:處理多行模式空間的問題;
2. H、h、G、g、x:將模式空間的內(nèi)容放入存儲(chǔ)空間以便接下來的編輯;
3. :、b、t:在腳本中實(shí)現(xiàn)分支與條件結(jié)構(gòu)。
多行模式空間的處理:
由于正則表達(dá)式是面向行的,因此,如若某個(gè)詞組一不分位于某行的結(jié)尾,另外一部分又在下一行的開始,這個(gè)時(shí)候用grep等命令來處理就相當(dāng)?shù)睦щy。然而,借助于sed的多行命令N、D、P,卻可以輕易地完成這個(gè)任務(wù)。
多行Next(N)命令是相對(duì)于next(n)命令的,后者將模式空間中的內(nèi)容輸出,然后把下一行讀入模式空間,但是腳本并不會(huì)轉(zhuǎn)移到開始而是從當(dāng)前的n 命令之后開始執(zhí)行;而前者則保存原來模式空間中的內(nèi)容,再把新的一行讀入,兩者之間依靠一個(gè)換行符"\n"來分隔。在N命令執(zhí)行后,控制流將繼續(xù)用N命令以后的命令對(duì)模式空間進(jìn)行處理。
值得注意的是,在多行模式中,特殊字符"^"和"$"匹配的是模式空間的最開始與最末尾,而不是內(nèi)嵌"\n"的開始與末尾。
例1:
$ cat expl.1
Consult Section 3.1 in the Owner and Operator
Guide for a description of the tape drives
available on your system.
現(xiàn)在要將"Owner and Operator Guide"替換為"Installation Guide":
$ sed '/Operator$/{
> N
> s/Owner and Operator\nGuide/Installation Guide\
> /
> }' expl.1
在上面的例子中要注意的是,行與行之間存在內(nèi)嵌的換行符;另外在用于替代的內(nèi)容中要插入換行符的話,要用如上的"\"的轉(zhuǎn)義。
再看一個(gè)例子:
例2:
$ cat expl.2
Consult Section 3.1 in the Owner and Operator
Guide for a description of the tape drives
available on your system.
Look in the Owner and Operator Guide shipped with your system.
Two manuals are provided including the Owner and
Operator Guide and the User Guide.
The Owner and Operator Guide is shipped with your system.
$ sed 's/Owner and Operator Guide/Installation Guide/
> /Owner/{
> N
> s/ *\n/ /
> s/Owner and Operator Guide */Installation Guide\
> /
}' expl.2
結(jié)果得到:
Consult Section 3.1 in the Installation Guide
for a description of the tape drives
available on your system.
Look in the Installation Guide shipped with your system.
Two manuals are provided including the Installation Guide
and the User Guide.
The Installation Guide is shipped with your system.
看上去sed命令中作了兩次替換是多余的。實(shí)際上,如果去掉第一次替換,再運(yùn)行腳本,就會(huì)發(fā)現(xiàn)輸出存在兩個(gè)問題。一個(gè)是結(jié)果中最后一行不會(huì)被替換(在某些版本的sed中甚至不會(huì)被輸出)。這是因?yàn)樽詈笠恍衅ヅ淞?#34;Owner",執(zhí)行N命令,但是已經(jīng)到了文件末尾,某些版本就會(huì)直接打印這行再退出,而另外一些版本則是不作出打印立即退出。對(duì)于這個(gè)問題可以通過命令"$!N"來解決。這表示N命令對(duì)最后一行不起作用。另外一個(gè)問題是"look manuals"一段被拆為兩行,而且與下一段的空行被刪除了。這是因?yàn)閮?nèi)嵌的換行符被替換的結(jié)果。因此,sed中做兩次替換一點(diǎn)也不是多余的。
例3:
$ cat expl.3
This is a test paragraph in Interleaf style ASCII. Another line
in a paragraph. Yet another.
v.1111111111111111111111100000000000000000001111111111111000000
100001000100100010001000001000000000000000000000000000000000000
000000
More lines of text to be found after the figure.
These lines should print.
我們的sed命令是這樣的:
$ sed '/{
> N
> c\
> .LP
> }
> /
> w fig.interleaf
> /
> .FG\
> \
> .FE
> d
> }
> /^$/d' expl.3
運(yùn)行后得到的結(jié)果是:
.LP
This is a test paragraph in Interleaf style ASCII. Another line
in a paragraph. Yet another.
.FG
.FE
.LP
More lines of text to e found after the figure.
These lines should print.
而
命令"d"作用是刪除模式空間的內(nèi)容,然后讀入新的行,sed腳本從頭再次開始執(zhí)行。而命令"D"的不同之處在于它刪除的是直到第一個(gè)內(nèi)嵌換行符為止的模式空間的一部分,但是不會(huì)讀入新的行,腳本將回到開始對(duì)剩下內(nèi)容進(jìn)行處理。
例4:
$ cat expl.4
This line is followed by 1 blank line.
This line is followed by 2 blank line.
This line is followed by 3 blank line.
This line is followed by 4 blank line.
This is the end.
總結(jié)
以上是生活随笔為你收集整理的linux sed高级用法,sed 高级用法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 女生微信个性签名可爱
- 下一篇: c语言相邻字符串字面量,C语言预处理#运