Printing in columns

is there a short one-liner that can print out in columns instead of a long list?

as in, instead of:

apples
oranges
cats
dogs
sky
monkey
giraffe
cups
spoons
tv
cable

it'll print something like this (properly indented of course :slight_smile: ):

apples       cats       sky          giraffe   cups       tv
oranges      dogs     monkey     cups     spoons     cable

Shell: bash
OS: linux and sunos

Hi

Not sure whether this is a good way of doing this:

$ awk 'NR%2{x=x?x"\t"$0:$0;next}{y=y?y"\t"$0:$0;}END{print x"\n"y;}' file
apples  cats    sky     giraffe spoons  cable
oranges dogs    monkey  cups    tv

Guru

tried this. didn't work. it jumbles it up. some are properly aligned and others are just all meshed together. :frowning:

Hi.

Perhaps:

SYNOPSIS
     column [-ntx] [-c columns] [-s sep] [file ...]

DESCRIPTION
     The column utility formats its input into multiple columns.

...

as in:

% column -c60 data3
apples	cats	sky	giraffe	spoons	cable
oranges	dogs	monkey	cups	tv

The tab separator may look like oranges and dogs are together, but they have whitespace between them on my terminal.

You can jiggle it with expand:

% column -c60 data3|expand -10
apples    cats      sky       giraffe   spoons    cable
oranges   dogs      monkey    cups      tv

Best wishes ... cheers, drl

1 Like

thank you! this works.

works like this:

your command | column

very very simple!

Note: column is not generally available on unix systems...

try with nawk
or try this

# nawk 'NR%2{if(x)x=x"\t"$0;else x=$0;next}{if(y)y=y"\t"$0;else y=$0}END{print x"\n"y}' infile
apples  cats    sky     giraffe spoons  cable
oranges dogs    monkey  cups    tv

In this particular case:

awk 'END{printf "\n%s\n",n} getline p{n=n (n?ORS:x) p}1' ORS='\t' infile

General case perhaps:

xargs -n 1 < infile | awk '{n=(NR-1)%r; R[n]=R[n] (R[n]?"\t":x) $1} END{for(i=0;i<r;i++)print R}' r=2

r is the number of rows...

Hi.

Although command column might not be on some UNIX boxes, I'd rather have a limited general facility like column, rather than a plethora of single-shot solutions for lots of similar, but different situations.

The perl module Array::columnize allows one to create a work-alike. We use one such for the cases when column might not exist on a platform, like so:

#!/usr/bin/env bash

# @(#) s1	Demonstrate columnize with standard GNU/Linux column, local my-columns.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
edges() { local _f _n _l;: ${1?"edges: need file"}; _f=$1;_l=$(wc -l $_f);
  head -${_n:=3} $_f ; pe "--- ( $_l: lines total )" ; tail -$_n $_f ; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C column my-columns divepm

FILE=${1-data1}

pl " Input data file $FILE:"
cat $FILE

pl " Results, standard Linux column:"
column -c60 $FILE

pl " Results, work-alike for Linux column, \"my-columns\":"
my-columns -w 60 $FILE

pl " Characteristics of my-columns:"
v1=$( command -v my-columns )
file "$v1" | sed 's/^.*: //' 
pe "Lines: $( wc -l < $( command -v my-columns ) )"
pe "Modules used by my-columns:"
divepm -q -i $v1

exit 0

producing:

% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
bash GNU bash 3.2.39
column - ( /usr/bin/column, 2007-11-20 )
my-columns (local) 1.2
divepm (local) 1.2

-----
 Input data file data1:
apples
oranges
cats
dogs
sky
monkey
giraffe
cups
spoons
tv
cable

-----
 Results, standard Linux column:
apples	cats	sky	giraffe	spoons	cable
oranges	dogs	monkey	cups	tv

-----
 Results, work-alike for Linux column, "my-columns":
apples   cats  sky     giraffe  spoons  cable
oranges  dogs  monkey  cups     tv    

-----
 Characteristics of my-columns:
a perl script text executable
Lines: 67
Modules used by my-columns:
 1.06	warnings
 1.04	strict
 1.04	English
 1.08	Carp
 2.37	Getopt::Long
 0.4.2	Array::Columnize
 1.11	feature

I encourage other organizations to do the same, perhaps contrinuting to The Comprehensive Perl Archive Network - www.cpan.org and possibly to PPT: Unix Reconstruction Project

Best wishes ... cheers, drl

try xargs

xargs -n 10 < filename