Solved

Report formatting in NAWK....

Posted on 2004-10-05
2
300 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

937 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

11 Experts available now in Live!

Get 1:1 Help Now