Linux Bash文本操做之sed篇其二

上一篇總結了sed的基礎應用(Linux Bash文本操做之sed篇其一),內容實在有夠多,這裏再對稍微高級一些的用法作一個整理,以方便使用時查閱。html

查看文本內容linux

示例1表示在第一到第四行匹配到的行後面添加一行空行。正則表達式

示例2帶行號顯式文本,行號與內容間隔兩個空格,也能夠是使用\t換成製表符。shell

示例3能夠用來實現相似於grep的操做。使用正則表達式顯示文中匹配到sed的行。express

示例4中使用正則表達式配合d指令實現了與3相同的結果。測試

示例5是grep命令獲得的結果,與前兩個示例所不一樣的是grep顯示出來的是帶有顏色信息的,清晰地指明瞭匹配發生的位置。ui

利用標註的標籤進行跳轉,至關於分支,用於改變流處理順序。spa

示例6顯式匹配到字符串的下一行的內容,使用n來讀取下一行內容到模式空間,替換當前行內容。.net

示例7使文本顯示爲左對齊。rest

: label     Label for b and t commands.
b           label Branch to label; if label is omitted, branch to end of script.
t label     If a s/// has done a successful substitution since the last input line was read and
            since the last t or T command, then branch to label; if label is omitted, branch to
            end of script.
cv@cv:~/myfiles$ sed -n '1,4{/stream/G;p}' test.txt    #example-1
NAME
       sed - stream editor for filtering and transforming text

SYNOPSIS
       sed [OPTION]... {script-only-if-no-other-script} [input-file]...
cv@cv:~/myfiles$ sed = test.txt | sed 'N;s/\n/  /'    #example-2
1  NAME
2         sed - stream editor for filtering and transforming text
3  SYNOPSIS
4         sed [OPTION]... {script-only-if-no-other-script} [input-file]...
5  DESCRIPTION
6         Sed  is  a  stream  editor.  A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline).  While in some ways similar to an
7         editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient.  But it is sed's ability to filter text
8         in a pipeline which particularly distinguishes it from other types of editors.
9
10         -n, --quiet, --silent
11                suppress automatic printing of pattern space
12         -e script, --expression=script
13                add the script to the commands to be executed
14         -f script-file, --file=script-file
15                add the contents of script-file to the commands to be executed
16         --follow-symlinks
17                follow symlinks when processing in place
18         -i[SUFFIX], --in-place[=SUFFIX]
cv@cv:
~/myfiles$ sed -n '/sed/p' test.txt #example-3 sed - stream editor for filtering and transforming text sed [OPTION]... {script-only-if-no-other-script} [input-file]... Sed is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient. But it is sed's ability to filter text cv@cv:~/myfiles$ sed '/sed/!d' test.txt #example-4 sed - stream editor for filtering and transforming text sed [OPTION]... {script-only-if-no-other-script} [input-file]... Sed is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient. But it is sed's ability to filter text cv@cv:~/myfiles$ grep 'sed' test.txt #example-5 sed - stream editor for filtering and transforming text sed [OPTION]... {script-only-if-no-other-script} [input-file]... Sed is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient. But it is sed's ability to filter text
cv@cv:~/myfiles$ sed -n '/OPTION/{n;p}' test.txt         #example-6
DESCRIPTION

cv@cv:~/myfiles$ sed ':a;s/^.\{1,70\}$/ &/;ta;s/\( *\)//' test.txt     #example-7
NAME
sed - stream editor for filtering and transforming text
SYNOPSIS
sed [OPTION]... {script-only-if-no-other-script} [input-file]...
DESCRIPTION
Sed  is  a  stream  editor.  A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline).  While in some ways similar to an
editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient.  But it is sed's ability to filter text
in a pipeline which particularly distinguishes it from other types of editors.

-n, --quiet, --silent
suppress automatic printing of pattern space
-e script, --expression=script
add the script to the commands to be executed
-f script-file, --file=script-file
add the contents of script-file to the commands to be executed
--follow-symlinks
follow symlinks when processing in place
-i[SUFFIX], --in-place[=SUFFIX]

咱們還能夠結合臨時空間與模式空間之間的特性實現將文本先後各行順序顛倒的效果,也就是第一行變成最後一行,第二行變成倒數第二行,第三行變成倒數第三行……最後一行變成第一行,相似於  tac  的操做。

下面有一張圖清晰的代表了整個轉換過程,圖的來源是參考文獻[3]給出的連接。

cv@cv:~/myfiles$ sed '1!G;h;$!d;' test.txt
       -i[SUFFIX], --in-place[=SUFFIX]
              follow symlinks when processing in place
       --follow-symlinks
              add the contents of script-file to the commands to be executed
       -f script-file, --file=script-file
              add the script to the commands to be executed
       -e script, --expression=script
              suppress automatic printing of pattern space
       -n, --quiet, --silent

       in a pipeline which particularly distinguishes it from other types of editors.
       editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient.  But it is sed's ability to filter text
       Sed  is  a  stream  editor.  A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline).  While in some ways similar to an
DESCRIPTION
       sed [OPTION]... {script-only-if-no-other-script} [input-file]...
SYNOPSIS
       sed - stream editor for filtering and transforming text
NAME

還能夠實現相似於tail的效果。

如示例1至關於 tail -n 2 ,除最後兩行以外合併,以  \n  相鏈接,而後再從模式空間刪除。

D表示刪除模式空間中從第一個字符到第一個換行符的內容,而且跳轉到命令開頭從新執行。而且當模式空間仍有內容時,不讀入新的輸入行,相似造成一個循環。

P命令僅打印模式空間中從第一個字符到第一個換行符的內容,從新在模式空間的內容上執行編輯命令,相似造成一個循環。

示例2打印最後  m-1  行,經過  N  和  D  命令的循環來實現。若是到最後一行,終止退出,不然的話讀入下一行追加到當前模式空間。

示例3用於打印匹配行的下一行內容。

示例4用於打印匹配行的上一行與下一行。

D   If pattern space contains no newline, start a normal new cycle as if the d command was issued.
    Otherwise, delete text in the pattern space up to the first newline, and restart cycle with the
    resultant pattern space, without reading a new line of input.
cv@cv:~/myfiles$ sed '$!N;$!D' test.txt             #example-1

cv@cv:~/myfiles$ sed ':a;$q;N;3,$D;ba;' test.txt     #example-2
              follow symlinks when processing in place
       -i[SUFFIX], --in-place[=SUFFIX]
cv@cv:~/myfiles$ sed -n '/sed/{g;1!p;};h' test.txt    #example-3
NAME
SYNOPSIS
DESCRIPTION
DESCRIPTION

cv@cv:~/myfiles$ sed -n '/sed/{x;1!p;=;g;$!N;p;D;};h' test.txt    #example-4
NAME
2
       sed - stream editor for filtering and transforming text
SYNOPSIS
SYNOPSIS
4
       sed [OPTION]... {script-only-if-no-other-script} [input-file]...
DESCRIPTION
DESCRIPTION
6
       Sed  is  a  stream  editor.  A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline).  While in some ways similar to an
       editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient.  But it is sed's ability to filter text
       Sed  is  a  stream  editor.  A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline).  While in some ways similar to an
7
       editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient.  But it is sed's ability to filter text
       in a pipeline which particularly distinguishes it from other types of editors.

刪除文本內容

還有一種  addr1  爲  0  的地址範圍表示法,不過這時  addr2  只能使用正則表達式來表示。其結果與前面給出的兩個地址的結果相同。

示例1刪除每一行開頭全部空格。

示例2表示刪除每一行末尾全部空格。

示例3是1和2的綜合做用,刪除每一行開頭和末尾的全部空格。

示例4表示只刪除匹配到的第一行內容。

0,addr2    Start out in "matched first address" state, until addr2 is found. This is similar to 1,addr2, except that if addr2 matches the
           very first line of input the  0,addr2  form will be at the end of its range, whereas the 1,addr2 form will still be at the beginning
       of its range. This works only when addr2 is a regular expression.
cv@cv:~/myfiles$ sed -n 's/^[ ^t]*//p' test.txt    #example-1

cv@cv:~/myfiles$ sed -n 's/[ ^t]*$//p' test.txt    #example-2

cv@cv:~/myfiles$ sed -n -e 's/^[ ^t]*//;s/[ ^t]*$//p' test.txt    #example-3
cv@cv:~/myfiles$ sed '0,/sed/{//d;}' test.txt    #example-4
NAME
SYNOPSIS
       sed [OPTION]... {script-only-if-no-other-script} [input-file]...
DESCRIPTION
       Sed  is  a  stream  editor.  A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline).  While in some ways similar to a
       editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient.  But it is sed's ability to filter tex
       in a pipeline which particularly distinguishes it from other types of editors.

       -n, --quiet, --silent
              suppress automatic printing of pattern space
       -e script, --expression=script
              add the script to the commands to be executed
       -f script-file, --file=script-file
              add the contents of script-file to the commands to be executed
       --follow-symlinks
              follow symlinks when processing in place
       -i[SUFFIX], --in-place[=SUFFIX]

對於空行的刪除咱們還能夠有不少不一樣的方法。能夠在咱們的測試文件裏添加一些空行試試效果。

示例1表示刪除文本中正文第一行以前的若干空行。

示例2刪除文中連續的空行,只保留一行,也能夠理解爲將中間連續的空行合併成一行。

示例3將多於一行的連續空行刪除掉,或者說合併成一行,與上面的命令效果相同。

示例4將多於兩行的連續空行刪減成兩行,或者說只保留兩行。

示例5刪除最尾部的空白行,找到最後一行則刪除,不是最後一行用N命令追加到其下一行。

cv@cv:~/myfiles$ sed '/./,$!d' test.txt              #example-1

cv@cv:~/myfiles$ sed '/./,/^$/!d' test.txt           #example-2

cv@cv:~/myfiles$ sed '/^$/N;/\n$/D' file.txt         #example-3

cv@cv:~/myfiles$ sed '/^$/N;/\n$/N;//D' test.txt     #example-4

cv@cv:~/myfiles$ sed ':a;/^\n*$/{$d;N;ba}' test.txt  #example-5

增長文件內容

下面的指令用於交換模式空間和臨時空間的內容。

示例1實如今每個匹配行的前面添加一行空行。讀到文本的一行後,模式空間內爲該行內容,臨時空間暫時爲空,此時若是交換二者內容,模式空間變爲空,打印出來就是空行;

而後再將二者交換回來,模式空間又變成了剛纔讀到的一行內容,這是再打印就是文本內容了。最終結果就是在文本以前多了一行空行。

示例2實如今每個匹配行的前面和後面各添加一行空行。

第二次交換後模式空間中爲原來讀到的一行內容,臨時空間爲空,這時將臨時空間的內容追加到模式空間以後,就至關於在行後又添加了一行空行。

x   Exchange the contents of the hold and pattern spaces.
cv@cv:~/myfiles$ sed -n '/sed/{x;p;x;p}' test.txt    #example-1

       sed - stream editor for filtering and transforming text

       sed [OPTION]... {script-only-if-no-other-script} [input-file]...

       Sed  is  a  stream  editor.  A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline).  While in some ways similar to an

       editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient.  But it is sed's ability to filter text

cv@cv:~/myfiles$ sed -n '/sed/{x;p;x;G;p}' test.txt | sed '$d'    #example-2

       sed - stream editor for filtering and transforming text


       sed [OPTION]... {script-only-if-no-other-script} [input-file]...


       Sed  is  a  stream  editor.  A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline).  While in some ways similar to an


       editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient.  But it is sed's ability to filter text

替換/轉換文本內容

以前咱們使用的都是基本正則表達式,若是在環境選擇時加上-r的選項,則可使用擴展正則表達式。

在基本正則中,幾個字符(  ? + {} () |  )默認被解釋爲普通字符,也即字面意思,跟  a b c  這樣的字符同樣表示 ASCII 字符,若是想要使用其對應的特殊含義,如使用  ( )  表示選項列表,  |  表示選項等,則須要加反斜槓  \  轉義。

而在擴展正則中,默認狀況下上述字符  ? + {} () |  被解釋爲特殊含義,若是想要使用其所對應的普通含義,做爲一個普通字符來使用,須要加反斜槓  \  轉義。

-r, --regexp-extended    use extended regular expressions in the script.

示例1表示將全部以s或S開頭的單詞轉換成所有大寫的形式。  \<  詞首錨定,用於匹配單詞詞首。

示例2是使用以前所說的單詞邊界匹配符,能夠獲得一樣的結果,注意這裏的  () +  在普通正則中使用時須要轉義成特殊含義。

示例3表示在匹配到字符串的第一行,將其中的sed替換成目標字符串。

cv@cv:~/myfiles$ sed -n -r 's/\<(s|S)[a-z]+/\U&/gp' test.txt    #example-1
       SED - STREAM editor for filtering and transforming text
       SED [OPTION]... {SCRIPT-only-if-no-other-SCRIPT} [input-file]...
       SED  is  a  STREAM  editor.  A STREAM editor is used to perform basic text transformations on an input STREAM (a file or input from a pipeline).  While in SOME ways SIMILAR to an
       editor which permits SCRIPTED edits (SUCH as ed), SED works by making only one pass over the input(s), and is consequently more efficient.  But it is SED's ability to filter text
       -n, --quiet, --SILENT
              SUPPRESS automatic printing of pattern SPACE
       -e SCRIPT, --expression=SCRIPT
              add the SCRIPT to the commands to be executed
       -f SCRIPT-file, --file=SCRIPT-file
              add the contents of SCRIPT-file to the commands to be executed
       --follow-SYMLINKS
              follow SYMLINKS when processing in place
cv@cv:~/myfiles$ sed -n -e 's/\b\(s\|S\)[a-z]\+/\U&/gp' test.txt    #example-2
SED - STREAM editor for filtering and transforming text SED [OPTION]... {SCRIPT-only-if-no-other-SCRIPT} [input-file]... SED is a STREAM editor. A STREAM editor is used to perform basic text transformations on an input STREAM (a file or input from a pipeline). While in SOME ways SIMILAR to an editor which permits SCRIPTED edits (SUCH as ed), SED works by making only one pass over the input(s), and is consequently more efficient. But it is SED's ability to filter text -n, --quiet, --SILENT SUPPRESS automatic printing of pattern SPACE -e SCRIPT, --expression=SCRIPT add the SCRIPT to the commands to be executed -f SCRIPT-file, --file=SCRIPT-file add the contents of SCRIPT-file to the commands to be executed --follow-SYMLINKS follow SYMLINKS when processing in place cv@cv:~/myfiles$ sed -n '0,/sed/s//to_that/p' test.txt #example-3 to_that - stream editor for filtering and transforming text

在根據匹配進行字符串等的替換時,咱們能夠指定替換第幾個匹配。

如示例1與示例2,在s//num中num指的是第幾個匹配位置,好比1表示該行第一個匹配,2表示第二個。

示例1就表示將每行第一個sed替換成目標字符串。

示例2就表示將每行第二個替換掉。

示例3表示替換倒數第二個匹配。

示例4表示替換倒數第一個,也就是最後一個匹配。

cv@cv:~/myfiles$ sed -n 's/sed/XXX/1p' test.txt    #example-1
       XXX - stream editor for filtering and transforming text
       XXX [OPTION]... {script-only-if-no-other-script} [input-file]...
       Sed  is  a  stream  editor.  A stream editor is uXXX to perform basic text transformations on an input stream (a file or input from a pipeline).  While in some ways similar to an
       editor which permits scripted edits (such as ed), XXX works by making only one pass over the input(s), and is consequently more efficient.  But it is sed's ability to filter text

cv@cv:~/myfiles$ sed -n 's/sed/XXX/2p' test.txt    #example-2
       editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient.  But it is XXX's ability to filter text

cv@cv:~/myfiles$ sed -n 's/\(.*\)sed\(.*sed\)/\1XXX\2/p' test.txt    #example-3
       editor which permits scripted edits (such as ed), XXX works by making only one pass over the input(s), and is consequently more efficient.  But it is sed's ability to filter text

cv@cv:~/myfiles$ sed -n 's/\(.*\)sed/\1XXX/p' test.txt    #example-4
       XXX - stream editor for filtering and transforming text
       XXX [OPTION]... {script-only-if-no-other-script} [input-file]...
       Sed  is  a  stream  editor.  A stream editor is uXXX to perform basic text transformations on an input stream (a file or input from a pipeline).  While in some ways similar to an
       editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient.  But it is XXX's ability to filter text

 

參考文獻

[1] Learning Linux Commands: sed

[2] Shell正則表達式

[3] 【系統工程師的自我修養】sed篇

相關文章
相關標籤/搜索