From: peter sands on
Hello,
I am having problems using printf in presenting my data, in
that it is all over the place.
I am using the printf command:

printf "%-10s %10s %10 %40s\n" $cont $lock $set $time_cont

Where $cont will have a max of 8 chars, where $lock and $set will have
a max of 5 chars, and $time_cont is a time stamp)

I wish it to be printed like so, or at least evenly tabulated:

aplga_uk true true Wed Apr 1 09:35:08 BST 2009
bpa_uk false true Wed Jan 13 18:02:47 GMT 2010
voilrae_uk false false Thu Jan 14 11:48:41 GMT 2010
entret_uk false true Mon Aug 17 04:08:37 BST 2009

But when using the above printf all the fields are spread
across the screen, and does not look right..

Any help or advise on this please.

Thanks
Pete.
From: Glenn Jackman on
At 2010-01-14 01:42PM, "peter sands" wrote:
> Hello,
> I am having problems using printf in presenting my data, in
> that it is all over the place.
> I am using the printf command:
>
> printf "%-10s %10s %10 %40s\n" $cont $lock $set $time_cont

I see $time_cont has spaces, so you'd better treat it as a single
argument to printf:

printf "%-10s %10s %10 %40s\n" $cont $lock $set "$time_cont"

>
> Where $cont will have a max of 8 chars, where $lock and $set will have
> a max of 5 chars, and $time_cont is a time stamp)
>
> I wish it to be printed like so, or at least evenly tabulated:
>
> aplga_uk true true Wed Apr 1 09:35:08 BST 2009
> bpa_uk false true Wed Jan 13 18:02:47 GMT 2010
> voilrae_uk false false Thu Jan 14 11:48:41 GMT 2010
> entret_uk false true Mon Aug 17 04:08:37 BST 2009
>
> But when using the above printf all the fields are spread
> across the screen, and does not look right..
>
> Any help or advise on this please.
>
> Thanks
> Pete.


--
Glenn Jackman
Write a wise saying and your name will live forever. -- Anonymous
From: Seebs on
On 2010-01-14, peter sands <peter_sands(a)techemail.com> wrote:
> Hello,
> I am having problems using printf in presenting my data, in
> that it is all over the place.
> I am using the printf command:

> printf "%-10s %10s %10 %40s\n" $cont $lock $set $time_cont

You probably want "-" on all of these. The third seems to have no
format specifier; you probably want an s. Finally, be sure to quote
the arguments.

printf "%8s" $time_cont

ought to give you
"Wed Apr 1 09:35:09BST 2009 "

(with no newline).

The reason is that, if you provide extra arguments, printf cycles through
its argument string for them, and $time_cont appears to expand to multiple
words.

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / usenet-nospam(a)seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
From: johnb850 on
On Jan 14, 12:39 pm, Seebs <usenet-nos...(a)seebs.net> wrote:
> On 2010-01-14, peter sands <peter_sa...(a)techemail.com> wrote:
>
> > Hello,
> > I am having problems using printf in presenting my data, in
> > that it is all over the place.
> > I am using the printf command:
> > printf "%-10s %10s %10 %40s\n" $cont $lock $set $time_cont
>
> You probably want "-" on all of these.  The third seems to have no
> format specifier; you probably want an s.  Finally, be sure to quote
> the arguments.
>
> printf "%8s" $time_cont
>
> ought to give you
> "Wed     Apr     1       09:35:09BST     2009    "
>
> (with no newline).
>
> The reason is that, if you provide extra arguments, printf cycles through
> its argument string for them, and $time_cont appears to expand to multiple
> words.
>
> -s
> --
> Copyright 2010, all wrongs reversed.  Peter Seebach / usenet-nos...(a)seebs.nethttp://www.seebs.net/log/<-- lawsuits, religion, and funny pictureshttp://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!

I simple demo of the effect; printf, date and dbl quotes.
Mashed output;
printf "%s" `date`
ThuJan1412:45:40MST2010

Adding in some dbl quotes helps
printf "%s" "`date`"
Thu Jan 14 12:45:50 MST 2010

And trying out Peter's format string trick ...
printf "%4s" `date`

JB


From: peter sands on
Thanks everyone, the examples posted work great,
and I now printf wiser.

Thanks
pete.