複数のファイルをマージし、出力形式を改行で列として指定します。

複数のファイルをマージし、出力形式を改行で列として指定します。

短時間で複数のファイルをユーザーに表示したいと思います。これらのファイルは比較的長い行を持ち、同じテキストを含みますが、言語が異なります(行の長さの違いが予想されます)。

例:

ファイル1.txt

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut lectus arcu bibendum at.

file2.txt(上記、Google翻訳)

The pain itself is the love of the pain, the main ecological problems, but I give this kind of time to fall down, so that some great pain and pain.
To drink at the bed of the bow.

予想される結果:

Lorem ipsum dolor sit amet, consectetur adipiscing    The pain itself is the love of the pain, the main
elit, sed do eiusmod tempor incididunt ut labore et   ecological problems, but I give this kind of time
dolore magna aliqua.                                  to fall down, so that some great pain and pain.
Ut lectus arcu bibendum at.                           To drink at the bed of the bow.

行がその行より短く、改行されない場合は、残りのスペースは空白のままにしてください。これは、画面サイズに合わせて区切り文字を変更して、2つおよび3つのファイルに対して機能します。私はそれを操作するためにカラムとprでペーストを使ってみました。 POSIXコンプライアンスを優先してください。

paste file1.txt file2.txt | column -t -s $'\t'

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et
dolore magna aliqua.                       The pain itself is the love of the pain, the main ecological
problems, but I give this kind of time to fall down, so that some great pain and pain.
Ut lectus arcu bibendum at.                To drink at the bed of the bow.

問題は、最初のファイルの行が端末の中央に到達したときに折り返されないのに対し、2番目のファイルは折り返されますが、中央ではなく行の先頭から始まるということです。

助けてくれてありがとう。

ベストアンサー1

改行を行う最初そしてfold

paste <(fold -sw 40 file1.txt) <(fold -sw 40 file2.txt) | column -t -s $'\t'

端末のサイズに合わせて調整するには:

width=$(( (COLUMNS - 4) / 2))
paste <(fold -sw $width file1.txt) <(fold -sw $width file2.txt) | column -t -s $'\t'

行を横に並べるには、ファイルを1行ずつ繰り返す必要があります。

while IFS= read -r -u3 line1
      IFS= read -r -u4 line2
do
    paste <(fold -sw $width <<<"$line1") \
          <(fold -sw $width <<<"$line2")

done 3< file1.txt 4< file2.txt \
| column -t -s $'\t'

サンプル入力ファイルと幅が45の場合

Lorem ipsum dolor sit amet, consectetur       The pain itself is the love of the pain, the
adipiscing elit, sed do eiusmod tempor        main ecological problems, but I give this
incididunt ut labore et dolore magna aliqua.  kind of time to fall down, so that some
great pain and pain.
Ut lectus arcu bibendum at.                   To drink at the bed of the bow.

この時点で、我々はPOSIXシェルを越えた。まさにbashです。また、他のプログラミング言語を使用することも考慮する必要があります。

おすすめ記事