21.07.2015 Views

GAWK: Effective AWK Programming

GAWK: Effective AWK Programming

GAWK: Effective AWK Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

300 <strong>G<strong>AWK</strong></strong>: <strong>Effective</strong> <strong>AWK</strong> <strong>Programming</strong>Appendix D Basic <strong>Programming</strong> ConceptsThis appendix attempts to define some of the basic concepts and terms that are usedthroughout the rest of this book. As this book is specifically about awk, and not aboutcomputer programming in general, the coverage here is by necessity fairly cursory andsimplistic. (If you need more background, there are many other introductory texts that youshould refer to instead.)D.1 What a Program DoesAt the most basic level, the job of a program is to process some input data and produceresults.DataProgramResultsThe “program” in the figure can be either a compiled program 1 (such as ls), or it maybe interpreted. In the latter case, a machine-executable program such as awk reads yourprogram, and then uses the instructions in your program to process the data.When you write a program, it usually consists of the following, very basic set of steps:InitializationMore Data?NoClean UpYesProcessInitializationThese are the things you do before actually starting to process data, such aschecking arguments, initializing any data you need to work with, and so on.This step corresponds to awk’s BEGIN rule (see Section 6.1.4 [The BEGIN andEND Special Patterns], page 99).If you were baking a cake, this might consist of laying out all the mixing bowlsand the baking pan, and making sure you have all the ingredients that youneed.Processing This is where the actual work is done. Your program reads data, one logicalchunk at a time, and processes it as appropriate.In most programming languages, you have to manually manage the readingof data, checking to see if there is more each time you read a chunk. awk’spattern-action paradigm (see Chapter 1 [Getting Started with awk], page 11)handles the mechanics of this for you.In baking a cake, the processing corresponds to the actual labor: breaking eggs,mixing the flour, water, and other ingredients, and then putting the cake intothe oven.1 Compiled programs are typically written in lower-level languages such as C, C++, Fortran, or Ada, andthen translated, or compiled, into a form that the computer can execute directly.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!