亚洲区国产区激情区无码区,国产成人mv视频在线观看,国产A毛片AAAAAA,亚洲精品国产首次亮相在线

sed 模式緩沖區(qū)

我們對(duì)任何文件執(zhí)行的基本操作之一是顯示其內(nèi)容。為此,我們可以使用 print 命令來(lái)打印?Pattern Buffer模式緩沖區(qū)?的內(nèi)容。

首先創(chuàng)建一個(gè)文件,其中包含行號(hào),書名,作者和頁(yè)數(shù)。在本教程中,我們將使用此文件,我們的文本文件將如下所示:

$vi books.txt 
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

現(xiàn)在,讓我們打印文件內(nèi)容。

$sed 'p' books.txt

執(zhí)行以上代碼后,將產(chǎn)生以下輸出。

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

您可能想知道為什么每一行都顯示兩次。讓我們找出答案。

您還記得SED的工作流程嗎?默認(rèn)情況下,SED打印Pattern Buffer模式緩沖區(qū)的內(nèi)容。此外,我們?cè)诿畈糠种忻鞔_包含了一條打印命令。因此,每行打印兩次。但是不用擔(dān)心。 SED具有 -n 選項(xiàng),以禁止緩沖區(qū)的默認(rèn)打印。以下命令說(shuō)明了這一點(diǎn)。

$sed -n 'p' books.txt 

執(zhí)行以上代碼后,將產(chǎn)生以下輸出。

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 

恭喜你!我們得到了預(yù)期的輸出。默認(rèn)情況下,SED在所有行上運(yùn)行。如想在上面的示例中,SED僅在第三行上運(yùn)行,我們?cè)赟ED命令之前指定了Address Range(地址范圍) 。

$sed -n '3p' books.txt 

執(zhí)行以上代碼后,將產(chǎn)生以下輸出。

3) The Alchemist, Paulo Coelho, 197 

此外,我們還可以指示SED僅打印某些行。如下面的代碼打印2到5的所有行。在這里,我們使用 逗號(hào)(,) 運(yùn)算符指定地址范圍。

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

執(zhí)行以上代碼后,將產(chǎn)生以下輸出。

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

還有一個(gè)特殊字符Dollar($)代表文件的最后一行。因此,讓我們打印文件的最后一行。

$sed -n '$p' books.txt 

執(zhí)行以上代碼后,將產(chǎn)生以下輸出。

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

但是,我們也可以使用Dollar($) 字符指定地址范圍。下面的示例通過(guò)第3行打印到最后一行。

$sed -n '3,$p' books.txt 

執(zhí)行以上代碼后,將產(chǎn)生以下輸出。

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 

我們學(xué)習(xí)了如何使用?逗號(hào)(,) 運(yùn)算符指定地址范圍。 SED支持兩個(gè)以上的運(yùn)算符,可用于指定地址范圍。首先是plus(+)運(yùn)算符,它可以與逗號(hào)(,)運(yùn)算符一起使用。如 M,+ n 將從行號(hào) M 開始打印下一個(gè) n 行。聽起來(lái)令人困惑?讓我們用一個(gè)簡(jiǎn)單的示例。以下示例從第2行開始打印接下來(lái)的4行。

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

執(zhí)行以上代碼后,將產(chǎn)生以下輸出。

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 

我們還可以使用 波浪號(hào)(?) 運(yùn)算符指定地址范圍,它使用 M?n 形式。它指示SED應(yīng)該從行號(hào)M開始并每隔n(th)行處理一次。如, 50?5 匹配行號(hào)50、55、60、65,依此類推。讓我們僅從文件中打印奇數(shù)行。

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

執(zhí)行以上代碼后,將產(chǎn)生以下輸出。

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

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

$sed -n '2~2 p' books.txt 

執(zhí)行以上代碼后,將產(chǎn)生以下輸出。

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