首页 > 解决方案 > 我正在寻找一个 sed 命令,以将源代码中的多行注释减少为单行

问题描述

我正在寻找一个 sed 命令来将源文件中的所有注释在Startmarker:之后和endMarker之前(不包括)加入一行。结束标记之前可能有空行,也可能没有。

    Startmarker: comments here comments here comments here comments here comments here comments here 
    comments here comments here comments here comments here comments here comments here comments here comments here comments here comments here comments here.
    endMarker code
    code
code
code
     Startmarker: comments here comments here comments here comments here comments here comments here 
    comments here comments here comments here comments here comments here comments here comments here comments here comments here comments here comments here.
    endMarker

code 
code

我试过以下

awk '
  /.*Startmarker/,/.*endMarker/ {

    if (/\n/)                   
      printf "%s", $0          
    else
      print
  }
' file.name

标签: sed

解决方案


找到awk时设置一个标志Startmarker

awk '/Startmarker/ {nonewline=1}
     /endMarker/ {nonewline=0} 
     { if (nonewline==1) printf("%s",$0); else print $0; }' file.name

推荐阅读