Bash は出力グループを列に分割します。

Bash は出力グループを列に分割します。

以下のような出力があります。この出力を使用しますが、同様のグループ/ブロックを使用して列を生成したいと思います。配管後の出力を処理します。

id: c2b751c227111edfgdghfhfg19079a466e1916f6df4
blob_size: 1965
weight: 1965
fee: 0.000015690000
fee/byte: 0.000000007984
receive_time: 1613453355 (3 minutes ago)
relayed: 16134535355 (3 minutes ago)
do_not_relay: F
kept_by_block: F
double_spend_seen: F
max_used_block_height: 2279513
max_used_block_id: 44fbf6656dff890aedc29ashfhdfh2f3f848dd94c2dcb79562f5b
last_failed_height: 0
last_failed_id: 0000000000000000000000000000000000000000000000000000000000000000

id: be7e8ec30c8fff9deb33702fd392a566e4a44192138086aaff2d0c9fa82cdbfe
blob_size: 1968
weight: 1968
fee: 0.000015710000
fee/byte: 0.000000007982
receive_time: 13452 (36 seconds ago)
relayed: 16113434534512 (36 seconds ago)
do_not_relay: F
kept_by_block: F
double_spend_seen: F
max_used_block_height: 2234534515
max_used_block_id: 16433ec85ab8a9e81ac714c2cc3171149dfgdfgf34eec4d42e81
last_failed_height: 0
last_failed_id: 0000000000000000000000000000000000000000000000000000000000000000

それはまるで。

id: 234234                id: 234234234
blob_size: 234234         blob_size: 2342342
.                         .
.                         .
.                         .
[empty line]              [empty line]
id :234234234             id: fwfsdfsdfsdf
blob_size: 24234234       blob_size: 234234
.                         .
.                         .
.                         .

誰かが助けることができることを願っています。

乾杯

ベストアンサー1

ブロックを複製または削除せずに奇数のブロックを処理できることを示すために、サンプル入力に3番目のブロックを追加し、各行の幅を20文字以下にし、ブロックあたりの行数を減らしました(下記参照)。 。この答え)出力が行と列に分割される方法を簡単に確認できます。

$ cat tst.awk
BEGIN {
    RS = ""
    FS = "\n"
    OFS = "\t"
    maxCols = (maxCols ? maxCols : 2)
}
{
    numRows = NF
    numCols++
    for (rowNr=1; rowNr<=numRows; rowNr++) {
        vals[rowNr,numCols] = $rowNr
    }
}
(NR%maxCols) == 0 { prt() }
END { prt() }

function prt(   rowNr,colNr) {
    if ( numCols != 0 ) {
        for ( rowNr=1; rowNr<=numRows; rowNr++) {
            for (colNr=1; colNr<=numCols; colNr++) {
                printf "%s%s", vals[rowNr,colNr], (colNr<numCols ? OFS : ORS)
            }
        }
        print ""
        numCols = 0
    }
}

$ awk -f tst.awk file | column -s$'\t' -tL
id: c2b751c227111edf  id: be7e8ec30c8fff9d
blob_size: 1965       blob_size: 1968
weight: 1965          weight: 1968
fee: 0.000015690000   fee: 0.000015710000
fee/byte: 0.00000000  fee/byte: 0.00000000

id: blahblahblahblah
blob_size: 1968
weight: 1968
fee: 0.000015710000
fee/byte: 0.00000000

ラインあたりのブロック(列)出力数を2以外の数に設定するには、ラインごとに出力するmaxColsブロック(列)の数を設定します。

$ awk -v maxCols=3 -f tst.awk file | column -s$'\t' -tL
id: c2b751c227111edf  id: be7e8ec30c8fff9d  id: blahblahblahblah
blob_size: 1965       blob_size: 1968       blob_size: 1968
weight: 1965          weight: 1968          weight: 1968
fee: 0.000015690000   fee: 0.000015710000   fee: 0.000015710000
fee/byte: 0.00000000  fee/byte: 0.00000000  fee/byte: 0.00000000

上記はこの入力ファイルに対して実行されました。

$ cat file
id: c2b751c227111edf
blob_size: 1965
weight: 1965
fee: 0.000015690000
fee/byte: 0.00000000

id: be7e8ec30c8fff9d
blob_size: 1968
weight: 1968
fee: 0.000015710000
fee/byte: 0.00000000

id: blahblahblahblah
blob_size: 1968
weight: 1968
fee: 0.000015710000
fee/byte: 0.00000000

おすすめ記事