Using awk to align columns in text file?

前端 未结 3 945
暗喜
暗喜 2020-12-23 12:45

Would awk be useful to convert \"Input\" to \"Desired output\"?

Input

testing speed of encryption
test 0 (64 bit key, 1         


        
3条回答
  •  天涯浪人
    2020-12-23 12:58

    Yes. Look at the syntax for awk's printf() function. Abbreviated sample code . . .

    {
      printf("%s %2s ", $1, $2);
      printf("%4s %s %s ", $3, $4, $5);
      printf("%4s %s %s ", $6, $7, $8);
      printf("%7s\n", $9);
    }
    

    Output.

    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 for GNU awk's printf().

    There are several ways to pass the "heading" through unmodified. This way assumes it's always on the first line of the file.

    NR==1 { print $0}
    NR>1 {
      printf("%s %2s ", $1, $2);
      printf("%4s %s %s ", $3, $4, $5);
      printf("%4s %s %s ", $6, $7, $8);
      printf("%7s\n", $9);
    }
    

提交回复
热议问题