Solved

Report formatting in NAWK....

Posted on 2004-10-05
2
302 Views
Last Modified: 2010-04-21
Hi,

I have a comma delimited file called lstname.rpt which is being brought into a nawk statement to generate a report. However, the report is not formatting very well.

#!/usr/bin/ksh

# Report generator for Last Name search results

nawk 'BEGIN {FS=","}
{print "Last Name \t First Name \t Depart City \t Arrival City \t Flight No \t Status \t Depart Date"}
{print "---------------------------------------------------------------------------------------------------"}
{print $1"\t",$2"\t",$3"\t",$4"\t",$5"\t",$6"\t\t",$7}' lstname.rpt

OUTPUT:

Last Name      First Name      Depart City      Arrival City      Flight No      Status      Depart Date
---------------------------------------------------------------------------------------------------
SMITH     BRAD     STL     DEN     147     DELAYED         2004/05/11
Last Name      First Name      Depart City      Arrival City      Flight No      Status      Depart Date
---------------------------------------------------------------------------------------------------
SMITH     DAVE     PIT     OAK     222     CANCELLED         2004/07/15
Last Name      First Name      Depart City      Arrival City      Flight No      Status      Depart Date
---------------------------------------------------------------------------------------------------
SMITH     ROD     DEN     LAX     214     ONTIME         2004/08/21
Would you like to do another search by Last Name:

How do I get report to look like:

Last Name     First Name     Depart City     Arrival City     Flight No     Status     Depart Date
--------------------------------------------------------------------------------------------------------
SMITH          BRAD             STL                DEN              147             OT         2004/05/11
SMITH          DAVE             PIT                 OAK              222             CA         2004/07/15


All fields are variable length and are dependant on what the user inputs.

Thanks,

Sam
0
Comment
Question by:elwayisgod
  • 2
2 Comments
 
LVL 23

Accepted Solution

by:
brettmjohnson earned 125 total points
ID: 12233608
Move the first two print statements to inside the BEGIN block so they are executed only
once at the beginning of the program.  As you have it now, they execute for every input line.


nawk 'BEGIN {FS=",";
  print "Last Name \t First Name \t Depart City \t Arrival City \t Flight No \t Status \t Depart Date";
  print "---------------------------------------------------------------------------------------------------"}
{print $1"\t",$2"\t",$3"\t",$4"\t",$5"\t",$6"\t\t",$7}' lstname.rpt



0
 
LVL 23

Expert Comment

by:brettmjohnson
ID: 12233627
I also suggest you use printf rather than print so you can get better formatting control.
As you have it, if anyone's name exceeds 8 characters, your tabs will throw the whole
line off.

0

Featured Post

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

778 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