Formatting output into columns (ksh)

Posted on 2004-11-08
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 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
    LVL 29

    Expert Comment

    Use awk and printf:

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

    Expert Comment

    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

    Manav, 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?
    LVL 16

    Accepted Solution


    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

    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

    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

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
    Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
    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.
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    734 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

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now