From: Sidney Lambe on
I've learned how to make nice neat colums/tables with printf.

var1="gem"
var2="frenchbread"
var3="z"
printf "%-5.3s%-5.3s%-5.3s\n" $var1 $var2 $var3
gem fre z

var1="pp"
var2="all"
var3="jamboree"
printf "%-5.3s%-5.3s%-5.3s\n" $var1 $var2 $var3
pp all jam

var1="toe"
var2="lettuce"
var3="rock"
printf "%-5.3s%-5.3s%-5.3s\n" $var1 $var2 $var3
toe let roc

gem fre z
pp all jam
toe let roc

So how do I position that entire table precisely? I can
mickey mouse it with with something like this:

printf " %-5.3s%-5.3s%-5.3s\n" $var1 $var2 $var3

But it seems to me that there must be a way to make the line:
"%-5.3s%-5.3s%-5.3s\n" a sub-expression (?) and. say. right
justify the whole line on the page as if the line was a
single 'column' defined by something like "%-72.15".

(I hope I am expressing that clearly.)

I've tried a bunch of things with parenthesis and seen no
results but a lot of error messages from printf. Lately,
they've read:

printf: --: invalid option
printf: usage: printf format [arguments]
printf: you are a dummy -- give it up

Sid

From: Janis Papanagnou on
Sidney Lambe wrote:
> I've learned how to make nice neat colums/tables with printf.
>
> var1="gem"
> var2="frenchbread"
> var3="z"
> printf "%-5.3s%-5.3s%-5.3s\n" $var1 $var2 $var3
> gem fre z
>
> var1="pp"
> var2="all"
> var3="jamboree"
> printf "%-5.3s%-5.3s%-5.3s\n" $var1 $var2 $var3
> pp all jam
>
> var1="toe"
> var2="lettuce"
> var3="rock"
> printf "%-5.3s%-5.3s%-5.3s\n" $var1 $var2 $var3
> toe let roc
>
> gem fre z
> pp all jam
> toe let roc
>
> So how do I position that entire table precisely? I can
> mickey mouse it with with something like this:
>
> printf " %-5.3s%-5.3s%-5.3s\n" $var1 $var2 $var3
>
> But it seems to me that there must be a way to make the line:
> "%-5.3s%-5.3s%-5.3s\n" a sub-expression (?) and. say. right
> justify the whole line on the page as if the line was a
> single 'column' defined by something like "%-72.15".
>
> (I hope I am expressing that clearly.)

For the simple case I'd do

indent=20
printf "%*s%s\t%s\n" "$indent" "" Arg1 Arg2

If you want to align it at the center of the screen with given width
in variable COLUMNS you can do

output=$( printf "%s %s" Arg1 Arg2 )

and use $COLUMNS to calculate and align the data in 'output' using a
second printf

printf "%*s%s\n" $(( ($COLUMNS - ${#output}) / 2 )) "" "$output"


Janis

>
> I've tried a bunch of things with parenthesis and seen no
> results but a lot of error messages from printf. Lately,
> they've read:
>
> printf: --: invalid option
> printf: usage: printf format [arguments]
> printf: you are a dummy -- give it up
>
> Sid
>
From: Ed Morton on
On 3/27/2010 9:28 PM, Sidney Lambe wrote:
> I've learned how to make nice neat colums/tables with printf.
>
> var1="gem"
> var2="frenchbread"
> var3="z"
> printf "%-5.3s%-5.3s%-5.3s\n" $var1 $var2 $var3
> gem fre z
>
> var1="pp"
> var2="all"
> var3="jamboree"
> printf "%-5.3s%-5.3s%-5.3s\n" $var1 $var2 $var3
> pp all jam
>
> var1="toe"
> var2="lettuce"
> var3="rock"
> printf "%-5.3s%-5.3s%-5.3s\n" $var1 $var2 $var3
> toe let roc
>
> gem fre z
> pp all jam
> toe let roc
>
> So how do I position that entire table precisely? I can
> mickey mouse it with with something like this:
>
> printf " %-5.3s%-5.3s%-5.3s\n" $var1 $var2 $var3
>
> But it seems to me that there must be a way to make the line:
> "%-5.3s%-5.3s%-5.3s\n" a sub-expression (?) and. say. right
> justify the whole line on the page as if the line was a
> single 'column' defined by something like "%-72.15".
>
> (I hope I am expressing that clearly.)
>
> I've tried a bunch of things with parenthesis and seen no
> results but a lot of error messages from printf. Lately,
> they've read:
>
> printf: --: invalid option
> printf: usage: printf format [arguments]
> printf: you are a dummy -- give it up
>
> Sid
>

Use a text processing tool like awk instead of shell:

$ cat file
gem frenchbread z
pp all jamboree
toe lettuce rock

$ awk '{printf "%-5.3s%-5.3s%-5.3s\n",$1,$2,$3}' file
gem fre z
pp all jam
toe let roc

$ awk '{printf "%32.15s\n",sprintf("%-5.3s%-5.3s%-5.3s\n",$1,$2,$3)}' file
gem fre z
pp all jam
toe let roc

$ awk -v var1="gem" -v var2="frenchbread" -v var3="z" '
BEGIN{printf "%32.15s\n",sprintf("%-5.3s%-5.3s%-5.3s\n",var1,var2,var3); exit}'
gem fre z

Regards,

Ed.
From: Sidney Lambe on
On comp.unix.shell, Ed Morton <mortonspam(a)gmail.com> wrote:
> On 3/27/2010 9:28 PM, Sidney Lambe wrote:
>> I've learned how to make nice neat colums/tables with printf.
>>
>> var1="gem"
>> var2="frenchbread"
>> var3="z"
>> printf "%-5.3s%-5.3s%-5.3s\n" $var1 $var2 $var3
>> gem fre z
>>
>> var1="pp"
>> var2="all"
>> var3="jamboree"
>> printf "%-5.3s%-5.3s%-5.3s\n" $var1 $var2 $var3
>> pp all jam
>>
>> var1="toe"
>> var2="lettuce"
>> var3="rock"
>> printf "%-5.3s%-5.3s%-5.3s\n" $var1 $var2 $var3
>> toe let roc
>>
>> gem fre z
>> pp all jam
>> toe let roc
>>
>> So how do I position that entire table precisely? I can
>> mickey mouse it with with something like this:
>>
>> printf " %-5.3s%-5.3s%-5.3s\n" $var1 $var2 $var3
>>
>> But it seems to me that there must be a way to make the line:
>> "%-5.3s%-5.3s%-5.3s\n" a sub-expression (?) and. say. right
>> justify the whole line on the page as if the line was a
>> single 'column' defined by something like "%-72.15".
>>
>> (I hope I am expressing that clearly.)
>>
>> I've tried a bunch of things with parenthesis and seen no
>> results but a lot of error messages from printf. Lately,
>> they've read:
>>
>> printf: --: invalid option
>> printf: usage: printf format [arguments]
>> printf: you are a dummy -- give it up
>>
>> Sid
>>
>
> Use a text processing tool like awk instead of shell:
>
> $ cat file
> gem frenchbread z
> pp all jamboree
> toe lettuce rock
>
> $ awk '{printf "%-5.3s%-5.3s%-5.3s\n",$1,$2,$3}' file
> gem fre z
> pp all jam
> toe let roc
>
> $ awk '{printf "%32.15s\n",sprintf("%-5.3s%-5.3s%-5.3s\n",$1,$2,$3)}' file
> gem fre z
> pp all jam
> toe let roc
>
> $ awk -v var1="gem" -v var2="frenchbread" -v var3="z" '
> BEGIN{printf "%32.15s\n",sprintf("%-5.3s%-5.3s%-5.3s\n",var1,var2,var3); exit}'
> gem fre z
>
> Regards,
>
> Ed.

An awk is a dumb bird. I'll bet an awk would answer a printf
question with an awk solution.

You are an awk fanatic, Ed. Your responses to questions about
text formatting are not to be trusted.

Tunnel vision is awk(ward). I prefer sed to awk, as you well
know. If you don't like it you can go eat some awk doo.

Sid


From: Janis Papanagnou on
Sidney Lambe wrote:
> On comp.unix.shell, Janis Papanagnou <janis_papanagnou(a)hotmail.com> wrote:
>> Sidney Lambe wrote:
>>> I've learned how to make nice neat colums/tables with printf.
>>>
>>> var1="gem"
>>> var2="frenchbread"
>>> var3="z"
>>> printf "%-5.3s%-5.3s%-5.3s\n" $var1 $var2 $var3
>>> gem fre z
>>>
>>> var1="pp"
>>> var2="all"
>>> var3="jamboree"
>>> printf "%-5.3s%-5.3s%-5.3s\n" $var1 $var2 $var3
>>> pp all jam
>>>
>>> var1="toe"
>>> var2="lettuce"
>>> var3="rock"
>>> printf "%-5.3s%-5.3s%-5.3s\n" $var1 $var2 $var3
>>> toe let roc
>>>
>>> gem fre z
>>> pp all jam
>>> toe let roc
>>>
>>> So how do I position that entire table precisely? I can
>>> mickey mouse it with with something like this:
>>>
>>> printf " %-5.3s%-5.3s%-5.3s\n" $var1 $var2 $var3
>>>
>>> But it seems to me that there must be a way to make the line:
>>> "%-5.3s%-5.3s%-5.3s\n" a sub-expression (?) and. say. right
>>> justify the whole line on the page as if the line was a
>>> single 'column' defined by something like "%-72.15".
>>>
>>> (I hope I am expressing that clearly.)
>> For the simple case I'd do
>>
>> indent=20
>> printf "%*s%s\t%s\n" "$indent" "" Arg1 Arg2
>>
>> If you want to align it at the center of the screen with given width
>> in variable COLUMNS you can do
>>
>> output=$( printf "%s %s" Arg1 Arg2 )
>>
>> and use $COLUMNS to calculate and align the data in 'output' using a
>> second printf
>>
>> printf "%*s%s\n" $(( ($COLUMNS - ${#output}) / 2 )) "" "$output"
>>
>>
>> Janis
>>
>
> Thanks, Janis. That really put me through my paces. I had
> to find an online C Reference Manual to figure out what
> that "*" meant. And I'm still not clear about the "". Is
> that a 'null string'?

Yes, it is. Just an (invisible) argument to satisfy the %*s. You could
as well output a blank " " instead. It's not immediately clear that %*s
requires two arguments; the first to replace the * by a number, and the
second is the actual argument to be displayed. Since what we want here
is just a (kind of invisible) padding I've choosen an empty string "".

>
> The documentation for bash builtin printf and /bin/printf in
> linux are pathetic. The manpage says to see the info manual

'info' pages?! - *shudder*

Janis

> for the full documentation, but there isn't an info manual for
> printf. I checked at gnu.org and even used google to search the
> entire sit.
>
> Sid
>
 |  Next  |  Last
Pages: 1 2
Prev: xgawk script for display xml tags and data
Next: shell