溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

sed使用小結(jié)

發(fā)布時間:2020-08-04 03:46:41 來源:網(wǎng)絡(luò) 閱讀:829 作者:470499989 欄目:系統(tǒng)運(yùn)維

對于行處理的工具,sed絕對是首選的。
sed處理文本過程如下:

sed使用小結(jié) 

  1. sed一次處理一行內(nèi)容,處理時,先讀入一行,去掉尾部換行符,存入pattern space,執(zhí)行編輯命令.  
  2. 處理完畢,除非加了-n參數(shù),把現(xiàn)在的pattern space打印出來,在后邊打印曾去掉的換行符.  
  3. 把pattern space內(nèi)容給hold space,把pattern space置空,接著讀下一行,處理下一行. 

這讓人感覺很抽象,sedsed的工具就是為了展現(xiàn)sed處理過程的。sedsed安裝和使用都很簡單,為了更好理解sed,裝上sedsed工具是很有必要的。

  1. wget http://aurelio.net/sedsed/sedsed-1.0   
  2. touch /usr/local/bin/sedsed   
  3. cat sedsed-1.0 >> /usr/local/bin/sedsed   
  4. chmod 755 /usr/local/bin/sedsed 

用法如下

  1. usage: sedsed OPTION [-e sedscript] [-f sedscriptfile] [inputfile]  
  2. OPTIONS:  
  3.  
  4.      -f, --file          add file contents to the commands to be parsed  
  5.      -e, --expression    add the script to the commands to be parsed  
  6.      -n, --quiet         suppress automatic printing of pattern space  
  7.          --silent        alias to --quiet  
  8.  
  9.      -d, --debug         debug the sed script  
  10.          --hide          hide some debug info (options: PATT,HOLD,COMM)  
  11.          --color         shows debug output in colors (default: ON)  
  12.          --nocolor       no colors on debug output  
  13.          --dump-debug    dumps to screen the debugged sed script  
  14.  
  15.          --emu           emulates GNU sed (INCOMPLETE)  
  16.          --emudebug      emulates GNU sed debugging the sed script (INCOMPLETE)  
  17.  
  18.      -i, --indent        script beautifier, prints indented and  
  19.                          one-command-per-line output do STDOUT  
  20.          --prefix        indent prefix string (default: 4 spaces)  
  21.  
  22.      -t, --tokenize      script tokenizer, prints extensive  
  23.                          command by command information  
  24.      -H, --htmlize       converts sed script to a colorful HTML page  
  25.        
  26. -f選項和sed的-f選項一樣  
  27. -d打開debug,其中--hide表示隱藏指定的內(nèi)容;如--hide=hold表示隱藏的保留空間緩沖區(qū)的內(nèi)容  
  28. -i 的--indent 格式化復(fù)雜的sed腳本變成更加人性化的腳本 

先看個簡單的例子:
[root@localhost ~]# cat 1.txt
linux
centos
redhat
linux
ubuntu
fedora
把linux換成debian,下面給出三種處理方式:sed處理;sedsed -d 和sedsed -d --hide

  1. [root@localhost ~]# sed 's/linux/debian/g' 1.txt   
  2. debian  
  3. centos  
  4. redhat  
  5. debian  
  6. ubuntu  
  7. fedora  
  8. [root@localhost ~]# sedsed -d  's/linux/debian/g' 1.txt   
  9. PATT:linux$  
  10. HOLD:$  
  11. COMM:s/linux/debian/g  
  12. PATT:debian$  
  13. HOLD:$  
  14. debian  
  15. PATT:centos$  
  16. HOLD:$  
  17. COMM:s/linux/debian/g  
  18. PATT:centos$  
  19. HOLD:$  
  20. centos  
  21. PATT:redhat$  
  22. HOLD:$  
  23. COMM:s/linux/debian/g  
  24. PATT:redhat$  
  25. HOLD:$  
  26. redhat  
  27. PATT:linux$  
  28. HOLD:$  
  29. COMM:s/linux/debian/g  
  30. PATT:debian$  
  31. HOLD:$  
  32. debian  
  33. PATT:ubuntu$  
  34. HOLD:$  
  35. COMM:s/linux/debian/g  
  36. PATT:ubuntu$  
  37. HOLD:$  
  38. ubuntu  
  39. PATT:fedora$  
  40. HOLD:$  
  41. COMM:s/linux/debian/g  
  42. PATT:fedora$  
  43. HOLD:$  
  44. fedora  
  45. [root@localhost ~]# sedsed -d  --hide=hold 's/linux/debian/g' 1.txt   
  46. PATT:linux$  
  47. COMM:s/linux/debian/g  
  48. PATT:debian$  
  49. debian  
  50. PATT:centos$  
  51. COMM:s/linux/debian/g  
  52. PATT:centos$  
  53. centos  
  54. PATT:redhat$  
  55. COMM:s/linux/debian/g  
  56. PATT:redhat$  
  57. redhat  
  58. PATT:linux$  
  59. COMM:s/linux/debian/g  
  60. PATT:debian$  
  61. debian  
  62. PATT:ubuntu$  
  63. COMM:s/linux/debian/g  
  64. PATT:ubuntu$  
  65. ubuntu  
  66. PATT:fedora$  
  67. COMM:s/linux/debian/g  
  68. PATT:fedora$  
  69. fedora 

其中:
PATT:sedsed輸出顯示模式空間緩沖區(qū)的內(nèi)容
COMM:顯示正在執(zhí)行的命令
HOLD:顯示hold sapce緩沖區(qū)的內(nèi)容

sed的增刪改查
sed用于刪除操作的是:d和D
命令"d"作用是刪除模式空間的內(nèi)容,然后讀入新的行,sed腳本從頭再次開始執(zhí)行.
命令"D"的不同之處在于它刪除的是直到第一個內(nèi)嵌換行符為止的模式空間的一部分,但是不會讀入新的行,腳本將回到開始對剩下內(nèi)容進(jìn)行處理. 
看個例子

  1. [root@localhost ~]# cat 2.txt   
  2. This line is followed by 1 blank line.  
  3.  
  4. This line is followed by 2 blank line.  
  5.  
  6.  
  7. This line is followed by 3 blank line.  
  8.  
  9.  
  10.  
  11. This line is followed by 4 blank line.  
  12.  
  13.  
  14.  
  15.  
  16. This is the end. 
  1. [root@localhost ~]# sed '/^$/{N;/^\n$/D}' 2.txt   
  2. This line is followed by 1 blank line.  
  3.  
  4. This line is followed by 2 blank line.  
  5.  
  6. This line is followed by 3 blank line.  
  7.  
  8. This line is followed by 4 blank line.  
  9.  
  10. This is the end.  
  11. [root@localhost ~]# sed '/^$/{N;/^\n$/d}' 2.txt   
  12. This line is followed by 1 blank line.  
  13.  
  14. This line is followed by 2 blank line.  
  15. This line is followed by 3 blank line.  
  16.  
  17. This line is followed by 4 blank line.  
  18. This is the end. 

用sedsed 打開debug看看執(zhí)行過程 //后面的內(nèi)容是我注釋的

  1. [root@localhost ~]# sedsed -d  '/^$/{N;/^\n$/D}' 2.txt   
  2. PATT:This line is followed by 1 blank line.$  //pattern空間讀入第一行內(nèi)容  
  3. HOLD:$                                        //hold空間開始為空  
  4. COMM:/^$/ {            //正在執(zhí)行的命令,判斷是否為空行,很明顯不是,所有不執(zhí)行后面的命令,執(zhí)行的結(jié)果送往屏幕并把結(jié)果給hold space.  
  5. PATT:This line is followed by 1 blank line.$  
  6. HOLD:$  
  7. This line is followed by 1 blank line.  
  8. PATT:$                //pattern空間讀入第二行  
  9. HOLD:$                //hold空間開始還是為空  
  10. COMM:/^$/ {           //正在執(zhí)行的命令,判斷是否為空行,很明顯是,所有執(zhí)行后面的命令  
  11. COMM:N                //執(zhí)行N讀取下一行進(jìn)入pattern空間  
  12. PATT:\nThis line is followed by 2 blank line.$  
  13. HOLD:$               //此時hold空間還是為空  
  14. COMM:/^\n$/ D        //對pattern空間繼續(xù)執(zhí)行后面的命令:如果是空行,執(zhí)行D命令,很明顯不是,所有不執(zhí)行。  
  15. PATT:\nThis line is followed by 2 blank line.$  //pattern空間內(nèi)容  
  16. HOLD:$              //hold空間內(nèi)容,任然為空。  
  17. COMM:}  
  18. PATT:\nThis line is followed by 2 blank line.$  
  19. HOLD:$  
  20.  
  21. This line is followed by 2 blank line.  //由于沒有滿足執(zhí)行條件,繼續(xù)讀取下一行。  
  22. PATT:$           //空行,滿足執(zhí)行條件,執(zhí)行命令  
  23. HOLD:$  
  24. COMM:/^$/ {      
  25. COMM:N  
  26. PATT:\n$  
  27. HOLD:$  
  28. COMM:/^\n$/ D    //滿足執(zhí)行D命令條件,刪除一空行(現(xiàn)在有兩空行)  
  29. PATT:$  
  30. HOLD:$  
  31. COMM:/^$/ {  
  32. COMM:N  
  33. PATT:\nThis line is followed by 3 blank line.$  
  34. HOLD:$  
  35. COMM:/^\n$/ D  
  36. PATT:\nThis line is followed by 3 blank line.$  
  37. HOLD:$  
  38. COMM:}  
  39. PATT:\nThis line is followed by 3 blank line.$  
  40. HOLD:$  
  41.  
  42. This line is followed by 3 blank line.  
  43. PATT:$         \\空行,滿足命令執(zhí)行條件,執(zhí)行命令,讀取下一行.  
  44. HOLD:$  
  45. COMM:/^$/ {  
  46. COMM:N  
  47. PATT:\n$  
  48. HOLD:$  
  49. COMM:/^\n$/ D  \\滿足執(zhí)行D命令,刪除一空行.  
  50. PATT:$         \\空行,滿足命令執(zhí)行條件,執(zhí)行命令,讀取下一行.  
  51. HOLD:$  
  52. COMM:/^$/ {  
  53. COMM:N  
  54. PATT:\n$  
  55. HOLD:$  
  56. COMM:/^\n$/ D  \\ \\滿足執(zhí)行D命令,刪除一空行.  
  57. PATT:$  
  58. HOLD:$  
  59. COMM:/^$/ {  
  60. COMM:N  
  61. PATT:\nThis line is followed by 4 blank line.$  
  62. HOLD:$  
  63. COMM:/^\n$/ D  
  64. PATT:\nThis line is followed by 4 blank line.$  
  65. HOLD:$  
  66. COMM:}  
  67. PATT:\nThis line is followed by 4 blank line.$  
  68. HOLD:$  
  69.  
  70. This line is followed by 4 blank line.  
  71. PATT:$  
  72. HOLD:$  
  73. COMM:/^$/ {  
  74. COMM:N  
  75. PATT:\n$  
  76. HOLD:$  
  77. COMM:/^\n$/ D  
  78. PATT:$  
  79. HOLD:$  
  80. COMM:/^$/ {  
  81. COMM:N  
  82. PATT:\n$  
  83. HOLD:$  
  84. COMM:/^\n$/ D  
  85. PATT:$  
  86. HOLD:$  
  87. COMM:/^$/ {  
  88. COMM:N  
  89. PATT:\n$  
  90. HOLD:$  
  91. COMM:/^\n$/ D  
  92. PATT:$  
  93. HOLD:$  
  94. COMM:/^$/ {  
  95. COMM:N  
  96. PATT:\nThis is the end.$  
  97. HOLD:$  
  98. COMM:/^\n$/ D  
  99. PATT:\nThis is the end.$  
  100. HOLD:$  
  101. COMM:}  
  102. PATT:\nThis is the end.$  
  103. HOLD:$  
  104.  
  105. This is the end.  
  106.  
  107. [root@localhost ~]# sedsed -d  '/^$/{N;/^\n$/d}' 2.txt   
  108. PATT:This line is followed by 1 blank line.$  
  109. HOLD:$  
  110. COMM:/^$/ {  
  111. PATT:This line is followed by 1 blank line.$  
  112. HOLD:$  
  113. This line is followed by 1 blank line.  
  114. PATT:$  
  115. HOLD:$  
  116. COMM:/^$/ {  
  117. COMM:N  
  118. PATT:\nThis line is followed by 2 blank line.$  
  119. HOLD:$  
  120. COMM:/^\n$/ d  
  121. PATT:\nThis line is followed by 2 blank line.$  
  122. HOLD:$  
  123. COMM:}  
  124. PATT:\nThis line is followed by 2 blank line.$  
  125. HOLD:$  
  126.  
  127. This line is followed by 2 blank line.  
  128. PATT:$          \\空行,滿足命令執(zhí)行條件,執(zhí)行命令,讀取下一行.  
  129. HOLD:$  
  130. COMM:/^$/ {  
  131. COMM:N  
  132. PATT:\n$  
  133. HOLD:$  
  134. COMM:/^\n$/ d   \\滿足執(zhí)行d命令條件,此時模式空間的兩個空行都被刪除.  
  135. PATT:This line is followed by 3 blank line.$  
  136. HOLD:$  
  137. COMM:/^$/ {  
  138. PATT:This line is followed by 3 blank line.$  
  139. HOLD:$  
  140. This line is followed by 3 blank line.  
  141. PATT:$  
  142. HOLD:$  
  143. COMM:/^$/ {  
  144. COMM:N  
  145. PATT:\n$  
  146. HOLD:$  
  147. COMM:/^\n$/ d  
  148. PATT:$  
  149. HOLD:$  
  150. COMM:/^$/ {  
  151. COMM:N  
  152. PATT:\nThis line is followed by 4 blank line.$  
  153. HOLD:$  
  154. COMM:/^\n$/ d  
  155. PATT:\nThis line is followed by 4 blank line.$  
  156. HOLD:$  
  157. COMM:}  
  158. PATT:\nThis line is followed by 4 blank line.$  
  159. HOLD:$  
  160.  
  161. This line is followed by 4 blank line.  
  162. PATT:$  
  163. HOLD:$  
  164. COMM:/^$/ {  
  165. COMM:N  
  166. PATT:\n$  
  167. HOLD:$  
  168. COMM:/^\n$/ d  
  169. PATT:$  
  170. HOLD:$  
  171. COMM:/^$/ {  
  172. COMM:N  
  173. PATT:\n$  
  174. HOLD:$  
  175. COMM:/^\n$/ d  
  176. PATT:This is the end.$  
  177. HOLD:$  
  178. COMM:/^$/ {  
  179. PATT:This is the end.$  
  180. HOLD:$  
  181. This is the end. 

一般情況下,都用d來刪除,如:

  1. 刪除空行:  
  2. sed '/^\s*$/d' filename  
  3. sed '/^[[:space:]]*$/d' filename   
  4. 用行標(biāo)示號來刪除  
  5. sed  'n1d'  filename   刪除第n1行  
  6. sed 'n1,n2d' filename  刪除第n1行到n2行間內(nèi)容(n1<=n2)  
  7. sed  '5,$d' filename   刪除第5行以后內(nèi)容內(nèi)容  
  8. 用特殊匹配來刪除,格式如下  
  9. sed '/regular_pattern/d' filename 

sed基于行的插入和替換操作是由:a\,i\,c\來完成

  1. a\命令是追加命令,追加將添加新文本到文件中當(dāng)前行(即讀入模式緩沖區(qū)中的行)的后面.所追加的文本行位于sed命令的下方另起一行.如果要追加的內(nèi)容超過一行,則每一行都必須以反斜線結(jié)束,最后一行除外.最后一行將以引號和文件名結(jié)束.  
  2. i\ 命令是在當(dāng)前行的前面插入新的文本.  
  3. c\ 用新的文本改變本行的文本 

a\和i\用法比較簡單,看一個c\的例子

  1. [root@localhost ~]# cat 1.txt   
  2. linux  
  3. centos  
  4. redhat  
  5. linux  
  6. ubuntu linux  
  7. fedora  
  8. [root@localhost ~]# sedsed -d '/linux/c\unix' 1.txt   
  9. PATT:linux$  
  10. HOLD:$  
  11. COMM:/linux/ c\\N\unix  
  12. unix  
  13. PATT:centos$  
  14. HOLD:$  
  15. COMM:/linux/ c\\N\unix  
  16. PATT:centos$  
  17. HOLD:$  
  18. centos  
  19. PATT:redhat$  
  20. HOLD:$  
  21. COMM:/linux/ c\\N\unix  
  22. PATT:redhat$  
  23. HOLD:$  
  24. redhat  
  25. PATT:linux$  
  26. HOLD:$  
  27. COMM:/linux/ c\\N\unix  
  28. unix  
  29. PATT:ubuntu linux$  
  30. HOLD:$  
  31. COMM:/linux/ c\\N\unix  
  32. unix  
  33. PATT:fedora$  
  34. HOLD:$  
  35. COMM:/linux/ c\\N\unix  
  36. PATT:fedora$  
  37. HOLD:$  
  38. fedora 

sed的替換操作是用的最多的,用sed處理內(nèi)容,最重要的就是要找特征數(shù)據(jù)(字符),并以此為基礎(chǔ)處理內(nèi)容而不會處理的內(nèi)容過多或者處理不完全.
如最開始的舉的把linux換成debian的例子.

sed 的高級運(yùn)用
打印匹配行號

  1. [root@localhost ~]# cat 1.txt   
  2. linux  
  3. centos  
  4. redhat  
  5. linux  
  6. ubuntu linux  
  7. fedora  
  8. [root@localhost ~]# sed -n '/^linux/=' 1.txt  
  9. 1  
  10. 4  
  11. [root@localhost ~]# sed -n '/^linux/{=;p}' 1.txt  
  12. 1  
  13. linux  
  14. 4  
  15. linux  
  16. [root@localhost ~]# sedsed -d  -n  '/^linux/{=;p}' 1.txt  
  17. PATT:linux$  
  18. HOLD:$  
  19. COMM:/^linux/ {  
  20. COMM:=  
  21. 1 
  22. PATT:linux$  
  23. HOLD:$  
  24. COMM:p  
  25. linux  
  26. PATT:linux$  
  27. HOLD:$  
  28. COMM:}  
  29. PATT:linux$  
  30. HOLD:$  
  31. PATT:centos$  
  32. HOLD:$  
  33. COMM:/^linux/ {  
  34. PATT:centos$  
  35. HOLD:$  
  36. PATT:redhat$  
  37. HOLD:$  
  38. COMM:/^linux/ {  
  39. PATT:redhat$  
  40. HOLD:$  
  41. PATT:linux$  
  42. HOLD:$  
  43. COMM:/^linux/ {  
  44. COMM:=  
  45. 4 
  46. PATT:linux$  
  47. HOLD:$  
  48. COMM:p  
  49. linux  
  50. PATT:linux$  
  51. HOLD:$  
  52. COMM:}  
  53. PATT:linux$  
  54. HOLD:$  
  55. PATT:ubuntu linux$  
  56. HOLD:$  
  57. COMM:/^linux/ {  
  58. PATT:ubuntu linux$  
  59. HOLD:$  
  60. PATT:fedora$  
  61. HOLD:$  
  62. COMM:/^linux/ {  
  63. PATT:fedora$  
  64. HOLD:$  
  65. [root@localhost ~]# sedsed -d   '/^linux/{=;p}' 1.txt  
  66. PATT:linux$  
  67. HOLD:$  
  68. COMM:/^linux/ {  
  69. COMM:=  
  70. 1 
  71. PATT:linux$  
  72. HOLD:$  
  73. COMM:p  
  74. linux  
  75. PATT:linux$  
  76. HOLD:$  
  77. COMM:}  
  78. PATT:linux$  
  79. HOLD:$  
  80. linux  
  81. PATT:centos$  
  82. HOLD:$  
  83. COMM:/^linux/ {  
  84. PATT:centos$  
  85. HOLD:$  
  86. centos  
  87. PATT:redhat$  
  88. HOLD:$  
  89. COMM:/^linux/ {  
  90. PATT:redhat$  
  91. HOLD:$  
  92. redhat  
  93. PATT:linux$  
  94. HOLD:$  
  95. COMM:/^linux/ {  
  96. COMM:=  
  97. 4 
  98. PATT:linux$  
  99. HOLD:$  
  100. COMM:p  
  101. linux  
  102. PATT:linux$  
  103. HOLD:$  
  104. COMM:}  
  105. PATT:linux$  
  106. HOLD:$  
  107. linux  
  108. PATT:ubuntu linux$  
  109. HOLD:$  
  110. COMM:/^linux/ {  
  111. PATT:ubuntu linux$  
  112. HOLD:$  
  113. ubuntu linux  
  114. PATT:fedora$  
  115. HOLD:$  
  116. COMM:/^linux/ {  
  117. PATT:fedora$  
  118. HOLD:$  
  119. fedora 

上面sedsed的調(diào)試顯示了sed  -n的參數(shù)的實際實現(xiàn):禁止自動打印模式空間內(nèi)容.

sed 多行處理

  1. sed多行處理是通過n和N來實現(xiàn)  
  2. 多行Next(N)命令是相對next(n)命令的,后者將模式空間中的內(nèi)容輸出,然后把下一行讀入模式空間,但是腳本并不會轉(zhuǎn)移到開始而是從當(dāng)前的n命令之后開始執(zhí)行; 而前者則保存原來模式空間中的內(nèi)容,再把新的一行讀入,兩者之間依靠一個換行符"\n"來分隔。在N命令執(zhí)行后,控制流將繼續(xù)用N命令以后的命令對模式空間進(jìn)行處理.  
  3. 值得注意的是,在多行模式中,特殊字符"^"和"$"匹配的是模式空間的最開始與最末尾,而不是內(nèi)嵌"\n"的開始與末尾. 

如下現(xiàn)在要將"Owner and Operator Guide"替換為"Installation Guide":

  1. [root@localhost ~]# cat 3.txt   
  2. Consult Section 3.1 in the Owner and Operator  
  3. Guide for a description of the tape drives  
  4. available on your system.  
  5. [root@localhost ~]# sedsed -d --hide=hold '/Operator$/{n;s/Owner and Operator\nGuide /Installation Guide\n/}' 3.txt  
  6. PATT:Consult Section 3.1 in the Owner and Operator$  
  7. COMM:/Operator$/ {  
  8. COMM:n  
  9. Consult Section 3.1 in the Owner and Operator  
  10. PATT:Guide for a description of the tape drives$  
  11. COMM:s/Owner and Operator\nGuide /nstallation Guide\n/  
  12. PATT:Guide for a description of the tape drives$  
  13. COMM:}  
  14. PATT:Guide for a description of the tape drives$  
  15. Guide for a description of the tape drives  
  16. PATT:available on your system.$  
  17. COMM:/Operator$/ {  
  18. PATT:available on your system.$  
  19. available on your system.  
  20. [root@localhost ~]# sedsed -d --hide=hold '/Operator$/{N;s/Owner and Operator\nGuide /Installation Guide\n/}' 3.txt  
  21. PATT:Consult Section 3.1 in the Owner and Operator$  
  22. COMM:/Operator$/ {  
  23. COMM:N  
  24. PATT:Consult Section 3.1 in the Owner and Operator\nGuide for a descr\  
  25. iption of the tape drives$  
  26. COMM:s/Owner and Operator\nGuide /nstallation Guide\n/  
  27. PATT:Consult Section 3.1 in the nstallation Guide\nfor a description \  
  28. of the tape drives$  
  29. COMM:}  
  30. PATT:Consult Section 3.1 in the nstallation Guide\nfor a description \  
  31. of the tape drives$  
  32. Consult Section 3.1 in the nstallation Guide  
  33. for a description of the tape drives  
  34. PATT:available on your system.$  
  35. COMM:/Operator$/ {  
  36. PATT:available on your system.$  
  37. available on your system. 

sed用于把文本讀入模式空間或者模式空間的內(nèi)容寫入到指定文本中.
sed把模式空間的內(nèi)容寫入到指定文本中:由w和W實現(xiàn)
w filename:Write the current pattern space to filename.
W filename:Write the first line of the current pattern space to filename.
如下:

  1. [root@localhost ~]# cat 1.txt   
  2. linux server  
  3. centos  
  4. redhat  
  5. linux web  
  6. ubuntu linux  
  7. fedora  
  8. [root@localhost ~]# sed  -n '/^linux/,/^linux/w a.txt' 1.txt   
  9. [root@localhost ~]# cat a.txt  
  10. linux server  
  11. centos  
  12. redhat  
  13. linux web 

sed把文本讀入模式空間由r實現(xiàn)

  1. [root@localhost ~]# cat 1.txt   
  2. linux server  
  3. centos  
  4. redhat  
  5. linux web  
  6. ubuntu linux  
  7. fedora  
  8. [root@localhost ~]# sed  '/centos/r /root/1.txt' 1.txt   
  9. linux server  
  10. centos  
  11. linux server  
  12. centos  
  13. redhat  
  14. linux web  
  15. ubuntu linux  
  16. fedora  
  17. redhat  
  18. linux web  
  19. ubuntu linux  
  20. fedora 

sed高級流控制

  1. b 分支:無條件轉(zhuǎn)移 ,分支到腳本中帶有標(biāo)記的地方,如果分支不存在則分支到腳本的末尾.  
  2. t 有條件的轉(zhuǎn)移,if分支,從最后一行開始,條件一旦滿足或者T,t命令,將導(dǎo)致分支到帶有標(biāo)號的命令處,或者到腳本的末尾.  
  3. T 有條件的轉(zhuǎn)移,錯誤分支,從最后一行開始,一旦發(fā)生錯誤或者T,t命令,將導(dǎo)致分支到帶有標(biāo)號的命令處,或者到腳本的末尾. 

[root@localhost ~]# cat 4.txt
a b c a d a a a
s d d d x s a
h j s a s h j h
j d f j a s j k j
要求:刪除行內(nèi)與第一列字符重復(fù)的字符,shell、sed、awk各寫一個。達(dá)到這個結(jié)果:
a b c d
s d d d x a
h j s a s j
j d f a s k
這個例子來自http://blog.chinaunix.net/uid-10540984-id-3086644.html

  1. [root@localhost ~]# sed ':a;s/^\(.\)\(.*\) \1/\1\2/;ta' 4.txt   
  2. a b c d  
  3. s d d d x a  
  4. h j s a s j  
  5. j d f a s k  
  6. [root@localhost ~]# sedsed -d --hide=hold ':a;s/^\(.\)\(.*\) \1/\1\2/;ta' 4.txt   
  7. PATT:a b c a d a a a$  
  8. COMM::a  
  9. COMM:s/^\(.\)\(.*\) \1/\1\2/  
  10. PATT:a b c a d a a$  
  11. COMM:t a  
  12. COMM:s/^\(.\)\(.*\) \1/\1\2/  
  13. PATT:a b c a d a$  
  14. COMM:t a  
  15. COMM:s/^\(.\)\(.*\) \1/\1\2/  
  16. PATT:a b c a d$  
  17. COMM:t a  
  18. COMM:s/^\(.\)\(.*\) \1/\1\2/  
  19. PATT:a b c d$  
  20. COMM:t a  
  21. COMM:s/^\(.\)\(.*\) \1/\1\2/  
  22. PATT:a b c d$  
  23. COMM:t a  
  24. PATT:a b c d$  
  25. a b c d  
  26. PATT:s d d d x s a$  
  27. COMM::a  
  28. COMM:s/^\(.\)\(.*\) \1/\1\2/  
  29. PATT:s d d d x a$  
  30. COMM:t a  
  31. COMM:s/^\(.\)\(.*\) \1/\1\2/  
  32. PATT:s d d d x a$  
  33. COMM:t a  
  34. PATT:s d d d x a$  
  35. s d d d x a  
  36. PATT:h j s a s h j h$  
  37. COMM::a  
  38. COMM:s/^\(.\)\(.*\) \1/\1\2/  
  39. PATT:h j s a s h j$  
  40. COMM:t a  
  41. COMM:s/^\(.\)\(.*\) \1/\1\2/  
  42. PATT:h j s a s j$  
  43. COMM:t a  
  44. COMM:s/^\(.\)\(.*\) \1/\1\2/  
  45. PATT:h j s a s j$  
  46. COMM:t a  
  47. PATT:h j s a s j$  
  48. h j s a s j  
  49. PATT:j d f j a s j k j$  
  50. COMM::a  
  51. COMM:s/^\(.\)\(.*\) \1/\1\2/  
  52. PATT:j d f j a s j k$  
  53. COMM:t a  
  54. COMM:s/^\(.\)\(.*\) \1/\1\2/  
  55. PATT:j d f j a s k$  
  56. COMM:t a  
  57. COMM:s/^\(.\)\(.*\) \1/\1\2/  
  58. PATT:j d f a s k$  
  59. COMM:t a  
  60. COMM:s/^\(.\)\(.*\) \1/\1\2/  
  61. PATT:j d f a s k$  
  62. COMM:t a  
  63. PATT:j d f a s k$  
  64. j d f a s k  
  65.  
  66. 附加兩種其他的解法  
  67. while read a b;do echo "$a ${b// $a}";done <4.txt 
  68. awk '{a=$1;gsub(" ?"a,"");print a""$0}' 4.txt 



 

向AI問一下細(xì)節(jié)
推薦閱讀:
  1. sed的使用
  2. sed用法

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI