Text

如何將文本包裝成兩列?

  • March 14, 2019

fold如果它有超過一定數量的字元,可以換行。但是,我想將每行少於 40 個字元的文本文件包裝成兩列(每行總共 80 個字元)。

我要實現

apple
banana
(28 items omitted)
grape
guava

進入

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

我怎樣才能做到?

使用-COLUMNor--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

引用自:https://unix.stackexchange.com/questions/318321