You are not logged in.

#1 2010-10-03 01:29:44

egan
Member
From: Mountain View, CA
Registered: 2009-08-17
Posts: 273

Stretchy Space in Bash [SOLVED]

Forgive the strange title. I would like to know how to produce variable length spacing/tabbing in script output.
Static spacing or tabbing to separate columns of text doesn't work because it depends on the first column.

I can get:
word                    a description
longlongword                    a description

But I want:
word                    a description
longlongword       a description

Hopefully this makes sense despite the non-monospace fonts many people are probably using.
What I want to do is effectively the same as being able to define in what column in the terminal printing begins for a substring.

Last edited by egan (2010-10-03 02:09:57)

Offline

#2 2010-10-03 02:00:28

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: Stretchy Space in Bash [SOLVED]

printf can define a fixed column width. You also can't (and shouldn't) account for people using variable width fonts on a terminal. They're doing it wrong.

printf "%-20s %-s\n" "word" "some description" "a longer word" "and a description"

Offline

#3 2010-10-03 02:09:37

egan
Member
From: Mountain View, CA
Registered: 2009-08-17
Posts: 273

Re: Stretchy Space in Bash [SOLVED]

Ah, thanks. I didn't realize there was a printf program...

Offline

#4 2010-10-03 03:33:06

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: Stretchy Space in Bash [SOLVED]

In Bash, it's actually a builtin. However...

$ pacman -Qo $(type -P printf)
/usr/bin/printf is owned by coreutils 8.5-2

Offline

Board footer

Powered by FluxBB