Solved

Report formatting in NAWK....

Posted on 2004-10-05
2
297 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
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

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…
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 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 get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

771 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

10 Experts available now in Live!

Get 1:1 Help Now