2012-12-30 11 views
15

awk "入力"を "希望出力"に変換すると便利ですか?awkを使用してテキストファイルの列を揃えますか?

入力

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) 

所望の出力

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) 

答えて

37

トリックは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) 
+2

+1、とても素敵... – Guru

+5

nice。ただし、最後の列の幅が一定でない場合、これは機能しません。 – hardmooth

14

はい。 awkのprintf()関数の構文を見てください。短縮されたサンプルコード。 。 。

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

出力。

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()

"見出し"を変更しないで渡す方法はいくつかあります。この方法は、ファイルの最初の行にあると仮定しています。 columnを使用して権利を揃える

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); 
} 
+0

非常に興味深いです!ヘッダーを整列させずに通過させることは可能ですか? –

+0

@SandraSchlichting:はい、更新された回答をご覧ください。 –

+0

私が言うことができる限り、これはマルチバイト文字列の文字列で 'width'修飾子('%4s'の '4 ')が大部分の人が"文字 "と考える代わりにバイトを数えているため、確実に機能しません –

4
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 
関連する問題