5

fold can wrap the line if it has more than a certain amount of characters. However, I want to wrap a text file which has less than 40 characters in each line into two columns (80 chars per line total).

I want to make

apple
banana
(28 items omitted)
grape
guava

into

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

How can I make it?

7

Using the -COLUMN or --columns=COLUMN option of 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

so either

pr -t -2 yourfile

or

pr -t --columns=2 yourfile


For example, augmenting your entries with some random dictionary words,

$ 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
  • Great. It cannot choose indent using spaces, but this can be solved by expand. – Kevin Dong Oct 23 '16 at 17:31
6

You can use the columns command from the autogen package, e.g.:

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

For example:

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

Output:

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
0

Adding to Steeldriver's answer, if you have a requirement like mine, i.e. to print alternate words in same columns, use -a(across) option.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.