3

タブで区切られた列を持つログ ファイルを生成したいと考えています。コメント フィールドを除くすべてのタブで区切られた出力を含む次の形式にする必要があります。

time        date        alias   comment
10:09:20    03/06/13    jre     This is a test comment

歴史的な目的で csh を使用しています

set time = `perl -MPOSIX -e 'print POSIX::strftime("%T", localtime)'`
set date = `perl -MPOSIX -e 'print POSIX::strftime("%d/%m/%y", localtime)'`
set alias = jre
set comment = "This is a test comment"

テキストをパイプする column -t

echo "time\tdate\talias\tcomment" | column -t > somefile
echo "$time\t$date\t$alias\t$comment" | column -t >> tt

欲しいものはほぼ手に入る。ただし、コメント フィールドのスペースもタブに変更されます。最初の 3 つのフィールドをタブで区切って、コメント フィールドのスペース区切りを維持する方法はありますか?

4

3 に答える 3

2

1 つのオプションは、すべてのジョブをで行うことです。

awk '
    BEGIN {
        OFS = "\t"
        header = "time date alias comment"
        gsub( /\s+/, OFS, header )
        print header

        out[0] = strftime( "%T", systime() )
        out[1] = strftime( "%d/%m/%y", systime() )
        out[2] = "jre"
        out[3] = "This is a test comment"

        l = length( out )
        for ( i = 0; i < l; i++ ) {
            printf "%s%s", out[ i ], i == l ? "" : OFS;
        }
        printf "\n"
    }
'

次の結果が得られます。

time    date    alias   comment
11:45:00    03/06/13    jre This is a test comment

ヘッダーのすぐ下には印刷されませんが、その場合は、printf代わりにフォーマットされた印刷 ( ) を使用することをお勧めします。

于 2013-06-03T09:49:35.447 に答える