¿Cómo mover partes y líneas repetidas de código c ++ usando awk o sed?

Nov 25 2020

Tengo una gran cantidad de código C ++ con miles de líneas como esta:

   case 14: //OrderSelect
      Execute_OrderSelect();
   break;
   case 15: // OrderGetDouble
      Execute_OrderGetDouble();
   break;
   case 16:   //OrderGetInteger
      Execute_OrderGetInteger();
   break;

Mi tarea es hacer que se vean así:

   case 14: Execute_OrderSelect();     break;   // OrderSelect
   case 15: Execute_OrderGetDouble();  break;   // OrderGetDouble
   case 16: Execute_OrderGetInteger(); break;   // OrderGetInteger

Tenga en cuenta que tanto Ejecutar ... como los comentarios pueden ser cualquier cadena.


Supongo que esquemáticamente podríamos escribir el original así:

AAA NN BBB
CCC
DDD

y tratar de convertirlo en: AAA NN CCC DDD BBB.

Lo intenté sin éxito con todo tipo de sedexpresiones, y lo mejor que pude hacer fue la operación trivial de combinar el Execute...()con el break;, pero no pude mover el comentario. Estoy pensando que estoy usando la herramienta incorrecta para esto, y quizás awksería una mejor opción o más simple de usar.

A continuación se muestran algunas awkvariables:

FNR    The input record number in the current input file.
FS     The input field separator, a space by default.
NF     The number of fields in the current input record.
NR     The total number of input records seen so far.
OFMT   The output format for numbers, "%.6g", by default.
OFS    The output field separator, a space by default.
ORS    The output record separator, by default a newline.
RS     The input record separator, by default a newline.
RT     The  record terminator. Gawk sets RT to the input 
       text that matched the character or regular expression 
       specified by RS.
RSTART The index of the first character matched by match(); 0 if no match

¿Cómo puedo hacer que mi día sea más brillante?


Preguntas relacionadas:

  • Forma AWK o sed de pegar líneas no adyacentes
  • cambiar la posición de una línea en un archivo usando sed
  • Cómo seleccionar líneas entre dos patrones de marcadores que pueden ocurrir varias veces con awk / sed

Respuestas

2 EdMorton Nov 25 2020 at 05:34

Aquí están los huesos, masaje para adaptarse;

$ cat tst.awk /^[[:space:]]*case[[:space:]]/ { comment = "" if ( match($0,"//") ) {
        comment = substr($0,RSTART) $0 = substr($0,1,RSTART-1) } caseLineNr = 1 } caseLineNr { if ( caseLineNr++ > 1 ) { sub(/^[[:space:]]+/,"") } sub(/[[:space:]]+$/,"")
    printf "%s\t", $0

    if ( /^break[[:space:]]*;/ ) {
        print comment
        caseLineNr = 0
    }
}

$ awk -f tst.awk file
   case 14:     Execute_OrderSelect();  break;  //OrderSelect
   case 15:     Execute_OrderGetDouble();       break;  // OrderGetDouble
   case 16:     Execute_OrderGetInteger();      break;  //OrderGetInteger