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
| Option | What it does |
|---|---|
-t | build a table: split lines into fields and align the columns |
-s CHARS | input field separators (any of the characters). Default — spaces and tabs, consecutive ones count as one separator |
-o STRING | output column separator (default two spaces), e.g. -o ' | ' |
-l N | at most N columns: everything beyond stays in the last column as is |
-L | keep 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.
| Option | What it does |
|---|---|
-N name1,name2,… | set the column names and print them as a header line |
-d | don't print the header (the names from -N are only needed to refer to columns) |
-R columns | right-align these columns — handy for numbers |
-H columns | hide these columns |
-O columns | output order of the columns, e.g. -O C,A |
-T columns | truncate text in these columns if it doesn't fit |
-W columns | wrap text in these columns if it doesn't fit |
-E columns | ignore overly long values of these columns when computing widths |
-J | output the table as JSON: an array of objects with keys from -N |
-n name | the array name in JSON (default table) |
List mode (without -t)
| Option | What it does |
|---|---|
-c WIDTH | output width in characters (default — the terminal width, 80 in a pipeline) |
-x | fill 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
Clocale turns into escapes likecaf\xc3\xa9forcafé. The sandbox's default locale isC, andcolumnescapes all non-ASCII bytes. Run it asLC_ALL=C.UTF-8 column -t— then both the text and the column widths come out right. - With
-severy separator counts. Without-sseveral 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",dwith-s,splits into four fields:a,"b,c",d. For CSV with quotes you need a real parser, e.g. thecsvmodule in python3. - Empty lines are dropped by default. If you need them as block separators, add
-L. -Ndoesn'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-Ror-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
columnfrom util-linux 2.39. The BSD version (macOS) and older util-linux lack many table-mode options (-N,-R,-Jand others), so these recipes may not work there.