开发者

sed or awk multiline replace

开发者 https://www.devze.com 2022-12-22 07:46 出处:网络
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.

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>


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>
0

精彩评论

暂无评论...
验证码 换一张
取 消