For Programmers: Free Programming Magazines  


Home > Archive > AWK > November 2006 > printf format ("%-{x}s","blerhg") where {x} is a variable









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author printf format ("%-{x}s","blerhg") where {x} is a variable
Mitch

2006-11-05, 6:56 pm

Hi there, I'm trying to format a printf statement. I can format a line
as in "%-10s" but for the table I am trying to produce, i would prefer
the number ten in that example to be variable.

I'll explain what it is I am trying to do and hopefully this will help,
or there will be an easier way...

Say I am using a username. usernames change in length depending on who
is the user. bob.geldof is shorter than fred.flinstone. I am trying to
print a table like the one below

bob.geldof data1 data2
1 1
2 4
3 9


fred.flinstone data1 data2
1 1
2 8
3 27


It looks bad like that, but these will only be printed one table at a time.

Is this possible? I can use a fixed table length, but I thought this
would look better in the long run, and I am curious as to whether it can
be done. Obviously i can do this easily when im printing the user name,
but when printing the second + lines, i need to dynamically say how wide
i need the first column to be.

I hope I've made that clear, and thanks!

Mitch.
Ed Morton

2006-11-05, 6:56 pm

Mitch wrote:

> Hi there, I'm trying to format a printf statement. I can format a line
> as in "%-10s" but for the table I am trying to produce, i would prefer
> the number ten in that example to be variable.


$ awk -v width=10 'BEGIN{fmt="%"width"s\n"; printf fmt, "stuff"}'
stuff
$ awk -v width=20 'BEGIN{fmt="%"width"s\n"; printf fmt, "stuff"}'
stuff

Regards,

Ed.
Janis Papanagnou

2006-11-05, 6:56 pm

Mitch wrote:
> Hi there, I'm trying to format a printf statement. I can format a line
> as in "%-10s" but for the table I am trying to produce, i would prefer
> the number ten in that example to be variable.


Subject: printf format ("%-{x}s","blerhg") where {x} is a variable

You can use string concatenation to build the format string...

printf("%-"number"s", whatever)

....where number is a variable.

Janis

>
> I'll explain what it is I am trying to do and hopefully this will help,
> or there will be an easier way...
>
> Say I am using a username. usernames change in length depending on who
> is the user. bob.geldof is shorter than fred.flinstone. I am trying to
> print a table like the one below
>
> bob.geldof data1 data2
> 1 1
> 2 4
> 3 9
>
>
> fred.flinstone data1 data2
> 1 1
> 2 8
> 3 27
>
>
> It looks bad like that, but these will only be printed one table at a time.
>
> Is this possible? I can use a fixed table length, but I thought this
> would look better in the long run, and I am curious as to whether it can
> be done. Obviously i can do this easily when im printing the user name,
> but when printing the second + lines, i need to dynamically say how wide
> i need the first column to be.
>
> I hope I've made that clear, and thanks!
>
> Mitch.

Kenny McCormack

2006-11-05, 6:56 pm

In article <c7n3h.166191$3D1.145567@fe3.news.blueyonder.co.uk>,
Mitch <spudtheimpaler@hotORgooMAIL.invalid> wrote:
>Hi there, I'm trying to format a printf statement. I can format a line
>as in "%-10s" but for the table I am trying to produce, i would prefer
>the number ten in that example to be variable.
>
>I'll explain what it is I am trying to do and hopefully this will help,
>or there will be an easier way...
>
>Say I am using a username. usernames change in length depending on who
>is the user. bob.geldof is shorter than fred.flinstone. I am trying to
>print a table like the one below
>
>bob.geldof data1 data2
> 1 1
> 2 4
> 3 9
>
>
>fred.flinstone data1 data2
> 1 1
> 2 8
> 3 27
>
>
>It looks bad like that, but these will only be printed one table at a time.


It sounds like what you are trying to do is have the length of the first
line (the header) determine the formatting of the remaining lines.

So, you could do something like this:

FNR == 1 { l = length($1)+5;print;next }
{ printf("%*d%5d\n",l,$1,$2) }

Note that others have suggested embedding the length in the string,
which is, in fact, the most natural way to do it. So, for variety's
sake, I've included the other way to do it.

Mitch

2006-11-05, 6:56 pm

Mitch wrote:
> Hi there, I'm trying to format a printf statement. I can format a line
> as in "%-10s" but for the table I am trying to produce, i would prefer
> the number ten in that example to be variable.
>
> I'll explain what it is I am trying to do and hopefully this will help,
> or there will be an easier way...
>
> Say I am using a username. usernames change in length depending on who
> is the user. bob.geldof is shorter than fred.flinstone. I am trying to
> print a table like the one below
>
> bob.geldof data1 data2
> 1 1
> 2 4
> 3 9
>
>
> fred.flinstone data1 data2
> 1 1
> 2 8
> 3 27
>
>
> It looks bad like that, but these will only be printed one table at a time.
>
> Is this possible? I can use a fixed table length, but I thought this
> would look better in the long run, and I am curious as to whether it can
> be done. Obviously i can do this easily when im printing the user name,
> but when printing the second + lines, i need to dynamically say how wide
> i need the first column to be.
>
> I hope I've made that clear, and thanks!
>
> Mitch.


Perfect. Thank you for helping a guy out, on a Sunday no less.

Cheers!

Mitch
Mitch

2006-11-05, 6:56 pm

Mitch wrote:
> Mitch wrote:
>
> Perfect. Thank you for helping a guy out, on a Sunday no less.
>
> Cheers!
>
> Mitch


lol, ok. I replied to my own thread to save replying to you each
individually, however as i didn't write "to you all" it looks like I'm
thanking no-one/myself.

Thank you all for your help! ;)

Mitch
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com