sid_com
sid_com

Reputation: 25117

How do I format a file in 4 columns?

cat file  
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16

I tried with

column -c 4 file 

to get an output with 4 columns, but it didn't work - I just get the same as the input.
Do I misunderstand the column man-page?

A second question: what format should the argument to the -s flag have?

Upvotes: 1

Views: 888

Answers (2)

Dennis Williamson
Dennis Williamson

Reputation: 360143

Give this a try:

fold -w 12 file

The number 12 is the number of data columns * the number of characters in a column (two digits + one space). The -w option is for designating a screen width in terms of character columns.

The column command won't work for this because it's intended to format newspaper-style columns.

This comes close to working the way you want:

sed 's/ /\n/g' file | column -xc 35

The "35" is somewhat arbitrary, but any value from 32 to 39 will work in this case. It's related to the width of the fields (2 characters which is less than the width of a tab stop), the number of fields desired per line and the width of tab stops (8 characters). So, basically, 8 * 4 is 32.

Here's a demonstration of the -s option (which is used with -t):

$ echo -e "a;b|c\naaaaa;bbbbb|ccccc"|column -t -s ';|'
a      b      c
aaaaa  bbbbb  ccccc

Without using column, the output looks like:

$ echo -e "a;b|c\naaaaa;bbbbb|ccccc"
a;b|c
aaaaa;bbbbb|ccccc

Upvotes: 2

tokland
tokland

Reputation: 67870

Let's guess you want:

01 02 03 04
05 06 07 08
09 10 11 12
13 14 15 16

In this case:

$ xargs -n4 < file

Upvotes: 1

Related Questions