38

Is there a simple utility or script to columnate the output from one of my scripts? I have data in some form:

A aldkhasdfljhaf
B klajsdfhalsdfh
C salkjsdjkladdag
D lseuiorlhisnflkc
E sdjklfhnslkdfhn
F kjhnakjshddnaskjdh

but if this becomes two long, write the data in the following form (where still vertically ordered):

A aldkhasdfljhaf    D lseuiorlhisnflkc
B klajsdfhalsdfh    E sdjklfhnslkdfhn
C salkjsdjkladdag   F kjhnakjshddnaskjdh

From reading the manpage, I don't think that this is something column would be appropriate for but I'm not sure. It's easy enough to split in the form:

A B 
C D 
E F

by only printing \n every second line (what my current script does). Any ideas? Thanks!

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
Hemmer
  • 483
  • 1
  • 4
  • 7

4 Answers4

32

column seems to be what you want:

$ cat file
A aldkhasdfljhaf
B klajsdfhalsdfh
C salkjsdjkladdag
D lseuiorlhisnflkc
E sdjklfhnslkdfhn

$ column file
A aldkhasdfljhaf    D lseuiorlhisnflkc
B klajsdfhalsdfh    E sdjklfhnslkdfhn
C salkjsdjkladdag   F kjhnakjshddnaskjdh
Nick Bull
  • 553
  • 3
  • 13
terdon
  • 234,489
  • 66
  • 447
  • 667
  • I did try. But wrongly (with `-t)... But for yours to work reliably you probably need `--columns`. – Hauke Laging Jul 04 '13 at 14:43
  • I did try but for some reason I didn't realise that it depended on the width of your terminal window (seems obvious now)! – Hemmer Jul 04 '13 at 15:22
  • also I was testing this on two machines at once. Apparently the old version (circa 1993 on Scientific Linux) behaves differently to the newer 2004 version... – Hemmer Jul 04 '13 at 15:35
19

To columnate the output, pr converts text files for printing:

-COLUMN, --columns=COLUMN: output COLUMN columns
      and print columns down, unless -a is used.

-t, --omit-header : omit page headers and trailers

e.g.

ls /etc | pr -2 -t
abrt                                gtk-2.0
acpi                                hal
adjtime                             hba.conf
agent.properties                    host.conf
aliases                             hosts
aliases.db                          hosts.allow
...
don_crissti
  • 79,330
  • 30
  • 216
  • 245
Digix
  • 191
  • 1
  • 2
3

You can split the file in two (with the same number of lines or the first file having one line more) and then do this:

paste file1 file2

If the length of the lines is too different then is may be necessary to use printf to adapth the lengths by padding with spaces.

Hauke Laging
  • 88,146
  • 18
  • 125
  • 174
1

You can use just plain columns:

$ cat test.txt | columns
A aldkhasdfljhaf             B klajsdfhalsdfh             C salkjsdjkladdag
D lseuiorlhisnflkc           E sdjklfhnslkdfhn            F kjhnakjshddnaskjdh

NOTE: the columns command is part of the autogen package on my Fedora 14 system.

$ rpm -qf /usr/bin/columns
autogen-5.9.4-7.fc12.x86_64

References

slm
  • 363,520
  • 117
  • 767
  • 871