如何将文本换行成两列?

如何将文本换行成两列?

fold如果字符数超过一定数量,可以换行。但是,我想将每行少于 40 个字符的文本文件包装成两列(每行总共 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

您可以使用columnsautogen 包中的命令,例如:

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

相关内容