sed使用小结-创新互联

对于行处理的工具,sed绝对是选的。
sed处理文本过程如下:

创新互联服务紧随时代发展步伐,进行技术革新和技术进步,经过10年的发展和积累,已经汇集了一批资深网站策划师、设计师、专业的网站实施团队以及高素质售后服务人员,并且完全形成了一套成熟的业务流程,能够完全依照客户要求对网站进行网站设计、成都网站制作、建设、维护、更新和改版,实现客户网站对外宣传展示的首要目的,并为客户企业品牌互联网化提供全面的解决方案。

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.      -f, --file          add file contents to the commands to be parsed
  4.      -e, --expression    add the script to the commands to be parsed
  5.      -n, --quiet         suppress automatic printing of pattern space
  6.          --silent        alias to --quiet
  7.      -d, --debug         debug the sed script
  8.          --hide          hide some debug info (options: PATT,HOLD,COMM)
  9.          --color         shows debug output in colors (default: ON)
  10.          --nocolor       no colors on debug output
  11.          --dump-debug    dumps to screen the debugged sed script
  12.          --emu           emulates GNU sed (INCOMPLETE)
  13.          --emudebug      emulates GNU sed debugging the sed script (INCOMPLETE)
  14.      -i, --indent        script beautifier, prints indented and
  15.                          one-command-per-line output do STDOUT
  16.          --prefix        indent prefix string (default: 4 spaces)
  17.      -t, --tokenize      script tokenizer, prints extensive
  18.                          command by command information
  19.      -H, --htmlize       converts sed script to a colorful HTML page
  20. -f选项和sed的-f选项一样
  21. -d打开debug,其中--hide表示隐藏指定的内容;如--hide=hold表示隐藏的保留空间缓冲区的内容
  22. -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. This line is followed by 2 blank line.
  4. This line is followed by 3 blank line.
  5. This line is followed by 4 blank line.
  6. This is the end.
  1. [root@localhost ~]# sed '/^$/{N;/^\n$/D}' 2.txt
  2. This line is followed by 1 blank line.
  3. This line is followed by 2 blank line.
  4. This line is followed by 3 blank line.
  5. This line is followed by 4 blank line.
  6. This is the end.
  7. [root@localhost ~]# sed '/^$/{N;/^\n$/d}' 2.txt
  8. This line is followed by 1 blank line.
  9. This line is followed by 2 blank line.
  10. This line is followed by 3 blank line.
  11. This line is followed by 4 blank line.
  12. 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. This line is followed by 2 blank line.  //由于没有满足执行条件,继续读取下一行。
  21. PATT:$           //空行,满足执行条件,执行命令
  22. HOLD:$
  23. COMM:/^$/ {
  24. COMM:N
  25. PATT:\n$
  26. HOLD:$
  27. COMM:/^\n$/ D    //满足执行D命令条件,删除一空行(现在有两空行)
  28. PATT:$
  29. HOLD:$
  30. COMM:/^$/ {
  31. COMM:N
  32. PATT:\nThis line is followed by 3 blank line.$
  33. HOLD:$
  34. COMM:/^\n$/ D
  35. PATT:\nThis line is followed by 3 blank line.$
  36. HOLD:$
  37. COMM:}
  38. PATT:\nThis line is followed by 3 blank line.$
  39. HOLD:$
  40. This line is followed by 3 blank line.
  41. PATT:$         \\空行,满足命令执行条件,执行命令,读取下一行.
  42. HOLD:$
  43. COMM:/^$/ {
  44. COMM:N
  45. PATT:\n$
  46. HOLD:$
  47. COMM:/^\n$/ D  \\满足执行D命令,删除一空行.
  48. PATT:$         \\空行,满足命令执行条件,执行命令,读取下一行.
  49. HOLD:$
  50. COMM:/^$/ {
  51. COMM:N
  52. PATT:\n$
  53. HOLD:$
  54. COMM:/^\n$/ D  \\ \\满足执行D命令,删除一空行.
  55. PATT:$
  56. HOLD:$
  57. COMM:/^$/ {
  58. COMM:N
  59. PATT:\nThis line is followed by 4 blank line.$
  60. HOLD:$
  61. COMM:/^\n$/ D
  62. PATT:\nThis line is followed by 4 blank line.$
  63. HOLD:$
  64. COMM:}
  65. PATT:\nThis line is followed by 4 blank line.$
  66. HOLD:$
  67. This line is followed by 4 blank line.
  68. PATT:$
  69. HOLD:$
  70. COMM:/^$/ {
  71. COMM:N
  72. PATT:\n$
  73. HOLD:$
  74. COMM:/^\n$/ D
  75. PATT:$
  76. HOLD:$
  77. COMM:/^$/ {
  78. COMM:N
  79. PATT:\n$
  80. HOLD:$
  81. COMM:/^\n$/ D
  82. PATT:$
  83. HOLD:$
  84. COMM:/^$/ {
  85. COMM:N
  86. PATT:\n$
  87. HOLD:$
  88. COMM:/^\n$/ D
  89. PATT:$
  90. HOLD:$
  91. COMM:/^$/ {
  92. COMM:N
  93. PATT:\nThis is the end.$
  94. HOLD:$
  95. COMM:/^\n$/ D
  96. PATT:\nThis is the end.$
  97. HOLD:$
  98. COMM:}
  99. PATT:\nThis is the end.$
  100. HOLD:$
  101. This is the end.
  102. [root@localhost ~]# sedsed -d  '/^$/{N;/^\n$/d}' 2.txt
  103. PATT:This line is followed by 1 blank line.$
  104. HOLD:$
  105. COMM:/^$/ {
  106. PATT:This line is followed by 1 blank line.$
  107. HOLD:$
  108. This line is followed by 1 blank line.
  109. PATT:$
  110. HOLD:$
  111. COMM:/^$/ {
  112. COMM:N
  113. PATT:\nThis line is followed by 2 blank line.$
  114. HOLD:$
  115. COMM:/^\n$/ d
  116. PATT:\nThis line is followed by 2 blank line.$
  117. HOLD:$
  118. COMM:}
  119. PATT:\nThis line is followed by 2 blank line.$
  120. HOLD:$
  121. This line is followed by 2 blank line.
  122. PATT:$          \\空行,满足命令执行条件,执行命令,读取下一行.
  123. HOLD:$
  124. COMM:/^$/ {
  125. COMM:N
  126. PATT:\n$
  127. HOLD:$
  128. COMM:/^\n$/ d   \\满足执行d命令条件,此时模式空间的两个空行都被删除.
  129. PATT:This line is followed by 3 blank line.$
  130. HOLD:$
  131. COMM:/^$/ {
  132. PATT:This line is followed by 3 blank line.$
  133. HOLD:$
  134. This line is followed by 3 blank line.
  135. PATT:$
  136. HOLD:$
  137. COMM:/^$/ {
  138. COMM:N
  139. PATT:\n$
  140. HOLD:$
  141. COMM:/^\n$/ d
  142. PATT:$
  143. HOLD:$
  144. COMM:/^$/ {
  145. COMM:N
  146. PATT:\nThis line is followed by 4 blank line.$
  147. HOLD:$
  148. COMM:/^\n$/ d
  149. PATT:\nThis line is followed by 4 blank line.$
  150. HOLD:$
  151. COMM:}
  152. PATT:\nThis line is followed by 4 blank line.$
  153. HOLD:$
  154. This line is followed by 4 blank line.
  155. PATT:$
  156. HOLD:$
  157. COMM:/^$/ {
  158. COMM:N
  159. PATT:\n$
  160. HOLD:$
  161. COMM:/^\n$/ d
  162. PATT:$
  163. HOLD:$
  164. COMM:/^$/ {
  165. COMM:N
  166. PATT:\n$
  167. HOLD:$
  168. COMM:/^\n$/ d
  169. PATT:This is the end.$
  170. HOLD:$
  171. COMM:/^$/ {
  172. PATT:This is the end.$
  173. HOLD:$
  174. 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. while read a b;do echo "$a ${b// $a}";done <4.txt
  67. awk '{a=$1;gsub(" ?"a,"");print a""$0}' 4.txt

另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


分享名称:sed使用小结-创新互联
网页链接:http://scyanting.com/article/djphes.html