Estoy tratando de convertir filas en columnas en formato de tabla.
Server Name : dev1-151vd Status : DONE Begin time : 2021-12-20 04:30:05.458719-05:00 End time : 2021-12-20 04:33:15.549731-05:00 Server Name : dev2-152vd Status : DONE Begin time : 2021-12-20 04:30:05.405746-05:00 End time : 2021-12-20 04:30:45.212935-05:00Usé el siguiente script awk para transponer filas en columnas
awk -F":" -vn=4 \ 'BEGIN { x=1; c=0;} ++c <= n && x == 1 {print $1; buf = buf $2 "\n"; if(c == n) {x = 2; printf buf} next;} !/./{c=0;next} c <=n {printf "%4s\n", $2}' temp1.txt | \ paste - - - - | \ column -t -s "$(printf "\t")" Server Name Status Begin time End time dev1-151vd DONE 2021-12-20 04 2021-12-20 04 dev2-152vd DONE 2021-12-20 04 2021-12-20 04El o/p anterior no tiene la hora de inicio y la hora de finalización adecuadas. Indíqueme cómo obtener el formato correcto para que la hora se imprima correctamente.
$ cat tst.awk BEGIN { OFS="\t" } NF { if ( ++fldNr == 1 ) { recNr++ rec = "" } tag = val = $0 sub(/[[:space:]]*:.*/,"",tag) sub(/[^:]+:[[:space:]]*/,"",val) hdr = hdr (fldNr>1 ? OFS : "") tag rec = rec (fldNr>1 ? OFS : "") val next } { if ( recNr == 1 ) { print hdr } print rec fldNr = 0 } END { if (fldNr) print rec } $ awk -f tst.awk file | column -s$'\t' -t Server Name Status Begin time End time dev1-151vd DONE 2021-12-20 04:30:05.458719-05:00 2021-12-20 04:33:15.549731-05:00 dev2-152vd DONE 2021-12-20 04:30:05.405746-05:00 2021-12-20 04:30:45.212935-05:00 Lo anterior funcionará sin importar cuántas líneas por registro tenga en su entrada y si tiene otros : s o %s s o cualquier otra cosa.
Ver este guión:
awk -F": " -vn=4 \ 'BEGIN { x=1; c=0;} ++c <= n && x == 1 {print $1; buf = buf $2 "\n"; if(c == n) {x = 2; printf buf} next;} !/./{c=0;next} c <=n {printf "%4s\n", $2}' 20211222.txt | \ paste - - - - | \ column -t -s "$(printf "\t")"Producción:
Server Name Status Begin time End time dev1-151vd DONE 2021-12-20 04:30:05.458719-05:00 2021-12-20 04:33:15.549731-05:00 dev2-152vd DONE 2021-12-20 04:30:05.405746-05:00 2021-12-20 04:30:45.212935-05:00 Explicación: En awk, la opción -F significa separador de campo. En su código, usó dos puntos para separar las columnas entre sí. Sin embargo, en su entrada, algunas líneas tienen más de 1 punto (es decir, su campo de marca de tiempo solo tiene 3 puntos), por lo tanto, awk las interpreta como si tuvieran 5 columnas.
La solución es agregar un espacio en blanco a su separador de campo ( ": " ), ya que su entrada tiene un espacio en blanco después de los primeros dos puntos y antes de su segunda columna.