Code Comments
Programming Forum and web based access to our favorite programming groups.Hi folks. Quick question I'm sure someone can answer easily for me. I'm using awk to format several reports I run daily. Each report has numerous columns and the width of each column is currently fixed. However, I'd like to be able to adjust column width based on the largest value in each of the columns. Is there a way to do that? Thanks in advance! -- Link King king@kinger.net
Post Follow-up to this messageIn article <Pine.LNX.4.44.0402041603430.27086-100000@mogul.kinger.net>, Link King <king@kinger.net> wrote: > Hi folks. > > Quick question I'm sure someone can answer easily for me. I'm using awk > to format several reports I run daily. Each report has numerous columns > and the width of each column is currently fixed. However, I'd like to be > able to adjust column width based on the largest value in each of the > columns. Is there a way to do that? Sure. However, you pretty much have to scan the file twice. First you have to determine the widest columns. Then you have to feed that info to the report generator. Depending on the size of the report, you might be able to load it into an array rather than scan it, but I can't think of any way to determine the widest columns without looking at the whole file first and then printing it. -- Robert B. Peirce, Venetia, PA 724-941-6883 bob AT peirce-family.com [Mac] rbp AT cooksonpeirce.com [Office]
Post Follow-up to this messageHere should be enough information to get you started. It's a
one-column, untested sample.
{
if (length($1) > max_width} max_width = length($1)
x[NR] = $1
}
END {
fmt = "%" max_width "d\n" # So fmt = "%12d\n" or whatever
for (i = 1; i <= NR; i++) printf(fmt, x[i])
}
If you use GAWK and perhaps some other AWK's, you can replace my
printf statement with
printf("%*d\n", max_width, x[i])
***
As someone else mentioned, you might have to read the file twice if
you can't store everything in an array. But it's possible that
although you have a huge input file, maybe you don't print but a
fraction of the file. In that case, an array might work fine.
***
Also, I just realized that if you don't have to use the minimum
maximum field length, you can just use a large field width that you
are certain could work. So my printf would then become
printf("%20d\n", x[i])
DKM
On Wed, 4 Feb 2004 16:06:07 -0700, Link King <king@kinger.net> wrote:
>
>Hi folks.
>
>Quick question I'm sure someone can answer easily for me. I'm using awk
>to format several reports I run daily. Each report has numerous columns
>and the width of each column is currently fixed. However, I'd like to be
>able to adjust column width based on the largest value in each of the
>columns. Is there a way to do that?
>
>Thanks in advance!
To contact me directly, send EMAIL to (single letters all)
DEE_KAY_EMM AT EarthLink.net
Post Follow-up to this messageIn article <Pine.LNX.4.44.0402041603430.27086-100000@mogul.kinger.net>, Link King <king@kinger.net> wrote: % Quick question I'm sure someone can answer easily for me. I'm using awk % to format several reports I run daily. This is not what you're asking, but consider using troff and tbl to do this. Your awk program spits out a load of gibberish like this .TS H c c c l l n
Post Follow-up to this message
Worked like a charm. The reports are generally chopped at about 100 rows
per so reading the file twice isn't a huge issue. Thanks for the pointer!
-Link
> Here should be enough information to get you started. It's a
> one-column, untested sample.
>
> {
> if (length($1) > max_width} max_width = length($1)
> x[NR] = $1
> }
>
> END {
> fmt = "%" max_width "d\n" # So fmt = "%12d\n" or whatever
>
> for (i = 1; i <= NR; i++) printf(fmt, x[i])
> }
>
> If you use GAWK and perhaps some other AWK's, you can replace my
> printf statement with
>
> printf("%*d\n", max_width, x[i])
>
> ***
>
> As someone else mentioned, you might have to read the file twice if
> you can't store everything in an array. But it's possible that
> although you have a huge input file, maybe you don't print but a
> fraction of the file. In that case, an array might work fine.
>
> ***
>
> Also, I just realized that if you don't have to use the minimum
> maximum field length, you can just use a large field width that you
> are certain could work. So my printf would then become
>
> printf("%20d\n", x[i])
>
> DKM
>
>
> On Wed, 4 Feb 2004 16:06:07 -0700, Link King <king@kinger.net> wrote:
>
>
>
> To contact me directly, send EMAIL to (single letters all)
> DEE_KAY_EMM AT EarthLink.net
>
--
Link King
king@kinger.net
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.