Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

511
Views
bash convierte filas en columnas en formato de tabla

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:00

Usé 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 04

El 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.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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

over 4 years ago · Santiago Trujillo Report

0

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!