mgo
OS/400 Edition
Volume 3, Number 2 -- January 15, 2003

Running Awk


Hey, Ted:

In the October 19, 2001 issue of Midrange Guru, in the article, "Running Perl On the AS/400 and iSeries," you showed us how to run Perl on the iSeries. Now, tell us how to run awk.


-- Linda


If you are at V5R1 or above and have PASE installed, you can run the AIX version of awk through Qshell. The PASE utilities are in directory /QOpenSys/usr/bin.

Linda, be aware of two important points:

  • Awk reads ASCII, not EBCDIC. Codepage 819 works well for input files.
  • Awk expects lines of a text file to be separated by linefeed characters, not the carriage return/linefeed sequence.

Here's an example. Suppose you have an ASCII file called phones.txt in the IFS. Each line of the text file contains a name, phone number, and extension. The fields are separated by a colon (:). Phone number and extension may be blank.

Bud::275               
Curly::290             
Larry:(555) 444-3333:  
Lou:(444) 333-2222:199 
Moe::235               
Oliver::145            
Stanley:(777) 888-9999:

If the records are delimited with a linefeed character, this awk command formats the data into columns and writes it to standard output (stdout).

/qopensys/bin/awk '
BEGIN {FS=":"} 
{printf "%-15s%-20s%-5s\n",$1,$2,$3}'  phones.txt

If records end with the carriage return/linefeed sequence, use sed to strip out the carriage returns.

sed 's/\015//' phones.txt | 
/qopensys/bin/awk '
BEGIN {FS=":"} 
{printf "%-15s%-20s%-5s\n",$1,$2,$3}'

Here's the output:

Bud                                275 
Curly                              290 
Larry          (555) 444-3333 
Lou            (444) 333-2222      199 
Moe                                235 
Oliver                             145 
Stanley        (777) 888-9999 

-- Ted


Sponsored By
SNAP-E BOOKS

Snap-eBooks is a new publishing company dedicated to the principle
that great products and tools do not have to cost a lot!

Snap-eBooks completely disagrees with the current industry practice that says that you should charge whatever the market will bear for AS/400 related products.

We believe that keeping the prices of our products low makes them accessible and affordable for everyone in our industry including programmers, operators, administrators, managers, and pretty much anyone else. After all, why should a book cost $89? At that price, only companies or corporations can afford them. Our low prices also mean that you can purchase however many eBooks you need, and get them when you need them.

Each eBook is full of step-by-step information that you can use to immediately learn a given topic in a short amount of time.

Download the Lightning-Guide eReader and then take a look at our sample eBook, called Introduction to Visual Age for RPG, and check it out for yourself just how useful our products really are.


THIS ISSUE
SPONSORED BY:

inFORM Decisions
Snap-E Books


BACK ISSUES

TABLE OF
CONTENTS

Dropping Procedures

Running Awk

Reader Feedback and Insights: More about Dumping IFS Files



Editors
Howard Arner
Joe Hertvik
Ted Holt
David Morris

Managing Editor
Mari Barrett

Publisher and
Advertising Director:

Jenny Thomas

Advertising Sales Representative
Kim Reed

Contact the Editors
Do you have a gripe, inside dope or an opinion?
Email the editors:
editors@itjungle.com



Copyright © 1996-2008 Guild Companies, Inc. All Rights Reserved.