
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
您可以使用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