Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting lists with the column command in *nix

I'm trying to format a list of entries in bash, and am using the column command. However, the -t option defaults to using any whitespace as a delimiter, which does not work for the data I have (it contains spaces and tabs). I can't figure out how to get the -s flag to specify a newline character as the sole column delimiter.

like image 894
Doug Powers Avatar asked Aug 26 '12 19:08

Doug Powers


People also ask

What is the command of column?

column command in Linux is used to display the contents of a file in columns. The input may be taken from the standard input or from the file. This command basically breaks the input into multiple columns. Rows are filled before columns.

Which command is used to format text in columns?

Select the text you want to format. Select the Page Layout tab, then click the Columns command. A drop-down menu will appear. Select the number of columns you want to create.

Which utility takes input from another command and formats output into columns?

It is very simple and easy to use command line utility. This command line utility converts the input file into multiple columns and you can convert the content into the columns based on any delimiter.

How do you create columns in Unix?

Specify a set of characters to be used to delimit columns for the -t option. Determine the number of columns the input contains and create a table. Columns are delimited with whitespace, by default, or with the characters supplied using the -s option. Useful for pretty-printing displays.


2 Answers

In theory, to specify a newline, you can use the $'...' notation, which is just like '...' except that it supports C-style escape-sequences:

column -t -s $'\n' list-of-entries.txt

However, I don't really understand the purpose of this. A newline is the row delimiter, so a column-delimiter of $'\n' is equivalent to not having any column-delimiter at all:

column -t -s '' list-of-entries.txt

which means that the input will be treated as having only one column; so it's equivalent to not using column at all:

cat list-of-entries.txt

It seems like you actually don't want to use the -t flag, because the purpose of the -t flag is to ensure that each line of input becomes one line of output, and it doesn't sound like that's what you want. I'm guessing you want this:

column list-of-entries.txt

which will treat each line of list-of-entries.txt as a value to be put in one cell of the table that column outputs.

like image 72
ruakh Avatar answered Oct 16 '22 15:10

ruakh


This works to output a pretty print version of a tab delimited file

column -t -s $'\t' list-of-entries.txt
like image 39
Etienne Lawlor Avatar answered Oct 16 '22 14:10

Etienne Lawlor