Sed教程(二):基本语法、循环、分支
sed使用簡(jiǎn)單,我們可以提供sed命令直接在命令行或具有sed命令的文本文件的形式。本教程講解調(diào)用sed的例子,有這兩種方法:
Sed 命令行
以下是我們可以指定單引號(hào)在命令行sed命令的格式如下:
sed [-n] [-e] 'command(s)' files例子
考慮一下我們有一個(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文件的完整顯示內(nèi)容如下:
[jerry]$ sed '' books.txt執(zhí)行上面的代碼,會(huì)得到如下結(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現(xiàn)在,我們從上述文件中顯示將看到sed的delete命令刪除某些行。讓我們刪除了第一,第二和第五行。在這里,要?jiǎng)h除給定的三行,我們已經(jīng)指定了三個(gè)單獨(dú)的命令帶有-e選項(xiàng)。
[jerry]$ sed -e '1d' -e '2d' -e '5d' books.txt執(zhí)行上面的代碼,會(huì)得到如下結(jié)果:
3) The Alchemist, Paulo Coelho, 197 4) The Fellowship of the Ring, J. R. R. Tolkien, 432 6) A Game of Thrones, George R. R. Martin, 864sed腳本文件
下面是第二種形式,我們可以提供一個(gè)sed腳本文件sed命令:
sed [-n] -f scriptfile files首先,創(chuàng)建一個(gè)包含在一個(gè)單獨(dú)的行的文本commands.txt文件,每次一行為每個(gè)sed命令,如下圖所示:
1d 2d 5d現(xiàn)在,我們可以指示sed從文本文件中讀取指令和執(zhí)行操作。這里,我們實(shí)現(xiàn)相同的結(jié)果,如圖在上述的例子。
[jerry]$ sed -f commands.txt books.txt執(zhí)行上面的代碼,會(huì)得到如下結(jié)果:
3) The Alchemist, Paulo Coelho, 197 4) The Fellowship of the Ring, J. R. R. Tolkien, 432 6) A Game of Thrones,George R. R. Martin, 864sed標(biāo)準(zhǔn)選項(xiàng)
sed支持可從命令行提供下列標(biāo)準(zhǔn)選擇。
?-n 選項(xiàng)
這是模式緩沖區(qū)的缺省打印選項(xiàng)。 GNU sed解釋器提供--quiet,--silent選項(xiàng)作為 -n選項(xiàng)的替代。
例如,下面 sed 命令不顯示任何輸出:
[jerry]$ sed -n '' quote.txt-e 選項(xiàng)
-e選項(xiàng)的編輯選項(xiàng)。通過使用此選項(xiàng),可以指定多個(gè)命令。例如,下面 sed 命令打印每行兩次:
[jerry]$ sed -e '' -e 'p' quote.txt執(zhí)行上面的代碼,會(huì)得到如下結(jié)果:
There is only one thing that makes a dream impossible to achieve: the fear of failure. There is only one thing that makes a dream impossible to achieve: the fear of failure. - Paulo Coelho, The Alchemist - Paulo Coelho, The Alchemist-f 選項(xiàng)
-f選項(xiàng)是用來提供包含sed命令的文件。例如,我們可以按如下方法通過文件指定一個(gè)打印命令:
[jerry]$ echo "p" > commands.txt [jerry]$ sed -n -f commands quote.txt執(zhí)行上面的代碼,會(huì)得到如下結(jié)果:
There is only one thing that makes a dream impossible to achieve: the fear of failure. - Paulo Coelho, The Alchemist
像其他的編程語言,sed還提供了一個(gè)循環(huán)和分支工具來控制程序的執(zhí)行流程。本教程將探討如何使用sed的循環(huán)和分支。
sed循環(huán)的工作原理類似于現(xiàn)代編程語言中的goto語句。 sed可以跳轉(zhuǎn)到標(biāo)記標(biāo)簽的行并繼續(xù)執(zhí)行下面提供該標(biāo)簽的剩余命令。
以下是對(duì)在sed定義一個(gè)標(biāo)簽的語法。在這里,冒號(hào)后的名稱(:)暗示的標(biāo)簽名稱。
:label :start :end :up要跳轉(zhuǎn)到一個(gè)特定的標(biāo)簽,我們可以使用 b 命令后面跟標(biāo)簽名稱。如果標(biāo)簽的名稱省略,則 sed 跳轉(zhuǎn)到 sed 文件的末尾。
考慮一下我們有一個(gè)待處理文本文件books.txt ,它有以下內(nèi)容:
A Storm of Swords George R. R. Martin The Two Towers J. R. R. Tolkien The Alchemist Paulo Coelho The Fellowship of the Ring J. R. R. Tolkien The Pilgrimage Paulo Coelho A Game of Thrones George R. R. Martin下面的例子是連接書名,并在一行用逗號(hào)分隔作者姓名。然后,它會(huì)搜索模式“Paulo”。如果能夠匹配,它打印一個(gè)連字符(- )在該行的前面,否則跳轉(zhuǎn)到打印行打印標(biāo)簽。
[jerry]$ sed -n ' h;n;H;x s/\n/, / /Paulo/!b Print s/^/- / :Print p' books.txt執(zhí)行上面的代碼,會(huì)得到如下結(jié)果:
A Storm of Swords, George R. R. Martin The Two Towers, J. R. R. Tolkien - The Alchemist, Paulo Coelho The Fellowship of the Ring, J. R. R. Tolkien - The Pilgrimage, Paulo Coelho A Game of Thrones, George R. R. Martin初看起來,上面的腳本可能看起來神秘。讓我們看看這是什么情況。
-
最初sed讀入模式緩沖區(qū)第一行即書名和保持緩沖區(qū)保持為空。后執(zhí)行-h命令模式緩沖區(qū)被復(fù)制到保留緩沖區(qū)。現(xiàn)在,這兩個(gè)緩沖區(qū)包含了本書即標(biāo)題.?A Storm of Swords.?接下來n命令打印當(dāng)前的模式緩沖區(qū)(在本例中沒有打印出來,因?yàn)?n選項(xiàng)),清除當(dāng)前圖形緩沖區(qū)讀取輸入的下一行。現(xiàn)在模式緩沖區(qū)包含George R. R. Martin。
-
第三個(gè)命令跳到僅當(dāng)模式不匹配,否則取代是由第四指令執(zhí)行的標(biāo)簽Print。
-
:Print?僅僅是一個(gè)標(biāo)簽名,p是打印命令。
為了提高可讀性,每個(gè)sed命令被放置在一個(gè)單獨(dú)的行。然而,人們可以選擇將所有命令在一行中,如下所示:
[jerry]$ sed -n 'h;n;H;x;s/\n/, /;/Paulo/!b Print; s/^/- /; :Print;p' books.txt執(zhí)行上面的代碼,會(huì)得到如下結(jié)果:
A Storm of Swords, George R. R. Martin The Two Towers, J. R. R. Tolkien - The Alchemist, Paulo Coelho The Fellowship of the Ring, J. R. R. Tolkien - The Pilgrimage, Paulo Coelho A Game of Thrones, George R. R. Martin
可以用t命令創(chuàng)建分支。 t 命令跳轉(zhuǎn)到標(biāo)簽,只有在以前的替換命令是成功的。讓我們以前面的章節(jié)同樣的例子,但不是打印一個(gè)連字符(- ),現(xiàn)在我們印刷四連字符。下面的例子演示了 t 命令的用法。
[jerry]$ sed -n ' h;n;H;x s/\n/, / :Loop /Paulo/s/^/-/ /----/!t Loop p' books.txt當(dāng)執(zhí)行上面的代碼,就會(huì)產(chǎn)生下面的結(jié)果。
A Storm of Swords, George R. R. Martin The Two Towers, J. R. R. Tolkien ----The Alchemist, Paulo Coelho The Fellowship of the Ring, J. R. R. Tolkien ----The Pilgrimage, Paulo Coelho A Game of Thrones, George R. R. Martin我們已經(jīng)討論了在前面的章節(jié)中的第一個(gè)命令。第三個(gè)命令定義一個(gè)標(biāo)簽循環(huán)。第四命令上前置的連字符( - ),如果該行包含字符串“Paulo”和t命令重復(fù)這一過程,直到有四個(gè)連字符位于行的開頭。
為了提高可讀性,每個(gè) sed 命令寫在一個(gè)單獨(dú)的行。否則,我們可以寫一行一個(gè) sed 如下:
[jerry]$ sed -n 'h;n;H;x; s/\n/, /; :Loop;/Paulo/s/^/-/; /----/!t Loop; p' books.txt當(dāng)執(zhí)行上面的代碼,就會(huì)產(chǎn)生下面的結(jié)果。
A Storm of Swords, George R. R. Martin The Two Towers, J. R. R. Tolkien ----The Alchemist, Paulo Coelho The Fellowship of the Ring, J. R. R. Tolkien ----The Pilgrimage, Paulo Coelho A Game of Thrones, George R. R. Martin
from: http://www.yiibai.com/sed/sed_useful_recipes.html
總結(jié)
以上是生活随笔為你收集整理的Sed教程(二):基本语法、循环、分支的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Sed教程(一):简介、环境设置、工作流
- 下一篇: Sed教程(三):模式缓冲区、模式范围