Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Formatting output into columns (ksh)

Posted on 2004-11-08
Medium Priority
Last Modified: 2013-12-26
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
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...

Question by:simulant
  • 3
  • 2
LVL 29

Expert Comment

ID: 12524615
Use awk and printf:

awk -F',' '{printf "%-10s%-10s%-30s%s\n",$1,$2,$3,$4;}' file1
LVL 16

Expert Comment

ID: 12524898
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.



Author Comment

ID: 12525073
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?
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 16

Accepted Solution

manav_mathur earned 500 total points
ID: 12525303

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

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


Author Comment

ID: 12525598
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.
LVL 16

Expert Comment

ID: 12531265
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.


Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question