sed应用
sed應用
文章目錄
- sed應用
- 一.sedsed命令的基本語法
- 1. 刪除
- 2. 替換 1.0
- 2.1 語法
- 2.2 例1
- 2.3 例2
- 2.4 例3
- 2.5 例4
- 2.6 例5
- 2.7 例6
- 2.8 例7
- 2.9 例8
- 3. 刪除 d
- 3.1 例1
- 4. 追加(a)插入(i)和更改(c)
- 4.1 例1 追加 a
- 4.2 例2 插入 i
- 4.3 例3 更改 c
- 4.4 例4 i
- 4.5 例5
- 5. 轉換 y
- 6. 打印 p
- 6.1 例
- 7. 打印行號
- 7.1 例
- 8. 下一步 (next)n
- 8.1 例
- 9. 讀(r)和寫(w)
- 9.1 例 w
- 二. 高級sed命令
- 1. 多行模式空間
- 2. 追加下一行 N
- 2.1 例
- 2.2 例
- 3. 多行刪除 D
- 3.1 例
- 4. 多行打印 P
- 4.1 例
- 5. 包含那一行
- 5.1 例
- 三. 高級的流控制命令
一.sedsed命令的基本語法
sed OPTIONS… [SCRIPT] [INPUTFILE…]常用的選項:-n,–quiet: 不輸出模式空間中的內容-i: 直接編輯原文件,默認不對原文件進行操作-e: 可以使用多個命令(腳本)進行操作-f /path/from/sed_script: 從指定的文本中讀取處理腳本-r: 使用擴展正則表達式1. 刪除
- 刪除文件中的內容
2. 替換 1.0
2.1 語法
[address]s/pattern/seplacement/flages 地址 模式 替換 flages: n 1-512之間的一個數字,表示對文本模式中指定模式第n次出現 g 全局替換,默認只替換第一個 p 如果成功替換則打印 w 將模式中的內容寫入文件中 i 不區分大小寫2.2 例1
轉義符默認為/ 也可以用# !將/usr/local/src 改成/usrs/local/bin 1. [root@node1 ~]# cat abc /usr/local/src [root@node1 ~]# sed 's/\/usr\/local\/src/\/usrs\/local\/bin/' abc /usrs/local/bin [root@node1 ~]# 2. [root@node1 ~]# sed 's#/usr/local/src#/usrs/local/bin#' abc /usrs/local/bin3. [root@node1 ~]# sed 's!/usr/local/src!/usrs/local/bin!' abc /usrs/local/bin [root@node1 ~]#2.3 例2
Tab和\t制表符將文本的制表符第二個或者第三個換成> 1. [root@node1 ~]# cat abc Column1 Column2 Column3 Column4 [root@node1 ~]# sed 's/\t/>/2' abc Column1 Column2>Column3 Column4 [root@node1 ~]# sed 's/\t/>/3' abc Column1 Column2 Column3>Column4 [root@node1 ~]#2.4 例3
替換元字符 & 用正則表達式匹配的內容進行替換 \n 換行 \ 轉義符1.將文件換行 [root@node1 ~]# cat abc Column1 Column2 Column3 Column4 [root@node1 ~]# sed 's/\t/\ > /2' abc Column1 Column2 Column3 Column4 [root@node1 ~]# sed 's/\t/\n/2' abc Column1 Column2 Column3 Column4 [root@node1 ~]# sed 's/\t/\n/3' abc Column1 Column2 Column3 Column4 [root@node1 ~]# 2. .Ah "Major Heading"替換@A HEAD = Major Heading [root@node1 ~]# cat abc Column1 Column2 Column3 Column4 .Ah "Major Heading" [root@node1 ~]# sed '/^\.Ah/{ > s/\.Ah */\ > @A HEAD = / > s/"//g > s/$/\ > /}' abc Column1 Column2 Column3 Column4@A HEAD = Major Heading[root@node1 ~]# [root@node1 ~]# cat abc Column1 Column2 Column3 Column4 .Ah "Major Heading" 1) [root@node1 ~]# sed '/^\.Ah/p' abc Column1 Column2 Column3 Column4 .Ah "Major Heading" .Ah "Major Heading"2) [root@node1 ~]# sed '/^\.Ah/s/\.Ah */@A HEAD = /' abc Column1 Column2 Column3 Column4 @A HEAD = "Major Heading" [root@node1 ~]# 3) [root@node1 ~]# sed '/^\.Ah/{s/\.Ah */@A HEAD = /;s/"//g}' abc Column1 Column2 Column3 Column4 @A HEAD = Major Heading [root@node1 ~]# 4) [root@node1 ~]# sed '/^\.Ah/{s/\.Ah */\n@A HEAD = /;s/"//g}' abc Column1 Column2 Column3 Column4@A HEAD = Major Heading5) [root@node1 ~]# sed '/^\.Ah/{s/\.Ah */\n@A HEAD = /;s/"//g};s/$/\n/' abc Column1 Column2 Column3 Column4@A HEAD = Major Heading[root@node1 ~]#2.5 例4
& 匹配整行內容 1. [root@node1 ~]# cat abc Column1 Column2 Column3 Column4 .Ah "Major Heading" ORA Associates, Inc. [root@node1 ~]# sed "s/ORA/& Associates, Inc./g" abc Column1 Column2 Column3 Column4 .Ah "Major Heading" ORA Associates, Inc. Associates, Inc. [root@node1 ~]# ORA Associates, Inc.替換O' Reilly ORA Associates, Inc. 2. [root@node1 ~]# cat abc Column1 Column2 Column3 Column4 .Ah "Major Heading" ORA Associates, Inc. 1) [root@node1 ~]# sed -r "s/ORA (.*)/O' Reilly &/g" abc Column1 Column2 Column3 Column4 .Ah "Major Heading" O' Reilly ORA Associates, Inc. 2) [root@node1 ~]# sed -r "s/ORA/O' Reilly &/g" abc Column1 Column2 Column3 Column4 .Ah "Major Heading" O' Reilly ORA Associates, Inc. [root@node1 ~]# 3. [root@node1 ~]# cat abc Column1 Column2 Column3 Column4 .Ah "Major Heading" ORA Associates, Inc.1)在ORA后面加1234 s/ORA/只匹配ORA [root@node1 ~]# sed 's/ORA/& 1234/g' abc Column1 Column2 Column3 Column4 .Ah "Major Heading" ORA 1234 Associates, Inc.2)在行尾加1234 s/ORA.*/ 匹配整行 [root@node1 ~]# sed 's/ORA.*/& 1234/g' abc Column1 Column2 Column3 Column4 .Ah "Major Heading" ORA Associates, Inc. 1234[root@node1 ~]# sed 's/ORA.*/&1234/g' abc Column1 Column2 Column3 Column4 .Ah "Major Heading" ORA Associates, Inc.1234 [root@node1 ~]#2.6 例5
\s \\s UNIX換成\s-2UNIX\s0 1. [root@node1 ~]# cat abc Column1 Column2 Column3 Column4 .Ah "Major Heading" ORA Associates, Inc. on the UNIX Operating System. [root@node1 ~]# sed 's/UNIX/\\s-2&\\s0/g' abc Column1 Column2 Column3 Column4 .Ah "Major Heading" ORA Associates, Inc. on the \s-2UNIX\s0 Operating System. [root@node1 ~]#2.7 例6
See Section 1.4 See Section 12.9 將上面兩行() 1. [root@node1 ~]# cat abc Column1 Column2 Column3 Column4 .Ah "Major Heading" ORA Associates, Inc. on the UNIX Operating System. See Section 1.4 See Section 12.9 [root@node1 ~]# sed 's/See Section [1-9][0-9]*\.[1-9][0-9]*/(&)/g' abc Column1 Column2 Column3 Column4 .Ah "Major Heading" ORA Associates, Inc. on the UNIX Operating System. (See Section 1.4) (See Section 12.9)2. [root@node1 ~]# sed -r 's/See Section [1-9][0-9]*.[1-9][0-9]*/(&)/g' abc Column1 Column2 Column3 Column4 .Ah "Major Heading" ORA Associates, Inc. on the UNIX Operating System. (See Section 1.4) (See Section 12.9) [root@node1 ~]# See Section\fb1.4\fp See Section\fb12.9\fp 3. [root@node1 ~]# cat abc Column1 Column2 Column3 Column4 .Ah "Major Heading" ORA Associates, Inc. on the UNIX Operating System. See Section 1.4 See Section 12.9 [root@node1 ~]# sed -r 's/(See Section) ([1-9][0-9]*.[1-9][0-9]*)/\1\\fb\2\\fp/g' abc Column1 Column2 Column3 Column4 .Ah "Major Heading" ORA Associates, Inc. on the UNIX Operating System. See Section\fb1.4\fp See Section\fb12.9\fp [root@node1 ~]# 或 [root@node1 ~]# sed -r 's/(.*) ([1-9][0-9]*.[1-9][0-9]*)/\1\\fb\2\\fp/g' abc Column1 Column2 Column3 Column4 .Ah "Major Heading" ORA Associates, Inc. on the UNIX Operating System. See Section\fb1.4\fp See Section\fb12.9\fp [root@node1 ~]#2.8 例7
first:second one:two將上面兩行調換位置 [root@node1 ~]# cat abc Column1 Column2 Column3 Column4 .Ah "Major Heading" ORA Associates, Inc. on the UNIX Operating System. See Section 1.4 See Section 12.9 first:second one:two [root@node1 ~]# sed -r 's/(.*):(.*)/\2:\1/g' abc Column1 Column2 Column3 Column4 .Ah "Major Heading" ORA Associates, Inc. on the UNIX Operating System. See Section 1.4 See Section 12.9 second:first two:one [root@node1 ~]#2.9 例8
substitution換成substitute[root@node1 ~]# cat abc Column1 Column2 Column3 Column4 .Ah "Major Heading" ORA Associates, Inc. on the UNIX Operating System. See Section 1.4 See Section 12.9 first:second one:two .XX "sed, substitution command" [root@node1 ~]# sed '/^\.XX /s/substitution/substitute/g' abc Column1 Column2 Column3 Column4 .Ah "Major Heading" ORA Associates, Inc. on the UNIX Operating System. See Section 1.4 See Section 12.9 first:second one:two .XX "sed, substitute command" [root@node1 ~]# s/^\.XX \(.*\)$/\/^\\.XX \/s\/\1/\1\// s#^\.XX (.*)$#/^\\.XX /s\1\1#3. 刪除 d
3.1 例1
[root@node1 ~]# cat -n abc 1 Column1 Column2 Column3 Column42 .Ah "Major Heading"3 aAh Major Heading4 zAh Major Heading5 ORA Associates, Inc.6 on the UNIX Operating System.7 See Section 1.48 See Section 12.99 first:second10 one:two11 .XX "sed, substitution command"1.刪除.Ah開頭的 [root@node1 ~]# sed '/.Ah/d' abc Column1 Column2 Column3 Column4 ORA Associates, Inc. on the UNIX Operating System. See Section 1.4 See Section 12.9 first:second one:two .XX "sed, substitution command" [root@node1 ~]# 2.刪除.Ah開頭的 [root@node1 ~]# sed '/\.Ah/d' abc Column1 Column2 Column3 Column4 aAh Major Heading zAh Major Heading ORA Associates, Inc. on the UNIX Operating System. See Section 1.4 See Section 12.9 first:second one:two .XX "sed, substitution command" [root@node1 ~]#4. 追加(a)插入(i)和更改?
append 追加 insert 插入 change 更改語法: [line-address]a\ [line-address]i\ [address]c\ test4.1 例1 追加 a
[root@node1 ~]# cat abc Column1 Column2 Column3 Column4 .Ah "Major Heading" aAh Major Heading zAh Major Heading ORA Associates, Inc. on the UNIX Operating System. See Section 1.4 See Section 12.9 first:second one:two .XX "sed, substitution command"1.在第一行后面加abc [root@node1 ~]# sed '1a abc' abc Column1 Column2 Column3 Column4 abc .Ah "Major Heading" aAh Major Heading zAh Major Heading ORA Associates, Inc. on the UNIX Operating System. See Section 1.4 See Section 12.9 first:second one:two .XX "sed, substitution command" [root@node1 ~]# 2.在第一行后面加abc ddas [root@node1 ~]# sed '1a abc ddas' abc Column1 Column2 Column3 Column4 abc ddas .Ah "Major Heading" aAh Major Heading zAh Major Heading ORA Associates, Inc. on the UNIX Operating System. See Section 1.4 See Section 12.9 first:second one:two .XX "sed, substitution command" [root@node1 ~]# 3.在第一行后面加 abc [root@node1 ~]# sed '1a\ abc' abc Column1 Column2 Column3 Column4abc .Ah "Major Heading" aAh Major Heading zAh Major Heading ORA Associates, Inc. on the UNIX Operating System. See Section 1.4 See Section 12.9 first:second one:two .XX "sed, substitution command" [root@node1 ~]# 4.利用匹配的方式在zAh Major Heading后加 abcdes [root@node1 ~]# sed '/^zAh/a \ abcdes' abc Column1 Column2 Column3 Column4 .Ah "Major Heading" aAh Major Heading zAh Major Headingabcdes ORA Associates, Inc. on the UNIX Operating System. See Section 1.4 See Section 12.9 first:second one:two .XX "sed, substitution command" [root@node1 ~]# 5.在See后加abcdes [root@node1 ~]# sed '/^See/a abcdes' abc Column1 Column2 Column3 Column4 .Ah "Major Heading" aAh Major Heading zAh Major Heading ORA Associates, Inc. on the UNIX Operating System. See Section 1.4 abcdes See Section 12.9 abcdes first:second one:two .XX "sed, substitution command" [root@node1 ~]#[root@node1 ~]# cat abc Column1 Column2 Column3 Column4 .Ah "Major Heading" aAh Major Heading zAh Major Heading ORA Associates, Inc. on the UNIX Operating System. See Section 1.4 See Section 12.9 See Section 13.5 first:second one:two .XX "sed, substitution command"See Section 12.9 See Section 13.5 6. 上面兩行后加abc [root@node1 ~]# sed '/See.*[1-9][0-9]\.[0-9]/a abc' abc Column1 Column2 Column3 Column4 .Ah "Major Heading" aAh Major Heading zAh Major Heading ORA Associates, Inc. on the UNIX Operating System. See Section 1.4 See Section 12.9 abc See Section 13.5 abc first:second one:two .XX "sed, substitution command" [root@node1 ~]#4.2 例2 插入 i
[root@node1 ~]# cat abc Column1 Column2 Column3 Column4 .Ah "Major Heading" aAh Major Heading zAh Major Heading ORA Associates, Inc. on the UNIX Operating System. See Section 1.4 See Section 12.9 See Section 13.5 first:second one:two .XX "sed, substitution command"1.在第一行插入mushuang [root@node1 ~]# sed '1i mushuang' abc mushuang Column1 Column2 Column3 Column4 .Ah "Major Heading" aAh Major Heading zAh Major Heading ORA Associates, Inc. on the UNIX Operating System. See Section 1.4 See Section 12.9 See Section 13.5 first:second one:two .XX "sed, substitution command" [root@node1 ~]# 或 [root@node1 ~]# sed '/^\.Ah/i abc' abc Column1 Column2 Column3 Column4 abc .Ah "Major Heading" aAh Major Heading zAh Major Heading ORA Associates, Inc. on the UNIX Operating System. See Section 1.4 See Section 12.9 See Section 13.5 first:second one:two .XX "sed, substitution command" [root@node1 ~]#4.3 例3 更改 c
[root@node1 ~]# cat abc Column1 Column2 Column3 Column4 .Ah "Major Heading" aAh Major Heading zAh Major Heading ORA Associates, Inc. on the UNIX Operating System. See Section 1.4 See Section 12.9 See Section 13.5 first:second one:two .XX "sed, substitution command"1.將ORA Associates, Inc.改成papap [root@node1 ~]# sed '/ORA/c papap' abc Column1 Column2 Column3 Column4 .Ah "Major Heading" aAh Major Heading zAh Major Heading papap on the UNIX Operating System. See Section 1.4 See Section 12.9 See Section 13.5 first:second one:two .XX "sed, substitution command" [root@node1 ~]#4.4 例4 i
[root@node1 ~]# cat abc Column1 Column2 Column3 Column4 .Ah "Major Heading" aAh Major Heading zAh Major Heading ORA Associates, Inc. on the UNIX Operating System. See Section 1.4 See Section 12.9 See Section 13.5 first:second one:two .XX "sed, substitution command" Larry' s Address1.Larry' s Address后加兩行 [root@node1 ~]# sed '/Larry/i 4700 Cross Court\ > French Lick, IN > ' abc Column1 Column2 Column3 Column4 .Ah "Major Heading" aAh Major Heading zAh Major Heading ORA Associates, Inc. on the UNIX Operating System. See Section 1.4 See Section 12.9 See Section 13.5 first:second one:two .XX "sed, substitution command" 4700 Cross Court French Lick, IN Larry' s Address [root@node1 ~]#4.5 例5
[root@node1 ~]# cat abc Column1 Column2 Column3 Column4 .Ah "Major Heading" aAh Major Heading zAh Major Heading ORA Associates, Inc. on the UNIX Operating System. See Section 1.4 See Section 12.9 See Section 13.5 first:second one:two .XX "sed, substitution command" Larry' s Address [root@node1 ~]# 1. .Ah開頭,on結尾替換 abcd [root@node1 ~]# sed '/^\.Ah/,/on /c abcd' abc Column1 Column2 Column3 Column4 abcd See Section 1.4 See Section 12.9 See Section 13.5 first:second one:two .XX "sed, substitution command" Larry' s Address [root@node1 ~]# 2.全部內容替換成abcde [root@node1 ~]# sed '/^Column/,$c abcde' abc abcde [root@node1 ~]# 或者 [root@node1 ~]# sed '1,$c abcde' abc abcde [root@node1 ~]#5. 轉換 y
[root@node1 ~]# cat abc Column1 Column2 Column3 Column4 [root@node1 ~]# cat abc Column1 Column2 Column3 Column4 .Ah "Major Heading" aAh Major Heading zAh Major Heading ORA Associates, Inc. on the UNIX Operating System. See Section 1.4 See Section 12.9 See Section 13.5 first:second one:two .XX "sed, substitution command" Larry' s Address1.將第三行中的ajg轉換成978 [root@node1 ~]# sed '3y/ajg/978/' abc Column1 Column2 Column3 Column4 .Ah "Major Heading" 9Ah M97or He9din8 zAh Major Heading ORA Associates, Inc. on the UNIX Operating System. See Section 1.4 See Section 12.9 See Section 13.5 first:second one:two .XX "sed, substitution command" Larry' s Address [root@node1 ~]#6. 打印 p
6.1 例
[root@node1 ~]# cat bca .Ah "Comment" .Ah "Substitution" .Ah "Delete" .Ah "Append, Insert and Change" .Ah "List"1. [root@node1 ~]# sed '/^\.Ah/{p}' bca .Ah "Comment" .Ah "Comment" .Ah "Substitution" .Ah "Substitution" .Ah "Delete" .Ah "Delete" .Ah "Append, Insert and Change" .Ah "Append, Insert and Change" .Ah "List" .Ah "List" [root@node1 ~]# 2. [root@node1 ~]# sed '/^\.Ah/{p;s/"//g;s/^\.Ah //}' bca .Ah "Comment" Comment .Ah "Substitution" Substitution .Ah "Delete" Delete .Ah "Append, Insert and Change" Append, Insert and Change .Ah "List" List [root@node1 ~]#7. 打印行號
7.1 例
[root@node1 ~]# cat bca .Ah "Comment" .Ah "Substitution" .Ah "Delete" .Ah "Append, Insert and Change" .Ah "List"將第三行行號及其內容打印出來 [root@node1 ~]# sed -n '/Delete/{=;p}' bca 3 .Ah "Delete" [root@node1 ~]#8. 下一步 (next)n
8.1 例
[root@node1 ~]# cat bca .Ah "Comment" .Ah "Substitution" .Ah "Delete" .Ah "Append, Insert and Change" .Ah "List"1. .Ah "Delete"打印下一行內容 [root@node1 ~]# sed -n '/Delete/p' bca .Ah "Delete" [root@node1 ~]# sed -n '/Delete/n;p' bca .Ah "Comment" .Ah "Substitution" .Ah "Append, Insert and Change" .Ah "List" [root@node1 ~]# 2. 將.Ah "Append, Insert and Change"的下一行List加個() [root@node1 ~]# cat bca .Ah "Comment" .Ah "Substitution" .Ah "Delete" .Ah "Append, Insert and Change" .Ah "List" [root@node1 ~]# sed -r '/Append/{n;s/\.Ah (.*)/\.Ah (\1)/g}' bca .Ah "Comment" .Ah "Substitution" .Ah "Delete" .Ah "Append, Insert and Change" .Ah ("List") [root@node1 ~]# 3. [root@node1 ~]# cat bca .Ah "Comment" Append 123 .Ah "Substitution" Append 456 .Ah "Delete" .Ah "Append, Insert and Change" .Ah "List" Append 567[root@node1 ~]# sed -r '/Append/{n;s/\.Ah (.*)/\.Ah (\1)/g}' bca .Ah "Comment" Append 123 .Ah ("Substitution") Append 456 .Ah ("Delete") .Ah "Append, Insert and Change" .Ah ("List") Append 567 [root@node1 ~]# [root@node1 ~]# cat bca .Ah "Comment" Append 123.Ah "Substitution" Append 456 .Ah "Delete" .Ah "Append, Insert and Change".Ah "List" Append 5674. 取有Append行,中下一行有空格刪除 [root@node1 ~]# sed '/Append/{n;/^$/d}' bca .Ah "Comment" Append 123 .Ah "Substitution" Append 456 .Ah "Delete" .Ah "Append, Insert and Change" .Ah "List" Append 567 [root@node1 ~]# [root@node1 ~]# sed -n '/Append/{n;/^$/d};p' bca .Ah "Comment" .Ah "Substitution" .Ah "Delete" .Ah "List" [root@node1 ~]#9. 讀(r)和寫(w)
9.1 例 w
[root@node1 ~]# cat bca .Ah "Comment" Append 123.Ah "Substitution" Append 456 .Ah "Delete" .Ah "Append, Insert and Change".Ah "List" Append 567[root@node1 ~]# sed -n '/Append/{n;/^$/d};p' bca .Ah "Comment" .Ah "Substitution" .Ah "Delete" .Ah "List" [root@node1 ~]# [root@node1 ~]# cat 555 .Ah "Comment" .Ah "Substitution" .Ah "Delete" .Ah "List" [root@node1 ~]#二. 高級sed命令
- 處理多行模式空間(N,D,P)
- 采用保持空間來保存模式空間的內容并使它可用于后續命令(H,h,G,g,x)
- 編寫使用分支和條件指令的腳本來更改控制流(:,b,t)
1. 多行模式空間
-
多行內容當成整體來處理
-
N:將下一行追加到模式空間后
-
D:刪除多行模式空間的第一行
-
P:打印
2. 追加下一行 N
2.1 例
第一個示例是,我們假設想要將“Owner and 0perator Guide”換成“lnstallation Guide”,但是我們發現它出現在文件中的兩行上,“Operator”和“Guide”被分開了。 Owner and Operator Guide 換成 installation Guide 空格用\nroot@node6 ~]# cat abc Consult Section 3.1 in the Owner and Operator Guide for a description of the tape drives available on your system.例1 [root@node6 ~]# sed -n '/Operator$/{N;p}' abc Consult Section 3.1 in the Owner and Operator Guide for a description of the tape drives [root@node6 ~]# sed -n '/Operator$/{N;s/Owner and Operator\nGuide/installation Guide/g;p}' abc Consult Section 3.1 in the installation Guide for a description of the tape drives [root@node6 ~]# sed '/Operator$/{N;s/Owner and Operator\nGuide/installation Guide/g}' abc Consult Section 3.1 in the installation Guide for a description of the tape drives available on your system. 或者 [root@node6 ~]# sed '/Operator/{ > N > s/Owner and Operator\nGuide/installation Guide/g > }' abc Consult Section 3.1 in the installation Guide for a description of the tape drives available on your system. [root@node6 ~]# 例2 將3.1改成3.2 Owner and Operator Guide 換成 installation Guide [root@node6 ~]# sed '/3.1/{N;s/3.1/3.2/g;s/Owner and Operator\nGuide/installation Guide/g}' abc Consult Section 3.2 in the installation Guide for a description of the tape drives available on your system. [root@node6 ~]#2.2 例
[root@node6 ~]# cat bcd Consult Section 3.1 in the Owner and Operator Guide for a description of the tape drives available on your system. Look in the Owner and Operator Guide shipped with your system. Two manuals are provided including the Owner and Operator Guide and the User Guide. The Owner and Operator Guide is shipped with your system.1. 1) [root@node6 ~]# sed 's/Owner and Operator Guide/Installation Guide/g' bcd Consult Section 3.1 in the Owner and Operator Guide for a description of the tape drives available on your system. Look in the Installation Guide shipped with your system. Two manuals are provided including the Owner and Operator Guide and the User Guide. The Owner and 0perator Guide is shipped with your system.2) [root@node6 ~]# sed -n 's/Owner and Operator Guide/Installation Guide/g;/Owner/p' bcd Consult Section 3.1 in the Owner and Operator Two manuals are provided including the Owner and The Owner and 0perator Guide is shipped with your system.3) [root@node6 ~]# sed -n 's/Owner and Operator Guide/Installation Guide/g;/Owner/{N;p}' bcd Consult Section 3.1 in the Owner and Operator Guide for a description of the tape drives Two manuals are provided including the Owner and Operator Guide and the User Guide.4) [root@node6 ~]# sed -n 's/Owner and Operator Guide/Installation Guide/g;/Owner/{N;s/ *\n/ /;p}' bcd Consult Section 3.1 in the Owner and Operator Guide for a description of the tape drives Two manuals are provided including the Owner and Operator Guide and the User Guide. [root@node6 ~]# 5) [root@node6 ~]# sed 's/Owner and Operator Guide/Installation Guide/g;/Owner/{N;s/ *\n/ /;s/Owner and Operator Guide */Installation Guide\n/g}' bcdConsult Section 3.1 in the Installation Guide for a description of the tape drives available on your system. Look in the Installation Guide shipped with your system. Two manuals are provided including the Installation Guide and the User Guide. The Installation Guide is shipped with your system.2. [root@node6 ~]# sed 's/Owner and Operator Guide/Installation Guide/g > /Owner/{ > N > s/ *\n/ / > s/Owner and Operator Guide */Installation Guide\ > /g > }' bcd Consult Section 3.1 in the Installation Guide for a description of the tape drives available on your system. Look in the Installation Guide shipped with your system. Two manuals are provided including the Installation Guide and the User Guide. The Installation Guide is shipped with your system. [root@node6 ~]# [root@node6 ~]# cat abc s/Owner and Operator Guide/Installation Guide/g /Owner/{ N s/ *\n/ / s/Owner and Operator Guide */Installation Guide\ /g } [root@node6 ~]# sed -f abc bcd Consult Section 3.1 in the Installation Guide for a description of the tape drives available on your system. Look in the Installation Guide shipped with your system. Two manuals are provided including the Installation Guide and the User Guide. The Installation Guide is shipped with your system.- $!N
- 它排除了對最后一行執行Next命令。在以上的腳本中,通過匹配最后一行上的“Owner and 0perator Guide”,我們避免了匹配“Owner”和應用N命令。然而,如果單詞“Owner”出現在最后一行,我們會遇到同樣的問題,除非使用“$!N”語法
3. 多行刪除 D
3.1 例
[root@node6 ~]# cat abc This line is followed by 1 blank line.This line is followed by 2 blank lines.This line is followed by 3 blank lines.This line is followed by 4 blank lines.This is the end. [root@node6 ~]# 1) [root@node6 ~]# sed -n '/^$/{N;p}' abc This line is followed by 2 blank lines.This line is followed by 4 blank lines.[root@node6 ~]# 2) [root@node6 ~]# sed -n '/^$/{N;/^\n$/d;p}' abc This line is followed by 2 blank lines.This line is followed by 4 blank lines. [root@node6 ~]#[root@node6 ~]# sed '/^$/{N;/^\n$/d}' abc This line is followed by 1 blank line.This line is followed by 2 blank lines. This line is followed by 3 blank lines.This line is followed by 4 blank lines. This is the end. [root@node6 ~]# 當有偶數個空行時,所有的空行都會被刪除。 僅當有奇數個空行時,有一行被保留下來。 這是因為刪除命令清除的是整個模式空間。 一旦遇到第一個空行,就讀入下一行,并且兩行都被刪除。 如果遇到第三個空行,并且下一行不為空,那么刪除命令就不會被執行,因此空行被輸出。 如果使用多行Delete命令(是D不是d),就能得到我們想要的結果4. 多行打印 P
4.1 例
[root@node6 ~]# cat abc Here are examples of the UNIX System. Where UNIX System appears, it should be the UNIX Operating System. [root@node6 ~]# 1) [root@node6 ~]# sed -n '/UNIX$/p' abc Here are examples of the UNIX System. Where UNIX System appears, it should be the UNIX [root@node6 ~]# 2) [root@node6 ~]# sed -n '/UNIX$/{N;p}' abc Here are examples of the UNIX System. Where UNIX System appears, it should be the UNIX Operating System.3) [root@node6 ~]# sed -n '/UNIX$/{N;/\nSystem/{p}}' abc Here are examples of the UNIX System. Where UNIX [root@node6 ~]# 4) [root@node6 ~]# sed -n '/UNIX$/{N;/\nSystem/{s// Operating &/g;p}}' abc Here are examples of the UNIX Operating System. Where UNIX [root@node6 ~]# 5) [root@node6 ~]# sed -n '/UNIX$/{N;/\nSystem/{s// Operating &/g;P;D;p}}' abc Here are examples of the UNIX Operating System. Where UNIX Operating [root@node6 ~]# sed '/UNIX$/{N;/\nSystem/{s// Operating &/g;P;D}}' abc Here are examples of the UNIX Operating System. Where UNIX Operating System appears, it should be the UNIX Operating System. [root@node6 ~]#5. 包含那一行
- 保持空間
| Hold | h(復制)或H (追加) 上傳 | 將模式空間的內容復制或追加到保持空間 |
| Get | g或G下載 | 將保持空間的內容復制或追加到模式空間 |
| Exchange | x | 交換保持空間和模式空間的內容 |
5.1 例
[root@node6 ~]# cat abc 1 2 11 22 111 222 [root@node6 ~]# 匹配1將內容放入保持空間,刪除,在將匹配2的內容追加模式空間 [root@node6 ~]# sed '/1/{h;d};/2/G' abc 2 1 22 11 222 111 [root@node6 ~]#三. 高級的流控制命令
標簽 :mylabel//:begin b mylabel 分支 b測試 t [address]t[label]總結
- 上一篇: Office 365 利用并行工作流构建
- 下一篇: 一人之下被动进阶鸿蒙,一人之下:乱金柝是