sed or awk multiline replace

心已入冬 提交于 2019-12-06 02:53:56

问题


I am trying to append formatting to all /* TODO : ... */ tags, but I am having trouble in the multi-line area. I can do single line sed's; but for multiline sed and awk, I don't know.

How do I do this? I'm open to either. Here's what I have so far.

sed 's/\/\/\*[ \t]*TODO[ \t]*:.*/*\//<span style="color:#aaaaaa;font-weight:bold;">&</span>/g'

replace :

int void main ( int h, char * argv[] )
  int a, b; /* TODO :
               - include libraries
               ...
            */
  foobar();
  /* TODO : fix missing {'s */

with :

int void main ( int h, char * argv[] )
  int a, b; <span style="color:#aaaaaa; font-weight:bold;">/* TODO :
               - include libraries
               ...
            */</span>
  foobar();
  <span style="color:#aaaaaa; font-weight:bold;">/* TODO : fix missing {'s */ </span>

回答1:


gawk 'BEGIN{
  RS="*/"
  replace="<span style=\"color:#aaaaaa; font-weight:bold;\">"
}
/\/\* +TODO/{
    gsub(/\/\* +TODO/,replace" /* TODO")
    RT=RT "</span>"
}
{ print $0RT}
' file

output

$ ./shell.sh
int void main ( int h, char * argv[] )
  int a, b; <span style="color:#aaaaaa; font-weight:bold;"> /* TODO :
               - include libraries
               ...
            */</span>

  foobar();
  <span style="color:#aaaaaa; font-weight:bold;"> /* TODO : fix missing {'s */</span>


来源:https://stackoverflow.com/questions/2377092/sed-or-awk-multiline-replace

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!