Formatting output into columns (ksh)

I'm writing a ksh script, and I want to be able to print out a report in a nice format with columns spaced out appropriately.  Say I have this text:
% cat file1
INST,PID,PROCESS,DESCRIPTION
peopdb2,112324,process1 arg1 arg2,Process1 Description
peopdb2,112425,process2 arg1,Process2 Description

...I want to be able to produce a report where the columns are spaced out evenly based on the length of the actual row values (I hope this came out ok on the post here):

INST        PID        PROCESS                DESCRIPTION
peopdb2   112324  process1 arg1 arg2  Process1 Description
peopdb2   112425  process2 arg1          Process2 Description


...I've been experimenting with the pr command in order to do this....is this the right approach??  I could also use custom tab-spacing, but I want to make sure I'm not overlooking a more natural solution...

Thanks.
TIM
simulantAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MikeOM_DBACommented:
Use awk and printf:

awk -F',' '{printf "%-10s%-10s%-30s%s\n",$1,$2,$3,$4;}' file1
0
manav_mathurCommented:
MikeOM_DBA suggestion is fine other than the statement I read in your question
>columns are spaced out evenly based on the length of the 'actual row values'

In case you want to make your spacing dynamic(based on actual row values),
it will involve two-passes thru your whole data. The first pass to get the max-width
for each field, and the second pass to actually display those values.

- If your data is in a file, make a two-pass arrangement although it
will be additional overhead.

- In case of output data, the logic gets more complex. you need to capture
 data somewhere (maybe a file/named pipe, but that would be way too complex)
 for such a single operation.

Manav

0
simulantAuthor Commented:
Manav, thanks....you are right about making two passes through the data, since the max-width of the row values determines the actual column width.  Yes, I could just use generic column widths (per MikeOM_DBA's suggestion), but then I run the risk of outputting an ugly report if the input is longer than expected.

I'm hoping that there is a Unix utility that will determine this logic for me, so I don't have to implement this myself....I hate reinventing the wheel.  Can the pr command do this for me?  Or maybe one of the *roff commands?
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

manav_mathurCommented:
Simulant,

With *roff functionality, you need to put special commands into your document itself.
The problem's here again, to put a specific command somewhere in the , let us say, 4 th record
you again need to know the lengths of all fields in the 5th column.

Actually, the logic is pretty simple.
You can get the length of any string/numeric/alphanumeric using

len=`expr "$s" : '[a-zA-Z0-9]*'`

where s is your input string/numeric/alphanumeric

So,
- initialize len variables for each field to zero.
- while you are calculating/getting the value of each field, , calculate length of each field using the above method.
- if it is greater than value stored in the len variable, update the len variable

Inthe end, you'll have largest field sizes in coresponding len variables.

Manav
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
simulantAuthor Commented:
Manav,
Thanks for the input.  I suppose I will implement this myself.  By the way, korn shell comes with a nice syntax for determining variable lengths:  ${#var} will tell you the length of var.
0
manav_mathurCommented:
Yeah simulant,
I suppose that would suit your purpose pretty well and I should've suggested you that one.

The advantage with using expr is if you want to count only the number of characters, or only the number of digits, or a combination of spaces and strings.....and so on.

For saving rework of my scripts, I tend to use expr. That way, even if a change comes, I dont have much of a problem.

Manav
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.