Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I align the columns of a space separated table in Bash? [duplicate]

Tags:

linux

bash

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.

like image 379
Richard Avatar asked Jul 06 '11 20:07

Richard


People also ask

How do I align a column in a CSV file?

In the Format Cells dialog box that appears, go to the Number tab and select one of the Date options under Category. Then, click on the OK button. Once you have selected how you want your columns to be aligned, you can click on the Finish button in the Text Import Wizard or close out of the Format Cells dialog box.

How do you align columns?

On the Home tab, click Paragraph, and then click Align. Select the Align with option and then select the paragraph tag pertaining to the column one paragraph. Click OK.

What is whitespace in Bash?

Whitespace — this is a tab, newline, vertical tab, form feed, carriage return, or space. Bash uses whitespace to determine where words begin and end. The first word is the command name and additional words become arguments to that command.


1 Answers

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.

like image 155
Michael Berkowski Avatar answered Oct 04 '22 14:10

Michael Berkowski