18

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!

15

Did you actually try column? It seems to be exactly 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
  • I did try. But wrongly (with -t)... But for yours to work reliably you probably need --columns`. – Hauke Laging Jul 4 '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 4 '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 4 '13 at 15:35
11

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
...
  • not working properly for large (long) file. – once May 7 at 18:03
2

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.

  • Or ... | paste - - when "splitting long output"... – don_crissti Apr 7 '15 at 1:17
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

  • columns: command not found. autogen? – Gilles Jul 5 '13 at 23:16
  • @Gilles - yes it's part of autogen. Just checked on my Fedora 14 system. I'll mention it in the answer as well. – slm Jul 5 '13 at 23:18

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.