Linux Bash文本操做之sed篇其一

做爲Linux系統中文本處理的強力工具之一,sed功能強大,用法多變,值得咱們好好學習。html

sed是用於過濾和轉換文本的流編輯器。正則表達式

通常狀況下sed把當前處理的行存儲在臨時緩衝區,按指定命令處理以後將緩衝區內容輸出到屏幕,固然可使用  -n  選項使得不打印內容到屏幕。另外這些操做默認對原文本沒有影響,不會改變原來的文本內容。shell

可是若是咱們確實想要將處理結果做用於原文本,使用  -i  選項將修改附加到原文件,注意要謹慎使用!express

調用方式vim

  • 命令行輸入
sed -e 'command' input_file
  • 腳本文件輸入
sed -f script_file input_file

下面經過一些實際操做說明一下 sed (未加說明便是指 sed (GNU sed) 4.2.2 ,下同)經常使用參數的含義和用法windows

首先得到實驗文本api

cv@cv: ~/myfiles$ touch test.txt
cv@cv: ~/myfiles$ man sed | head -n 30 | tail -n 28 > test.txt
cv@cv: ~/myfiles$ cat test.txt
 1 NAME
 2        sed - stream editor for filtering and transforming text
 3 
 4 SYNOPSIS
 5        sed [OPTION]... {script-only-if-no-other-script} [input-file]...
 6 
 7 DESCRIPTION
 8        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
 9        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
10        in a pipeline which particularly distinguishes it from other types of editors.
11 
12        -n, --quiet, --silent
13 
14               suppress automatic printing of pattern space
15 
16        -e script, --expression=script
17 
18               add the script to the commands to be executed
19 
20        -f script-file, --file=script-file
21 
22               add the contents of script-file to the commands to be executed
23 
24        --follow-symlinks
25 
26               follow symlinks when processing in place
27 
28        -i[SUFFIX], --in-place[=SUFFIX]
test.txt

而後根據咱們得到的文本進行實驗操做,這裏還要說明一下,下面的展現中,緩存

如 d Delete pattern... 這樣單獨列出來的是命令的含義,摘自sed手冊;app

如 -e 這樣表示環境配置指令,不在引號表示的指令中出現,應該放在引號指令以前;less

如  d  這樣的要在 'command' 中才能使用。

刪除操做

 d  表示刪除模式空間中的內容,進行下一個循環。 number 用來匹配單獨的一行, regexp 爲正則項,用來尋找符合條件的內容。

 ^  是正則表達式中經常使用來匹配行首的字符。如  ^#  匹配以  #  開頭的行。

示例1用來展現刪除第二行的操做。

示例2表示刪除第二到第十行,包括第二行。

示例3表示刪除空行,注意該命令只能刪除純空行,也就是不能是由空格、製表符等組成的空行。

示例4中小數點能夠用來匹配任意一個字符,包括空格製表符等,感嘆號表示除了這些以外的全部行,二者結合就表示不包含任意字符的行即純空行,所以它的做用就是刪除文本中全部空行。

示例5表示若是不想將全部的空行刪除,好比我只想將前四行中的空行刪掉,可使用下面的指令。

示例6表示刪除指定行行首的空格,替換爲製表符 。

示例7表示刪除從匹配到第一個Sed開始的行到第一個pipeline所在行之間的部分。

d          Delete pattern space.  Start next cycle.
number     Match only the specified line number (which increments cumulatively across files, unless the -s option is specified on the command line).
/regexp/   Match lines matching the regular expression regexp.
cv@cv:~/myfiles$ sed '2d' test.txt  #example-1

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

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

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

cv@cv:~/myfiles$ sed '1,4{/^$/d}' test.txt  #example-5

cv@cv:~/myfiles$ sed '6,10s/^[[:space:]]*/\t/g' test.txt  #example-6

cv@cv:~/myfiles$ sed '/Sed/,/pipeline/d' 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

       -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]

瞭解了這些以後咱們能夠試着將處理做用於原文件,能夠查看一下效果。

-i[SUFFIX], --in-place[=SUFFIX]    edit files in place (makes backup if SUFFIX supplied)
cv@cv:~/myfiles$ sed -i '1,6{/^$/d};12,${/^$/d}' test.txt
cv@cv:~/myfiles$ cat test.txt
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]

查看操做

默認狀況下,sed打印當前緩存區中的輸入行,也就是說它會把全部輸入行都打印在標準輸出上。若是在某一行匹配到給定字符串,該行就會被另外打印一遍。

選項  -n  取消sed取消默認打印操做。命令  p  指示sed將再次打印該行。此兩者配合使用,模式緩衝區內的輸入行,只被打印一次。

示例1表示輸出第三到五行內容。

示例2表示打印全部匹配行的行號。

示例3表示打印全部匹配行的內容。

示例4表示既打印行號又打印內容。

示例5表示查看從第二行開始到匹配到的行之間的內容。

示例6表示打印全部以三個大寫字母開頭的行。  ^  匹配行首, [A-Z] 匹配一個大寫字母, {3} 表示前面的字母有三個,綜合起來就表示以三個大寫字母開頭。

示例7用來打印長度超過60個字符的全部行。

示例8與此相對,打印長度不超過60個字符的全部行。

-e script, --expression=script    add the script to the commands to be executed
-n, --quiet, --silent    suppress automatic printing of pattern space
=    Print the current line number.
p    Print the current pattern space.
cv@cv:~/myfiles$ sed -n '3,5p' test.txt  #example-1
SYNOPSIS
       sed [OPTION]... {script-only-if-no-other-script} [input-file]...
DESCRIPTION

cv@cv:~/myfiles$ sed -n '/[sS]ed/=' test.txt  #example-2

cv@cv:~/myfiles$ sed -n '/[sS]ed/p' test.txt  #example-3

cv@cv:~/myfiles$ sed -n -e '/[sS]ed/=' -e '/[sS]ed/p' test.txt  #example-4
2
       sed - stream editor for filtering and transforming text
4
       sed [OPTION]... {script-only-if-no-other-script} [input-file]...
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

cv@cv:~/myfiles$ sed -n '2,/quiet/p' test.txt  #example-5
    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

cv@cv:~/myfiles$ sed -n '/^[A-Z]\{3\}/p' test.txt      #example-6
NAME
SYNOPSIS
DESCRIPTION

cv@cv:~/myfiles$ sed -n '/^.\{60\}/p' test.txt    #example-7
       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
       in a pipeline which particularly distinguishes it from other types of editors.
              add the contents of script-file to the commands to be executed

cv@cv:~/myfiles$ sed -n '/^.\{60\}/!p' test.txt    #example-8
NAME
SYNOPSIS
DESCRIPTION

       -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
       --follow-symlinks
              follow symlinks when processing in place
       -i[SUFFIX], --in-place[=SUFFIX]

甚至能夠設置奇偶行或者指定間隔的行輸出和操做,  first  指的是其實匹配行,  step  指的是步長。以下面的例子

示例1表示從第一行開始,每隔兩行輸出一次,也就是輸出全部奇數行的內容。

示例2爲了更清晰地展現輸出的是第幾行的內容,稍微改動一下指令,使用  =  顯示行號便可。

first~step    Match  every step th line starting with line first.  For example, ``sed -n 1~2p'' will print
              all the odd-numbered lines in the input stream, and the address 2~5 will match every fifth line,
              starting with the second. first can be zero; in this case, sed operates as if it were equal to
              step. (This is an extension.)
cv@cv:~/myfiles$ sed -n '1~2p' test.txt      #example-1

cv@cv:~/myfiles$ sed -n '1~2=;1~2p' test.txt   #example-2
1
NAME
3
SYNOPSIS
5
DESCRIPTION
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
9

11
              suppress automatic printing of pattern space
13
              add the script to the commands to be executed
15
              add the contents of script-file to the commands to be executed
17
              follow symlinks when processing in place

增長一行或幾行內容

示例1表示在每一行以後輸出 "my name is lee" 

示例2表示只在第二行以後輸出。

示例3表示只在最後一行輸出,相似於vim中的o,在行後插入內容。

示例4表示在最後一行以前輸出,相似於vim中的O,在行前插入內容。

示例5表示在匹配到的行以後添加一行內容。考慮到在最後一行以後再添加空行反而影響輸出效果,咱們能夠經過添加  $!  來取消對最後一行的操做。

示例6和7表示在每一行的後面添加一空行,總體文本行間距加倍,連續兩個  G  使後面添加兩行空行。一樣的,對最後一行不操做。

a text    Append text, which has each embedded newline preceded by a backslash.
i text    Insert text, which has each embedded newline preceded by a backslash.
$         Match the last line.
G         Copy/append hold space to pattern space.
cv@cv: ~/myfiles$ sed 'a my name is lee' test.txt   #example-1

cv@cv: ~/myfiles$ sed '2a my name is lee' test.txt  #example-2

cv@cv: ~/myfiles$ sed '$a my name is lee' test.txt  #example-3

cv@cv: ~/myfiles$ sed '$i my name is lee' test.txt  #example-4

cv@cv:~/myfiles$ sed '/quiet/a\ inserted line' test.txt    #example-5
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
 inserted line

cv@cv:~/myfiles$ sed '$!G' test.txt        #example-6
cv@cv:~/myfiles$ sed '$!G;$!G' test.txt     #example-7

替換操做

以下面的例子所示。

示例1表示將第二行替換爲 "my name is lee" 。

示例2表示將第二行到最後一行替換爲 "my name is lee" ,也便是刪除選中行,再插入給定的一行。

c text    Replace the selected lines with text, which has each embedded newline preceded by a backslash.
cv@cv:~/myfiles$ sed '2c my name is lee' test.txt      #example-1
cv@cv:~/myfiles$ sed '2,$c my name is lee' test.txt     #example-2

除了像上面這種命令直接替換以外,還有一種更強大的模式匹配替換。

格式: [address[,address]]s/pattern-find/replacement-pattern/[g,p,w,n] 

  g  對模式空間全部出現的狀況進行全局更改,缺省狀態只替換首次出現的模式。

  p  打印模式空間的內容,上面已經解釋過了。

  w  後面跟文件名,表示將替換操做重定向到指定文件。

  n  表明 [1, 512] 之間的一個數字,表示對本模式中指定模式第n次出現的狀況進行替換。

示例1表示將文中的sed或Sed替換成SED,只替換每一行首次出現的匹配字符串 ,這是缺省設置。

示例2表示使用參數  g ,與上面的結果對比,能夠發現使用該參數會替換全部行內出現的全部匹配字符串而不僅是第一次出現的。注意第六行的 used 在示例1中並無改變。

示例3表示使用參數p只顯示被改變的行,但直接像示例這樣還不行,會將全部改變的行多顯示一遍,所以咱們常將  p  和  n  一塊兒使用。

示例4表示使用 --quiet/silent 參數完成上面的任務,只顯示被改變的行。

示例5表示將第一行到第三行之間的全部sed或Sed替換成SED,後面使用  p  和 gp  等價,結果徹底同樣。

示例6表示多個命令使用分隔符 ';' 隔離開,而且可使用其餘的分隔符來代替經常使用的默認的 '/' 。

示例7展現了將替換部分重定向到指定文件的操做。

s/regexp/replacement/    Attempt to match regexp against the pattern space. If successful, replace that portion matched with replacement.
                         The replacement may contain the special character & to refer to that portion of the pattern space which matched,
                         and the special escapes \1 through \9 to refer to the corresponding matching sub-expressions in the regexp.
cv@cv:~/myfiles$ sed 's/[sS]ed/\U&/' test.txt   #example-1

cv@cv:~/myfiles$ sed 's/[sS]ed/\U&/g' test.txt  #example-2

cv@cv:~/myfiles$ sed 's/[sS]ed/\U&/p' test.txt  #example-3

cv@cv:~/myfiles$ sed -n 's/[sS]ed/\U&/p' test.txt    #example-4

cv@cv:~/myfiles$ sed -n '2,4s/[sS]ed/\U&/p' test.txt  #example-5
cv@cv:~/myfiles$ sed -n '2,4s/[sS]ed/\U&/gp' test.txt

cv@cv:~/myfiles$ sed -n '2,4s/[sS]ed/\U&/gp;6,7s#editor#EdItOr#gp' test.txt  #example-6

cv@cv:~/myfiles$ touch output    # example-7
cv@cv:~/myfiles$ sed 's/[sS]ed/SED/gw output'  test.txt
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]

cv@cv:~/myfiles$ cat output
       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

示例1使用I指令是sed對大小寫不敏感,能夠此來簡化上面示例中的[]表達式。

示例2利用正則表達式匹配到的行做爲操做當前行,將該行中的sed替換成SED。

示例3做用與2正相反,對未匹配到editor字符串且包含有sed模式的行進行操做,將其中的sed替換成SED。

示例4先匹配文中的sed字符串,找到以後用下一個輸入行替代模式空間中的當前行,執行替換操做,而後打印該行,再進行其餘操做。

cv@cv:~/myfiles$ sed -n 's/sed/SED/Ip' 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 '/editor/s/sed/SED/p' test.txt    #example-2
       SED - stream editor for filtering and transforming 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
       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 '/editor/!s/sed/SED/p' test.txt    #example-3
       SED [OPTION]... {script-only-if-no-other-script} [input-file]...

cv@cv:~/myfiles$ sed -n '/sed/{n;s/sed/SED/p;}' test.txt    #example-4
        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

還可使用匹配到的行去替換另外一種匹配獲得的行,這裏

 h  是將模式空間內容拷貝到保持空間,相似於windows中複製以後保存到剪貼板上;

 g  表示將保持空間的內容拷貝到模式空間,也就是來替換咱們找到的匹配處內容。這裏的參數與剛剛測試過的正則表達式中的參數含義不一樣,請特別注意。

操做流程大體以下

  1. 讀取輸入文件流;
  2. 讀取文件流中一行內容到 Pattern Space ;
  3. 在 Pattern Space 與 Hold Space 中執行命令;
  4. 輸出 Pattern Space 中的內容;
  5. 清空 Pattern Space ;
  6. 判斷是否爲文件末尾 EOF ;
  7. 若是還沒有到末尾,讀取文件下一行內容,返回步驟2循環操做;
  8. 若是爲文件末尾,終止循環,結束操做流程。

示例1的做用是尋找文本中包含 NAME 的行,並用它來替換全部包含 pipeline 的行。

示例2的分析能夠結合上面的流程進行。

h H    Copy/append pattern space to hold space.
g G    Copy/append hold space to pattern space.
cv@cv:~/myfiles$ sed -e '/NAME/h' -e '/pipeline/g' test.txt  #example-1
cv@cv:~/myfiles$ cat test.txt
NAME
       sed - stream editor for filtering and transforming text
SYNOPSIS
       sed [OPTION]... {script-only-if-no-other-script} [input-file]...
DESCRIPTION
NAME
       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
NAME

       -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]
cv@cv:~/myfiles$ seq 5 | sed 'H;g'    #example-2

1

1
2

1
2
3

1
2
3
4

1
2
3
4
5

轉換內容操做

y/source/dest/    Transliterate the characters in the pattern space which appear in source to the corresponding character in dest.

用來將原字符轉換成指定字符,只能一個一個轉換。不經常使用,用的時候要注意做用範圍。以下面的示例。

cv@cv:~/myfiles$ sed 'y/Ssed/XXYZ/' test.txt
NAME
       XYZ - XtrYam YZitor for filtYring anZ tranXforming tYxt
XYNOPXIX
       XYZ [OPTION]... {Xcript-only-if-no-othYr-Xcript} [input-filY]...
DEXCRIPTION
       XYZ  iX  a  XtrYam  YZitor.  A XtrYam YZitor iX uXYZ to pYrform baXic tYxt tranXformationX on an input XtrYam (a filY or input from a pipYlinY).  WhilY in XomY wayX Ximilar to an
       YZitor which pYrmitX XcriptYZ YZitX (Xuch aX YZ), XYZ workX by making only onY paXX ovYr thY input(X), anZ iX conXYquYntly morY YfficiYnt.  But it iX XYZ'X ability to filtYr tYxt
       in a pipYlinY which particularly ZiXtinguiXhYX it from othYr typYX of YZitorX.

       -n, --quiYt, --XilYnt
              XupprYXX automatic printing of pattYrn XpacY
       -Y Xcript, --YxprYXXion=Xcript
              aZZ thY Xcript to thY commanZX to bY YxYcutYZ
       -f Xcript-filY, --filY=Xcript-filY
              aZZ thY contYntX of Xcript-filY to thY commanZX to bY YxYcutYZ
       --follow-XymlinkX
              follow XymlinkX whYn procYXXing in placY
       -i[XUFFIX], --in-placY[=XUFFIX]

 \b  用來匹配單詞邊界,就是單詞的開頭或結尾的意思,例如給定一個單詞 hello ,使用  \bhe  能夠找到該單詞,  h  爲單詞的左邊界,  llo\b  也能夠匹配到,  o  爲單詞的右邊界。

同理  \B  用來匹配非單詞邊界,也就是要匹配的內容必須包含在單詞中間,不能是左邊界也不能是右邊界,  \Bell, ell\B, \Bell\B  均可以匹配到單詞,  \Bhe  或者  llo\B  就不行,由於  h  和  o  是單詞邊界。

示例1中後面的  \U&  意思是將符合條件的過濾項轉換成所有大寫的形式,該示例表示  \b  用來匹配文本中單詞開頭或結尾字符,  \btw  這裏指以  tw  開頭的單詞。

示例2中  tw\b  這裏指的是以  tw  結尾的單詞。

示例3尋找以  tw  開頭且以  tw  結尾,匹配形如  tw、twtw、tw*tw  的單詞。

示例4中  \B  用來匹配文本中非單詞開頭和結尾字符,這裏指的是單詞中包含  tw  可是  tw  不在開頭也不在結尾處。

示例5尋找不以  tw  結尾的單詞。

示例6尋找不以  tw  開頭也不以  tw  結尾,只能在單詞中間存在。

示例7尋找以  tw  開頭但不以  tw  結尾,  twtw  這樣的就不符合篩選條件。

示例8尋找不以  tw  開頭並且以  tw  結尾的單詞。

\b    matches the empty string at the edge of a word
\B    matches the empty string provided it's not at the edge of a word
cv@cv:~/myfiles$ echo "one two three btw is the abbr of by the way whether twher is meaningful? SHA random code twfdoetw tw wsr239wfgrte see-you-tw-tommorrow" >> test.txt

cv@cv:~/myfiles$ sed -n 's/\btw/\U&/gp' test.txt    #example-1
one TWo three btw is the abbr of by the way whether TWher is meaningful? SHA random code TWfdoetw TW wsr239wfgrte seeyoutwtommorrow

cv@cv:~/myfiles$ sed -n 's/tw\b/\U&/gp' test.txt    #example-2
one two three bTW is the abbr of by the way whether twher is meaningful? SHA random code twfdoeTW TW wsr239wfgrte seeyoutwtommorrow

cv@cv:~/myfiles$ sed -n 's/\btw\b/\U&/gp' test.txt   #example-3
one two three btw is the abbr of by the way whether twher is meaningful? SHA random code twfdoetw TW wsr239wfgrte seeyoutwtommorrow

cv@cv:~/myfiles$ sed -n 's/\Btw/\U&/gp' test.txt    #example-4
one two three bTW is the abbr of by the way whether twher is meaningful? SHA random code twfdoeTW tw wsr239wfgrte seeyouTWtommorrow

cv@cv:~/myfiles$ sed -n 's/tw\B/\U&/gp' test.txt    #example-5
one TWo three btw is the abbr of by the way whether TWher is meaningful? SHA random code TWfdoetw tw wsr239wfgrte seeyouTWtommorrow

cv@cv:~/myfiles$ sed -n 's/\Btw\B/\U&/gp' test.txt   #example-6
one two three btw is the abbr of by the way whether twher is meaningful? SHA random code twfdoetw tw wsr239wfgrte seeyouTWtommorrow

cv@cv:~/myfiles$ sed -n 's/\btw\B/\U&/gp' test.txt   #example-7
one TWo three btw is the abbr of by the way whether TWher is meaningful? SHA random code TWfdoetw tw wsr239wfgrte seeyoutwtommorrow

cv@cv:~/myfiles$ sed -n 's/\Btw\b/\U&/gp' test.txt   #example-8
one two three bTW is the abbr of by the way whether twher is meaningful? SHA random code twfdoeTW tw wsr239wfgrte seeyoutwtommorrow

cv@cv:~/myfiles$ sed -i '$d' test.txt          # delete the last line

這裏的  &  實際上是正則表達式中用來表明剛匹配過的字符串的符號。

示例1中在第一到四行找到匹配指定模式的行並將其匹配字符串左右各加一個等號。

示例2中包含在小括號內的模式  s  做爲標籤1保存在特定的寄存器中,替換串能夠經過  \1  來引用它,該例就是將 sed 中的 ed 替換成 ad 而保持首字母不變。

示例3中小括號內使用  \|  表示選擇,能夠匹配兩個中的任意一個,而後再執行與示例2相同的操做。

示例4也同樣能夠將第一行到第六行的sed或Sed替換成SED,這裏的  \|  也表示選擇。

示例5表示尋找包含其中一種模式的行,括號是正則表達式中的額選擇項。

cv@cv:~/myfiles$ sed -n '1,4s/sed/=&=/p' test.txt    #example-1
       =sed= - stream editor for filtering and transforming text
       =sed= [OPTION]... {script-only-if-no-other-script} [input-file]...

cv@cv:~/myfiles$ sed -n '1,6s/\(s\)ed/\1ad/p' test.txt    #example-2
       sad - stream editor for filtering and transforming text
       sad [OPTION]... {script-only-if-no-other-script} [input-file]...
       Sed  is  a  stream  editor.  A stream editor is usad to perform basic text transformations on an input stream (a file or input from a pipeline).  While in some ways similar to an

cv@cv:~/myfiles$ sed -n '1,6s/\(s\|S\)ed/\1ad/p' test.txt    #example-3
       sad - stream editor for filtering and transforming text
       sad [OPTION]... {script-only-if-no-other-script} [input-file]...
       Sad  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

cv@cv:~/myfiles$ sed -n '1,6s/sed\|Sed/SED/p' 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

cv@cv:~/myfiles$ sed '/\(file\|script\)/d' test.txt      #example-5
NAME
       sed - stream editor for filtering and transforming text
SYNOPSIS
DESCRIPTION
       in a pipeline which particularly distinguishes it from other types of editors.

       -n, --quiet, --silent
              suppress automatic printing of pattern space
       --follow-symlinks
              follow symlinks when processing in place
       -i[SUFFIX], --in-place[=SUFFIX]

合併指定兩行

說到合併兩行內容,就須要介紹  N  的含義和用法。

根據手冊以下內容,可知  N  的做用是讀取下一行內容到模式空間,也能夠理解爲把兩行讀入成中間帶  \n  換行的一行內容,sed本來是按行處理文本,  N  選項就是告訴sed把下一行同時讀取到模式空間待命。並無輸出當前模式空間中的行。

對於  n  指令,若是在某一行匹配到給定模式,  n  就指示sed,提早讀入下一個輸入行替換模式空間中的當前行,進行操做,而後再繼續往下處理。注意跟在  n  後的命令應用在新讀入的行上。

大寫字母  P  的做用是將模式空間中的第一行內容打印到屏幕;

小寫字母  p  的做用是將當前模式空間中的全部內容都打印到屏幕,注意區分一下。

示例1中使用  N  選項將1和2同時讀入模式空間,再依據  P  選項打印模式空間第一行內容,從而輸出1。而後讀取3和4,輸出3。最後讀取5,但爲何沒有輸出呢?由於當沒法處理下一行內容時,也就是讀不到第二行時,  N  會自動終止退出,

因此後面的  P  也不會執行,所以沒有打印5到屏幕上。

示例2與上面有所區別,這裏使用  $!N  選項,意思是對最後一行不執行  N  命令,直接進行下一步,繼續執行  P  ,打印模式空間第一行,也就是讀到的5,所以能輸出5。

示例3讀取最後一行5時,該命令中  N  沒法處理下一行內容,終止退出,不執行後面的替換語句,這裏另外使用了  -n  參數,沒有處理的命令再也不輸出,若是去掉該參數sed默認會輸出該行內容,因此5依然能夠輸出到屏幕。

示例4讀取最後一行到模式空間時,  N  沒起做用,後面的替換執行也不成功,由於末尾沒有換行符,但sed仍是會默認輸出該行內容,5也出如今了屏幕上。

注意這裏的小  p  放在替換語句結束符 ';' 以後,若是放在分號以前表示只打印輸出有改動的行,就會獲得示例5和6的結果,沒有5的輸出。

示例7中將文本第一行和第二行,第三行和第四行分別合併爲一行,中間以空格隔離開。

n    Read the next line of input into the pattern space.
N    Append the next line of input into the pattern space.
P    Print up to the first embedded newline of the current pattern space.(Capital)
p    Print the current pattern space.(Lowercase)
cv@cv:~/myfiles$ seq 5 | sed -n 'N;P'      #example-1
1
3

cv@cv:~/myfiles$ seq 5 | sed -n '$!N;P'        #example-2
1
3
5

cv@cv:~/myfiles$ seq 5 | sed -n 'N;s/\n/ /;p'  #example-3
1 2
3 4

cv@cv:~/myfiles$ seq 5 | sed -n '$!N;s/\n/ /;p' #example-4
1 2
3 4
5

cv@cv:~/myfiles$ seq 5 | sed -n 'N;s/\n/ /p'    #example-5
1 2
3 4

cv@cv:~/myfiles$ seq 5 | sed -n '$!N;s/\n/ /p'  #example-6
1 2
3 4

cv@cv:~/myfiles$ sed -n -e '$!N;1,4s/\n[[:space:]]*/ /p;' test.txt   #example-7
NAME sed - stream editor for filtering and transforming text
SYNOPSIS sed [OPTION]... {script-only-if-no-other-script} [input-file]...

其餘特性或指令的應用

關於匹配地址,做用範圍,手冊裏面給出了下面的解釋。當沒有給定範圍時,默認在整個文本範圍內進行匹配搜索,對全部行操做。當只給出一個地址時,只對該行進行搜索和操做。當給定兩個範圍時,在範圍中間進行操做。

而且也列出了三點注意事項,一個是地址之間用逗號隔開;第二個是 addr1 必定會操做,就算是 addr2 小於第一個地址的值,若是找不到  addr2  ,也即結束條件沒法知足,會一直操做到文件結尾處;第三個是當 addr2 以正則形式給出時,對於與 addr1 有衝突的按 addr1 執行,沒有太明白這句話表達的意思。

此外地址後面加  !  表示跳過指定的行,執行文本中的其餘全部行。

Addresses    
    Sed commands can be given with no addresses, in which case the command will be executed for all input lines;
    with one address, in which case the command will only be executed for input lines which match that address;
    or with two addresses, in which case the command will be executed for all input lines which match the inclusive range of lines starting from the first address and continuing to the second address.
    Three things to note about address ranges:
        >> the syntax is addr1,addr2 (i.e., the addresses are separated by a comma);
        >> the line which addr1 matched will always be accepted, even if addr2 selects an earlier line;
        >> and if addr2 is a regexp, it will not be tested against the line that addr1 matched.
    After the address (or address-range), and before the command, a ! may be inserted, which specifies that the command shall only be executed if the address (or address-range) does not match.

下面的第一個指令表示從  addr1  行開始往下匹配N行,總共匹配N+1行。

第二個指令表示從  addr1  開始往下匹配,一直到N的倍數所指的行結束,包括N的倍數所指的行。

addr1,+N    Will match addr1 and the N lines following addr1.
addr1,~N    Will match addr1 and the lines following addr1 until the next line whose input line number is a multiple of N.

以下面示例中那樣,用大括號能夠表示一系列命令。

{    Begin a block of commands (end with }).
}    The closing bracket of a { } block.

示例1表示顯示文中含有字符串sed或Sed的全部行。

示例2表示顯示第二行到第六行之間含有sed或Sed的全部行,包括第二行。

示例3表示從第二行開始往下四行中含有給定字符串的行,包括第二行。

示例4表示從第二行開始,到4的倍數所指的行爲止含有給定字符串的行,包括第二行和第四行。

示例5表示從第二行開始,到6的倍數所指的行爲止含有給定字符串的行,包括第二行和第六行。

cv@cv:~/myfiles$ sed -n '/[sS]ed/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 '2,6{/[sS]ed/p}' 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
cv@cv:~/myfiles$ sed -n '2,+4{/[sS]ed/p}' test.txt    #examle-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
cv@cv:~/myfiles$ sed -n '2,~4{/[sS]ed/p}' test.txt    #example-4
       sed - stream editor for filtering and transforming text
       sed [OPTION]... {script-only-if-no-other-script} [input-file]...
cv@cv:~/myfiles$ sed -n '2,~6{/[sS]ed/p}' 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
cv@cv:~/myfiles$ sed -n '2,4!{/[sS]ed/p}' test.txt    #example-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

 q  指令表示退出程序,當sed執行到該處時,將會終止程序,再也不進行其餘處理。

示例1表示執行完第五行退出。前面加行號表示取前若干行,在讀取大文件的前若干行時有很大的做用。

示例2表示尋找Sed匹配的行,找到後對該行中的Sed進行替換操做,操做完畢打印到屏幕上,而後終止程序退出。

q [exit-code]   Immediately quit the sed script without processing any more input, except that
                if auto-print is not disabled the current pattern space will be  printed.
                The exit code argument is a GNU extension.
cv@cv:~/myfiles$ sed '5q' test.txt    #example-1
NAME
       sed - stream editor for filtering and transforming text
SYNOPSIS
       sed [OPTION]... {script-only-if-no-other-script} [input-file]...
DESCRIPTION

cv@cv:~/myfiles$ sed -n '/Sed/{s/Sed/SAD/p;q}' test.txt    #example-2
       SAD  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

 剩下的內容留到下一篇接着寫。

參考資料

[1] sed命令用法

[2] Linux 經常使用命令sed/awk/grep及正則表達式

[3] shell命令-sed經常使用命令

[4] sed中標識符\b和\B的用法

[5] Linux三劍客之sed

[6] sed之N和$!N的區別和運用

[7] sed行處理詳解(交換行,合併行,刪除行等)

[8] A Sed and Awk Micro-Primer

相關文章
相關標籤/搜索