Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Column width
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


Report this thread to moderator Post Follow-up to this message
Old Post
Link King
03-20-04 01:23 AM


Re: Column width
In 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]


Report this thread to moderator Post Follow-up to this message
Old Post
Robert Peirce
03-20-04 01:23 AM


Re: Column width
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:

>
>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

Report this thread to moderator Post Follow-up to this message
Old Post
Doug McClure
03-20-04 01:23 AM


Re: Column width
In 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

Report this thread to moderator Post Follow-up to this message
Old Post
Patrick TJ McPhee
03-20-04 01:23 AM


Re: Column width
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


Report this thread to moderator Post Follow-up to this message
Old Post
Link King
03-20-04 01:23 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

AWK archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 04:54 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.