Solved

Report formatting in NAWK....

Posted on 2004-10-05
2
314 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
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.:
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…
Suggested Courses

623 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