Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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!

share|improve this question

4 Answers 4

up vote 5 down vote accepted

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
share|improve this answer
    
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

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.

share|improve this answer

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

share|improve this answer
    
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

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

ls /etc | pr -2 -t
abrt                                gtk-2.0
acpi                                hal
adjtime                             hba.conf
agent.properties                    host.conf
aliases                             hosts
aliases.db                          hosts.allow
...
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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