Solved

Report formatting in NAWK....

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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

Hello fellow BSD lovers, I've created a patch process for patching openjdk6 for BSD (FreeBSD specifically), although I tried to keep all BSD versions in mind when creating my patch. Welcome to OpenJDK6 on BSD First let me start with a little …
In tuning file systems on the Solaris Operating System, changing some parameters of a file system usually destroys the data on it. For instance, changing the cache segment block size in the volume of a T3 requires that you delete the existing volu…
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…
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.

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