From: Sidney Lambe on
On comp.unix.shell, Janis Papanagnou <janis_papanagnou(a)hotmail.com> wrote:
> 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 "".

That's really helpful. I'm saving your post to /usr/doc/printf.

>
>> The documentation for bash builtin printf and /bin/printf in
>> linux are pathetic. The manpage says to see the info manual
>
> 'info' pages?! - *shudder*

Ha! I hate that that stupid info app and reader and don't even
install it. If I want the manual I go to gnu.org and pick up a
text or html version. Got my own 'info' app: A shell script
menu aliased to "doc".

a) edit script
b) software
c) shellscript
d) netcat
e) proc
f) links
g) vi
h) sed
i) VM
j) nag
k) textools
l) print1
m) print2
n) rute
o) screen
p) slrn
q) slrn score
r) wget
s) find

[delete]

Sid


From: Ed Morton on
On 3/28/2010 2:42 PM, Sidney Lambe wrote:
> 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.

The awk solution to this problem is better in every way than the strictly shell
alternative. I apologize if it makes me a fanatic for suggesting you consider it.

> 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

Sed and awk are not mutually exclusive. You don't have to pick one for all jobs,
just use whichever is appropriate for the job at hand.

I would be interested in seeing the sed solution to this problem though, since
you seem to be suggesting that sed would be a reasonable choice of tool for this
job.

Ed.
First  |  Prev  | 
Pages: 1 2
Prev: xgawk script for display xml tags and data
Next: shell