Utiliser awk pour aligner des colonnes dans un fichier texte?

est-ce que awk serait utile pour convertir" Input "en"Desired output"?

Entrée

testing speed of encryption
test 0 (64 bit key, 16 byte blocks): 2250265 operations in 1 seconds (36004240 bytes)
test 1 (128 bit key, 64 byte blocks): 879149 operations in 1 seconds (56265536 bytes)
test 2 (128 bit key, 256 byte blocks): 258978 operations in 1 seconds (66298368 bytes)
test 3 (128 bit key, 1024 byte blocks): 68218 operations in 1 seconds (69855232 bytes)
test 4 (128 bit key, 8192 byte blocks): 8614 operations in 1 seconds (70565888 bytes)
test 10 (256 bit key, 16 byte blocks): 1790881 operations in 1 seconds (3654096 bytes)

sortie désirée

testing speed of encryption
test  0  (64 bit key,   16 byte blocks): 2250265 operations in 1 seconds (36004240 bytes)
test  1 (128 bit key,   64 byte blocks):  879149 operations in 1 seconds (56265536 bytes)
test  2 (128 bit key,  256 byte blocks):  258978 operations in 1 seconds (66298368 bytes)
test  3 (128 bit key, 1024 byte blocks):   68218 operations in 1 seconds (69855232 bytes)
test  4 (128 bit key, 8192 byte blocks):    8614 operations in 1 seconds (70565888 bytes)
test 10 (256 bit key,   16 byte blocks): 1790881 operations in 1 seconds  (3654096 bytes)
18
demandé sur Sandra Schlichting 2012-12-31 01:49:00

3 réponses

une astuce pour s'aligner à droite en utilisant column est d'utiliser rev :

$ head -1 file; tail -n+2 file | rev | column -t | rev
testing speed of encryption
test   0   (64  bit  key,    16  byte  blocks):  2250265  operations  in  1  seconds  (36004240  bytes)
test   1  (128  bit  key,    64  byte  blocks):   879149  operations  in  1  seconds  (56265536  bytes)
test   2  (128  bit  key,   256  byte  blocks):   258978  operations  in  1  seconds  (66298368  bytes)
test   3  (128  bit  key,  1024  byte  blocks):    68218  operations  in  1  seconds  (69855232  bytes)
test   4  (128  bit  key,  8192  byte  blocks):     8614  operations  in  1  seconds  (70565888  bytes)
test  10  (256  bit  key,    16  byte  blocks):  1790881  operations  in  1  seconds   (3654096  bytes)
38
répondu Chris Seymour 2012-12-30 22:01:28

Oui. Regardez la syntaxe de la fonction printf() d'awk. Code échantillon abrégé . . .

{
  printf("%s %2s ", , );
  printf("%4s %s %s ", , , );
  printf("%4s %s %s ", , , );
  printf("%7s\n", );
}

sortie.

test  0  (64 bit key,   16 byte blocks): 2250265
test  1 (128 bit key,   64 byte blocks):  879149
test  2 (128 bit key,  256 byte blocks):  258978
test  3 (128 bit key, 1024 byte blocks):   68218
test  4 (128 bit key, 8192 byte blocks):    8614
test 10 (256 bit key,   16 byte blocks): 1790881

Docs pour GNU awk printf() .

il y a plusieurs façons de passer le" cap " sans modification. De cette façon, suppose qu'il est toujours sur la première ligne du fichier.

NR==1 { print "151920920"}
NR>1 {
  printf("%s %2s ", , );
  printf("%4s %s %s ", , , );
  printf("%4s %s %s ", , , );
  printf("%7s\n", );
}
18
répondu Mike Sherrill 'Cat Recall' 2012-12-30 23:51:44
awk '
FNR==1 { if (NR==FNR) print; next }
NR==FNR {
   for(i=1;i<=NF;i++)
      w[i] = (w[i] <= length($i) ? length($i) : w[i])
   next
}
{
   for(i=1;i<=NF;i++)
      printf "%*s",w[i]+(i>1?1:0),$i
   print ""
}
' file file
4
répondu Ed Morton 2012-12-31 07:37:25