在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ Linux/ sed模式緩沖區(qū)
Sed字符串
Sed實(shí)用功能
Sed管理模式
sed環(huán)境設(shè)置
sed模式緩沖區(qū)
Sed循環(huán)
Sed模式范圍
Sed教程
Sed正則表達(dá)式
Sed分支
Sed基本語(yǔ)法
sed工作流程
Sed特殊字符
Sed基本命令

sed模式緩沖區(qū)

我們對(duì)任何文件進(jìn)行基本操作,顯示其內(nèi)容。為了達(dá)到這個(gè)目的,我們可以用打印的模式緩沖區(qū)的打印命令。本教程將介紹更多的模式緩沖區(qū),以及如何打印使用相關(guān)模式緩沖區(qū)不同運(yùn)算符的文件的內(nèi)容。

考慮一下我們有一個(gè)文本文件books.txt待處理,它有以下內(nèi)容:

1) A Storm of Swords, George R. R. Martin, 1216 
2) The Two Towers, J. R. R. Tolkien, 352 
3) The Alchemist, Paulo Coelho, 197 
4) The Fellowship of the Ring, J. R. R. Tolkien, 432 
5) The Pilgrimage, Paulo Coelho, 288 
6) A Game of Thrones, George R. R. Martin, 864

Sed p 命令

我們可以用Sed 'p'命令來打印指定文件的內(nèi)容。下面是一個(gè)簡(jiǎn)單的命令來打印文件 books.txt 的內(nèi)容。

[jerry]$ sed 'p' books.txt

當(dāng)執(zhí)行上面的代碼,就會(huì)產(chǎn)生下面的結(jié)果。

1) A Storm of Swords, George R. R. Martin, 1216 
1) A Storm of Swords, George R. R. Martin, 1216 
2) The Two Towers, J. R. R. Tolkien, 352 
2) The Two Towers, J. R. R. Tolkien, 352 
3) The Alchemist, Paulo Coelho, 197 
3) The Alchemist, Paulo Coelho, 197 
4) The Fellowship of the Ring, J. R. R. Tolkien, 432 
4) The Fellowship of the Ring, J. R. R. Tolkien, 432 
5) The Pilgrimage, Paulo Coelho, 288 
5) The Pilgrimage, Paulo Coelho, 288 
6) A Game of Thrones, George R. R. Martin, 864 
6) A Game of Thrones, George R. R. Martin, 864

讓我們找出為什么每個(gè)行被打印兩次。實(shí)際上,在默認(rèn)情況下,sed打印模式緩沖區(qū)的內(nèi)容。此外,我們已明確地接入命令部分 print  命令。因此,每行打印了兩次。Sed有一個(gè)-n選項(xiàng)來抑制模式緩沖區(qū)的默認(rèn)打印。讓我們?cè)囋囅旅娴拿睿?/p>

[jerry]$ sed -n 'p' books.txt 

當(dāng)執(zhí)行上面的代碼,就會(huì)產(chǎn)生下面的結(jié)果。

1) A Storm of Swords, George R. R. Martin, 1216 
2) The Two Towers, J. R. R. Tolkien, 352 
3) The Alchemist, Paulo Coelho, 197 
4) The Fellowship of the Ring, J. R. R. Tolkien, 432 
5) The Pilgrimage, Paulo Coelho, 288 
6) A Game of Thrones, George R. R. Martin, 864 

Sed 地址范圍

默認(rèn)情況下,sed在所有行上操作。但是,可以強(qiáng)制sed只在某些行上使用。例如,在下面的例子中,sed只運(yùn)行在第三行。在這個(gè)例子中,我們指定 sed 命令前一個(gè)地址范圍。

[jerry]$ sed -n '3p' books.txt 

當(dāng)執(zhí)行上面的代碼,就會(huì)產(chǎn)生下面的結(jié)果。

3) The Alchemist, Paulo Coelho, 197 

Sed comma 命令

下面的代碼打印2?5。這里我們使用了逗號(hào)(,)運(yùn)算符指定的地址范圍內(nèi)的所有行:

[jerry]$ sed -n '2,5 p' books.txt 

當(dāng)執(zhí)行上面的代碼,就會(huì)產(chǎn)生下面的結(jié)果。

2) The Two Towers, J. R. R. Tolkien, 352 
3) The Alchemist, Paulo Coelho, 197 
4) The Fellowship of the Ring, J. R. R. Tolkien, 432 
5) The Pilgrimage, Paulo Coelho, 288

Sed $ 運(yùn)算符

我們可以使用美元符號(hào)$運(yùn)算符來打印該文件的最后一行,如下所示:

[jerry]$ sed -n '$ p' books.txt 

當(dāng)執(zhí)行上面的代碼,就會(huì)產(chǎn)生下面的結(jié)果。

6) A Game of Thrones, George R. R. Martin, 864 

但是,我們也可以使用美元符號(hào)($)來指定一個(gè)地址范圍。下面的例子打印通過第3行到最后一行。

[jerry]$ sed -n '3,$ p' books.txt 

當(dāng)執(zhí)行上面的代碼,就會(huì)產(chǎn)生下面的結(jié)果。

3) The Alchemist, Paulo Coelho, 197
4) The Fellowship of the Ring, J. R. R. Tolkien, 432
5) The Pilgrimage, Paulo Coelho, 288
6) A Game of Thrones, George R. R. Martin, 864

Sed 加操作

sed的加號(hào)(+)運(yùn)算符可以用來與逗號(hào)(,)運(yùn)算符。例如M,+ n將打印的行數(shù)M,以下示例開始打印從第2行開始到下一個(gè)4行:

[jerry]$ sed -n '2,+4 p' books.txt 

當(dāng)執(zhí)行上面的代碼,就會(huì)產(chǎn)生下面的結(jié)果。

2) The Two Towers, J. R. R. Tolkien, 352 
3) The Alchemist, Paulo Coelho, 197 
4) The Fellowship of the Ring, J. R. R. Tolkien, 432 
5) The Pilgrimage, Paulo Coelho, 288 
6) A Game of Thrones, George R. R. Martin, 864 

Sed 波浪線運(yùn)算符

或者,我們也可以使用波浪符號(hào)(?)運(yùn)算符指定的地址范圍。它采用M?n形式。這表明 sed 應(yīng)該開始行數(shù)M和處理每n(次)行。例如,50?5行號(hào)50,55,60,65,等等。讓我們從打印的文件只有奇數(shù)行。

[jerry]$ sed -n '1~2 p' books.txt 

當(dāng)執(zhí)行上面的代碼,就會(huì)產(chǎn)生下面的結(jié)果。

1) A Storm of Swords, George R. R. Martin, 1216 
3) The Alchemist, Paulo Coelho, 197 
5) The Pilgrimage, Paulo Coelho, 288

下面的代碼僅打印偶數(shù)行的文件。

[jerry]$ sed -n '2~2 p' books.txt 

當(dāng)執(zhí)行上面的代碼,就會(huì)產(chǎn)生下面的結(jié)果。

2) The Two Towers, J. R. R. Tolkien, 352 
4) The Fellowship of the Ring, J. R. R. Tolkien, 432 
6) A Game of Thrones, George R. R. Martin, 864 

上一篇:Sed正則表達(dá)式下一篇:Sed教程