テキストを2列にまとめるには?

テキストを2列にまとめるには?

fold文字数が一定数を超えると改行されることがあります。しかし、1行に40文字未満のテキストファイルを2つの列(1行に合計80文字)で囲みたいです。

気づきたい

apple
banana
(28 items omitted)
grape
guava

入力する

apple                                   ...
banana                                  ...
(12 items omitted)                      (12 items omitted)
...                                     grape
...                                     guava

どうすればいいですか?

答え1

使用-COLUMNまたは--columns=COLUMNオプションpr

-COLUMN, --columns=COLUMN
       output COLUMN columns and print columns down, unless -a is used.
       Balance number of lines in the columns on each page

だからどちらにしても

pr -t -2 yourfile

または

pr -t --columns=2 yourfile


たとえば、ランダムな辞書の単語でアイテムを展開します。

$ cat << EOF | pr -t -2
> apple
> banana
> `shuf -n28 /usr/share/dict/words`
> grape
> guava
> EOF
apple                               overachieves
banana                              wickerwork
cottonmouths                        supersonic
adapter's                           draftiest
boudoir's                           insisting
cruised                             programs
mousetrap                           parcel
shticks                             basically
TLC's                               coruscates
conduction                          Jones
geeing                              Ty
gloamings                           bondage
investing                           candelabra's
radiotherapists                     Inchon's
clasp's                             grape
critters                            guava

答え2

columnsたとえば、autogenパッケージのコマンドを使用できます。

columns -c 2 -w 40 --by-column < input

たとえば、

{
  echo apple
  echo banana
  shuf -n28 /usr/share/dict/words
  echo grape
  echo guave
} |
columns -w 40 -c 2 --by-columns

出力:

apple                                   merwoman
banana                                  chiroplasty
antispreading                           stylommatophorous
spearmint                               Sphaerobolaceae
sulphoxyphosphate                       snark
nymphaeum                               reactionary
ahluwalia                               hobo
husky                                   oxamethane
crimeproof                              deltarium
cerebrosis                              hematoporphyrin
yoghurt                                 noncompoundable
colloquial                              sororially
unaffirmed                              nonobjection
saccharated                             reundercut
thermochemic                            grape
preobedience                            guave

答え3

Steeldriverの回答に加えて、同じ列に代替単語が印刷されるような要件がある場合は、-a(across)オプションを使用してください。

[user@server ~]$ cat << EOF | pr -a -t -2
> word1
> word2
> word3
> word4
> word5
> word6
> EOF
word1                               word2
word3                               word4
word5                               word6

関連情報