Rhys Ulerich
Rhys Ulerich

Reputation: 1332

Can I change how many spaces 'column -t' outputs between columns?

column -t is amazing with one nit: How can I change how many spaces are output between columns? I want one. column -t gives two. For example,

echo -en '111 22 3\n4 555 66\n' | column -t

outputs

111  22   3
4    555  66

but I would like it to output the following:

111 22  3
4   555 66

I think I could run the output through sed regex to turn two spaces followed by a word boundary into a single space, but I'd like to avoid adding another tool to the mix unless its necessary.

Suggestions? Simple replacement commands that I could use instead of column -t which accomplish the same thing? Playing OFS games with awk doesn't seem like a drop-in replacement.

Upvotes: 3

Views: 561

Answers (4)

OldBuildingAndLoan
OldBuildingAndLoan

Reputation: 3022

column now has an option to define that spacing.

column --help
  -o, --output-separator <string>  columns separator for table output (default is two spaces)

single space delimited

echo -en '111 22 3\n4 555 66\n' | column -t -o ' '
111 22  3
4   555 66

and pipe delimited

echo -en '111 22 3\n4 555 66\n' | column -t -o '|'
111|22 |3
4  |555|66

Upvotes: 0

Steve
Steve

Reputation: 54392

I think the simplest way is to use tabs. In your output you are expecting a tab number of 5. So, in terminal type: tabs 5. This will change the tab-width to 5.

Then type:

echo -en '111 22 3\n4 555 66\n' | tr ' ' '\t'

or:

echo -en '111\t22\t3\n4\t555\t66\n'

Results:

111 22  3
4   555 66

For more info, type: man tabs

Upvotes: 0

twalberg
twalberg

Reputation: 62379

Switching from column to pr might be a good idea. pr has a whole lot more options for controlling output formatting, and can create columns as well...

For example:

echo -en '111 22 3\n4 555 66\n' | tr ' ' '\n' | pr -3aT -s' '

produces:

111 22 3
4 555 66

Not sure how to keep the alignment while still reducing the spaces, so it's not perfect.

The tr is in there because pr expects each entry on a single line.

Upvotes: 0

mavam
mavam

Reputation: 12552

You cannot change the builtin spacing of column. This leaves you with either switching to a different tool or post-processing. You can accomplish the ladder cheaply with sed to remove a single space before each number:

echo -en '111 22 3\n4 555 66\n' | column -t | sed 's/ \([0-9]\)/\1/g'

Output:

111 22  3
4   555 66

Upvotes: 2

Related Questions