sed使用小結

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

 

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

這讓人感覺很抽象,sedsed的工具就是爲了展現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表示隱藏指定的內容;如--hide=hold表示隱藏的保留空間緩衝區的內容  
  28. -i 的--indent 格式化複雜的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輸出顯示模式空間緩衝區的內容
COMM:顯示正在執行的命令
HOLD:顯示hold sapce緩衝區的內容

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

  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看看執行過程 //後面的內容是我註釋的

  1. [root@localhost ~]# sedsed -d  '/^$/{N;/^\n$/D}' 2.txt   
  2. PATT:This line is followed by 1 blank line.$  //pattern空間讀入第一行內容  
  3. HOLD:$                                        //hold空間開始爲空  
  4. COMM:/^$/ {            //正在執行的命令,判斷是否爲空行,很明顯不是,所有不執行後面的命令,執行的結果送往屏幕並把結果給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:/^$/ {           //正在執行的命令,判斷是否爲空行,很明顯是,所有執行後面的命令  
  11. COMM:N                //執行N讀取下一行進入pattern空間  
  12. PATT:\nThis line is followed by 2 blank line.$  
  13. HOLD:$               //此時hold空間還是爲空  
  14. COMM:/^\n$/ D        //對pattern空間繼續執行後面的命令:如果是空行,執行D命令,很明顯不是,所有不執行。  
  15. PATT:\nThis line is followed by 2 blank line.$  //pattern空間內容  
  16. HOLD:$              //hold空間內容,任然爲空。  
  17. COMM:}  
  18. PATT:\nThis line is followed by 2 blank line.$  
  19. HOLD:$  
  20.  
  21. This line is followed by 2 blank line.  //由於沒有滿足執行條件,繼續讀取下一行。  
  22. PATT:$           //空行,滿足執行條件,執行命令  
  23. HOLD:$  
  24. COMM:/^$/ {      
  25. COMM:N  
  26. PATT:\n$  
  27. HOLD:$  
  28. COMM:/^\n$/ D    //滿足執行D命令條件,刪除一空行(現在有兩空行)  
  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:$         \\空行,滿足命令執行條件,執行命令,讀取下一行.  
  44. HOLD:$  
  45. COMM:/^$/ {  
  46. COMM:N  
  47. PATT:\n$  
  48. HOLD:$  
  49. COMM:/^\n$/ D  \\滿足執行D命令,刪除一空行.  
  50. PATT:$         \\空行,滿足命令執行條件,執行命令,讀取下一行.  
  51. HOLD:$  
  52. COMM:/^$/ {  
  53. COMM:N  
  54. PATT:\n$  
  55. HOLD:$  
  56. COMM:/^\n$/ D  \\ \\滿足執行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:$          \\空行,滿足命令執行條件,執行命令,讀取下一行.  
  129. HOLD:$  
  130. COMM:/^$/ {  
  131. COMM:N  
  132. PATT:\n$  
  133. HOLD:$  
  134. COMM:/^\n$/ d   \\滿足執行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. 用行標示號來刪除  
  5. sed  'n1d'  filename   刪除第n1行  
  6. sed 'n1,n2d' filename  刪除第n1行到n2行間內容(n1<=n2)  
  7. sed  '5,$d' filename   刪除第5行以後內容內容  
  8. 用特殊匹配來刪除,格式如下  
  9. sed '/regular_pattern/d' filename 

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

  1. a\命令是追加命令,追加將添加新文本到文件中當前行(即讀入模式緩衝區中的行)的後面.所追加的文本行位於sed命令的下方另起一行.如果要追加的內容超過一行,則每一行都必須以反斜線結束,最後一行除外.最後一行將以引號和文件名結束.  
  2. i\ 命令是在當前行的前面插入新的文本.  
  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處理內容,最重要的就是要找特徵數據(字符),並以此爲基礎處理內容而不會處理的內容過多或者處理不完全.
如最開始的舉的把linux換成debian的例子.

sed 的高級運用
打印匹配行號

  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的調試顯示了sed  -n的參數的實際實現:禁止自動打印模式空間內容.

sed 多行處理

  1. sed多行處理是通過n和N來實現  
  2. 多行Next(N)命令是相對next(n)命令的,後者將模式空間中的內容輸出,然後把下一行讀入模式空間,但是腳本並不會轉移到開始而是從當前的n命令之後開始執行; 而前者則保存原來模式空間中的內容,再把新的一行讀入,兩者之間依靠一個換行符"\n"來分隔。在N命令執行後,控制流將繼續用N命令以後的命令對模式空間進行處理.  
  3. 值得注意的是,在多行模式中,特殊字符"^"和"$"匹配的是模式空間的最開始與最末尾,而不是內嵌"\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用於把文本讀入模式空間或者模式空間的內容寫入到指定文本中.
sed把模式空間的內容寫入到指定文本中:由w和W實現
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實現

  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 分支:無條件轉移 ,分支到腳本中帶有標記的地方,如果分支不存在則分支到腳本的末尾.  
  2. t 有條件的轉移,if分支,從最後一行開始,條件一旦滿足或者T,t命令,將導致分支到帶有標號的命令處,或者到腳本的末尾.  
  3. T 有條件的轉移,錯誤分支,從最後一行開始,一旦發生錯誤或者T,t命令,將導致分支到帶有標號的命令處,或者到腳本的末尾. 

[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
要求:刪除行內與第一列字符重複的字符,shell、sed、awk各寫一個。達到這個結果:
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 



 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章