← Full reference

column

Align text into a table, set headers, reorder and hide columns, output JSON

column from the util-linux package turns "ragged" text into a neat table: with -t it splits lines into fields and aligns them to the width of the longest value. Without -t it lays a list out in several columns, like ls. Handy at the end of a pipeline: mount | column -t, … | column -t -s, for CSV.

Syntax

column -t [options] [file…]       # table
column [-c width] [-x] [file…]    # list in several columns

Without files it reads standard input.

Options

Table mode

OptionWhat it does
-tbuild a table: split lines into fields and align the columns
-s CHARSinput field separators (any of the characters). Default — spaces and tabs, consecutive ones count as one separator
-o STRINGoutput column separator (default two spaces), e.g. -o ' | '
-l Nat most N columns: everything beyond stays in the last column as is
-Lkeep empty lines (dropped by default)

Headers and columns

The options below work with -t and refer to columns by name from -N or by number starting at 1.

OptionWhat it does
-N name1,name2,…set the column names and print them as a header line
-ddon't print the header (the names from -N are only needed to refer to columns)
-R columnsright-align these columns — handy for numbers
-H columnshide these columns
-O columnsoutput order of the columns, e.g. -O C,A
-T columnstruncate text in these columns if it doesn't fit
-W columnswrap text in these columns if it doesn't fit
-E columnsignore overly long values of these columns when computing widths
-Joutput the table as JSON: an array of objects with keys from -N
-n namethe array name in JSON (default table)

List mode (without -t)

OptionWhat it does
-c WIDTHoutput width in characters (default — the terminal width, 80 in a pipeline)
-xfill rows first, then columns (default — columns first)

Examples

printf 'name age city\nalice 30 Berlin\nbob 4 Paris\n' > people.txt

# a neat table
column -t people.txt

# CSV: comma separator, empty fields are kept
printf 'a,b,c\n1,,3\n' | column -t -s,

# custom output separator
printf 'a:b\n11:2\n' | column -t -s: -o ' | '

# add a header and right-align the numbers
printf 'x 1\nyyyy 1000\n' | column -t -N name,size -R size

# reorder and hide columns
printf '1 2 3\n4 5 6\n' | column -t -N A,B,C -O C,A -H B

# to JSON
printf 'x 1\ny 2\n' | column -t -N name,n -J -n items

# the first two fields are columns, the rest of the line stays as is
printf 'id 1 some long text\nid 22 short\n' | column -t -l 3

# a list in columns
seq 12 | column -c 30
seq 12 | column -c 30 -x

# /etc/passwd — readable
column -t -s: /etc/passwd | head -n 3

Pitfalls

  • Non-ASCII text in the C locale turns into escapes like caf\xc3\xa9 for café. The sandbox's default locale is C, and column escapes all non-ASCII bytes. Run it as LC_ALL=C.UTF-8 column -t — then both the text and the column widths come out right.
  • With -s every separator counts. Without -s several spaces in a row are one separator. With -s ' ' two spaces in a row produce an empty column and the table shifts. For space-aligned text, don't give -s.
  • CSV quotes aren't understood. a,"b,c",d with -s, splits into four fields: a, "b, c", d. For CSV with quotes you need a real parser, e.g. the csv module in python3.
  • Empty lines are dropped by default. If you need them as block separators, add -L.
  • -N doesn't take the header from the file, it adds a new one. If the first input line is already a header, you don't need -N — it gets aligned together with the data. And if you need names for -R or -H, cut off the old header (tail -n +2) and pass the same names to -N.
  • List mode uses tabs for alignment: when you copy into other programs the columns may "drift".
  • This is column from util-linux 2.39. The BSD version (macOS) and older util-linux lack many table-mode options (-N, -R, -J and others), so these recipes may not work there.

See also

paste · join · cut · awk · nl