46

I have a file with an arbitrary number of non-aligned columns separated with whitespace.

I would like to align the columns of the file.

I've looked at the col command, and it doesn't seem appropriate.

I could write an AWK script, but it seems like a more obvious command should exist.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Richard
  • 50,293
  • 28
  • 163
  • 235
  • 1
    @CiroSantilli新疆改造中心六四事件法轮功: It's more like that question is a duplicate of mine, since it was asked more than a year later. But it does have helped information - thanks for making the association! – Richard Sep 06 '18 at 18:42
  • 1
    Hey Richard, the current consensus is to close by "quality": http://meta.stackexchange.com/questions/147643/should-i-vote-to-close-a-duplicate-question-even-though-its-much-newer-and-ha Since "quality" is not measurable, I just go by upvotes. ;-) Likely it comes down to which question hit the best newbie Google keywords on the title. It is sometimes random :-( – Ciro Santilli Путлер Капут 六四事 Sep 06 '18 at 18:56

1 Answers1

82

You might want the column command, usually with --table / -t to produce basic tabular output:

From the man page:

 -t, --table 

Determine the number of columns the input contains and create a table. Columns are delimited with whitespace, by default, or with the charac‐ters supplied using the --output-separator option. Table output is useful for pretty-printing.

column -t [file]

# or from stdin
cat file | column -t

# For a quick demonstration, format the output of mount
mount | column -t

column has a lot of other complex options. man column for details.

Michael Berkowski
  • 260,803
  • 45
  • 432
  • 377
  • 5
    Note: If you don't want to split on all whitespace, add the additional argument `-s`. Eg to split on tabs `column -t -s$'\t'` Note the use of single quotes and the `$`, both are necessary to get `\t` to be recognized as a tab. – Cole Apr 29 '20 at 03:31
  • Been using terminals for a long time and I just learned about `column` today. Thank you! – dimitarvp May 18 '22 at 13:57