100

I'd like to use the Unix column command to format some text. I have fields delimited by tabs, but within each field there are also spaces. column delimits on white space (tabs and spaces). How can I make column only use tabs as the delimiter?

I was trying to specify tab as the delimiter using:

cat myfile | column -t -s"\t"
Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
turtle
  • 2,697
  • 5
  • 20
  • 17

4 Answers4

145
column -t -s '\t'

would separate columns on \ and t characters.

column -s \t is the same as column -s 't', as the backslash is interpreted as a quoting operator by the shell.

Here you want to pass a real TAB character to column. With ksh93, zsh, bash, mksh, busybox sh or FreeBSD sh:

column -ts $'\t'

Or enter a real tab character by typing Ctrl-V Tab at the shell prompt (within quotes or preceded by a backslash as the tab character is a token separator in the shell syntax just like space), or use "$(printf '\t')" (those double quotes needed to disable the split+glob operator as the tab character also happens to be in the default value of $IFS).

  • 6
    I had to do column -t -s $'\t' as bash seemed to think '\t' mean both \ and t, but $'\t' means a literal tab. Bash stinks – ThorSummoner Jun 21 '16 at 22:11
  • If you need POSIX compliancy (lord help me), please see my answer, which is heavily based on this fantastic answer! – Nick Bull Sep 10 '18 at 14:49
  • 1
    This solution worked for me -- the $'\t' makes tab the delimiter. But I'm pretty sure I do awk -F "\t" to use a tab as a delimiter for awk. Why does that work and not here for column? – Mike Dec 07 '18 at 01:38
  • 1
    Can I suggest that your answer would be better if the top line were the solution with the explanation below it, rather than the top line being something that doesn't answer the question. – Mark Booth Apr 01 '22 at 12:48
  • THANK you! Good gods, I must have been at this for 20 minutes now! I didn't know the $'\t' syntax... great tip! – NerdyDeeds Dec 26 '22 at 01:42
20

For POSIX, $'...', as known as ANSI-C escaping, isn't defined.

Instead, you can use the POSIX $(printf '\t'):

column -t -s "$(printf '\t')"

$(printf '\011') could be used, as 011 (octal representation of decimal 9) is the ANSI code for a horizontal tab character:

column -t -s "$(printf '\011')"

However this is discouraged as it was commented under this and his answer by Stéphane Chazelas. This is because it may not be consistent across shell versions as POSIX doesn't specify what the encoding of TAB is. There are still POSIX systems whose C locale encoding is EBCDIC based where TAB is 5, not 9 like in ASCII. Wherever possible, it's better to refer to characters by name (\t here) to avoid this kind of issue. Note that $'...' is planned for inclusion in the next major version of the POSIX specification as of Sep 10, 2018.

Cadoiz
  • 276
Nick Bull
  • 563
  • 3
    Note that POSIX doesn't specify what the encoding of TAB is. There are still POSIX systems whose C locale encoding is EBCDIC based where TAB is 5, not 9 like in ASCII. Wherever possible, it's better to refer to characters by name to avoid this kind of issue like with "$(printf '\t')" as shown in my answer. Note that $'...' is planned for inclusion in the next major version of the POSIX specification. – Stéphane Chazelas Sep 10 '18 at 14:53
9

I used the following (only works if your text does not contain |):

cat myfile | tr '\t' '|' | column -t -s '|'

This just replaces tabs with pipes, then uses column with pipes as delimiters.

(I did this because I didn't see anything in Stéphane's answer that worked out-of-the-box in the fish shell. Otherwise, Stéphane's answer seems good.)

Cadoiz
  • 276
2

The -t is for selecting the number of columns you want. Leaving this blank does not change anything. Also, you want white space after the -s so try this out:

cat myfile | column -s \t

lurker
  • 327
  • Thanks. This is close to what I am looking for. However, now all the lines are merged onto one line. How can I keep each line on its own line? – turtle Nov 30 '12 at 10:09
  • By default, column fills rows before columns. You may be interested in pr – lurker Nov 30 '12 at 10:35
  • This wasn't true for me when using pactl list short sources | column -ts $'\t'. It doesn't work as expected without the t. – Cadoiz Nov 08 '22 at 07:32