日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

sed应用

發布時間:2024/3/26 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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. 刪除

  • 刪除文件中的內容
語法 [address]command [line-address]command1.刪除以pwpolicy開頭的內容 [root@node1 ~]# cat -n anac.cfg 1 %anaconda2 pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty3 pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok4 pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty5 %end [root@node1 ~]# sed '/^pwpolicy/d' anac.cfg %anaconda %end2. 刪除第四行 [root@node1 ~]# sed '4d' anac.cfg %anaconda pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok %end [root@node1 ~]#

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\ test
4.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 ~]# 例23.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. 包含那一行

  • 保持空間
命令縮寫功能
Holdh(復制)或H (追加) 上傳將模式空間的內容復制或追加到保持空間
Getg或G下載將保持空間的內容復制或追加到模式空間
Exchangex交換保持空間和模式空間的內容
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]

總結

以上是生活随笔為你收集整理的sed应用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 成人玩具h视频 | 91成人综合 | 三级在线视频 | 国产激情综合五月久久 | 亚洲精品久久久久久动漫器材一区 | 欧美天天性 | 91亚洲精品久久久久久久久久久久 | 亚洲欧美日韩国产一区 | 日本电影一区二区三区 | 久久禁 | 国产普通话bbwbbwbbw | 欧美色视频在线 | 国产夫妻视频 | 黑丝扣逼| www99热| 在线观看av资源 | 性史性dvd影片农村毛片 | 日韩第四页 | 精品无码人妻少妇久久久久久 | 久久精品99国产精 | 日韩一级片免费在线观看 | 国产精品zjzjzj在线观看 | 免费激情网 | 一级在线免费视频 | 波多野结衣一区在线 | www.av黄色 | 一级片免费播放 | 老女人一区 | 国产最新视频在线 | 日本一本高清 | 玖草在线观看 | 在线看的av网站 | 国产精品v亚洲精品v日韩精品 | 三级视频网 | 中文字幕有码在线观看 | 日韩精品一区二区三区免费视频 | 一本一道精品欧美中文字幕 | 深田咏美中文字幕 | 特级性生活片 | 欧美成人三区 | 美女福利一区 | 欧美日韩你懂的 | 午夜啪视频 | 亚洲二三区 | 午夜怡红院 | 999精品在线观看 | 日韩在线观看免费全 | 国产区一二三 | 亚洲综合第一 | 国产淫片av片久久久久久 | gai免费观看网站外网 | 日日夜夜艹| 亚洲九九视频 | 日本一区二区视频免费 | 成人深夜福利 | 人妻91麻豆一区二区三区 | 免费网站黄色 | 日韩美女国产精品 | 久久成人视屏 | 亚洲成人精品久久 | 蜜桃视频在线观看一区 | www夜插内射视频网站 | 亚洲免费黄色网址 | 99热免费观看 | 成人午夜在线免费观看 | 亚洲精品在线电影 | 亚洲国产精华液网站w | 在线免费看av | 欧美在线免费观看视频 | 伊人青青草原 | 免费视频福利 | 久久久久久久性 | 欧美国产日韩一区 | 成年人黄色录像 | 性爱一级视频 | 国产精品无码免费专区午夜 | 中文字幕91视频 | 欧美性生活精品 | 美女张开腿让男人操 | 国产在线视频一区二区三区 | 亚洲片国产一区一级在线观看 | 天天爱天天爽 | 国语对白做受xxxxx在线中国 | 亚洲九色 | 少妇高潮av久久久久久 | 国产亚洲久久 | 狠狠操亚洲 | 国产乱子伦精品 | 污视频网站免费看 | 精品一区欧美 | 不卡的毛片 | 国产福利视频在线观看 | 国产农村乱对白刺激视频 | 亚洲精品www | 日本欧美一区 | 蜜桃av噜噜一区二区三区 | 日韩极品在线 | 男人日女人的网站 | 国产三级全黄裸体 |