他のコマンドの入力を分割し、結果をマージします。

他のコマンドの入力を分割し、結果をマージします。

私は他のコマンドの結果を組み合わせる方法を知っています。

paste -t',' <(commanda) <(commandb)

同じ入力を別のコマンドに渡す方法を知っています。

cat myfile | tee >(commanda) >(commandb)

今、これらのコマンドを組み合わせる方法は?だから私はそれを行うことができます

cat myfile | tee >(commanda) >(commandb) | paste -t',' resulta resultb

私にファイルがあるとします。

私のファイル:

1
2
3
4

新しいファイルを作成したい

1 4 2
2 3 4
3 2 6
4 1 8

使った

cat myfile | tee >(tac) >(awk '{print $1*2}') | paste

垂直方向の結果を得るには、水平方向に貼り付けたいと思います。

ベストアンサー1

複数のプロセス置換を実行するときに特定の順序で出力を取得できるという保証はありません。

paste -t',' <(commanda < file) <(commandb < file)

高価なパイプを表現すると仮定すると、cat myfile出力をファイルまたは変数に保存する必要があると思います。

output=$( some expensive pipeline )
paste -t',' <(commanda <<< "$output") <(commandb <<< "$output")

あなたの例を使用して:

output=$( seq 4 )
paste -d' ' <(cat <<<"$output") <(tac <<<"$output") <(awk '$1*=2' <<<"$output")
1 4 2
2 3 4
3 2 6
4 1 8

もう一つのアイデア:FIFOとシングルパイプ

mkfifo resulta resultb
seq 4 | tee  >(tac > resulta) >(awk '$1*=2' > resultb) | paste -d ' ' - resulta resultb
rm resulta resultb
1 4 2
2 3 4
3 2 6
4 1 8

おすすめ記事