代码之家  ›  专栏  ›  技术社区  ›  rlb.usa

sed或awk多行替换

  •  6
  • rlb.usa  · 技术社区  · 16 年前

    我正在尝试将格式附加到所有/*TODO:…*/标签,但我在多行区域有问题。我可以做单线sed's;但对于多行sed和awk,我不知道。

    我该怎么做?我对任何一个都持开放态度。 这是我目前掌握的情况。

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

    替换:

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

    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 回复  |  直到 16 年前
        1
  •  4
  •   ghostdog74    16 年前
    gawk 'BEGIN{
      RS="*/"
      replace="<span style=\"color:#aaaaaa; font-weight:bold;\">"
    }
    /\/\* +TODO/{
        gsub(/\/\* +TODO/,replace" /* TODO")
        RT=RT "</span>"
    }
    { print $0RT}
    ' file
    

    $ ./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>