Awk - A Pattern Scanning and Processing Language

Peter Kitson

ISBN : -

Order a printed copy of this book from Amazon --UNAVAILABLE--

Cover Design - Awk - A Pattern Scanning and Processing Language

For your free electronic copy of this book please verify the numbers below. 

(We need to do this to make sure you're a person and not a malicious script)



Sample Chapter From Awk - A Pattern Scanning and Processing Language
     Copyright © Alfred V. Aho, Peter J. Weinberger, Brian W. Kernighan


Awk is a programming language whose basic operation is to search a set of files for patterns, and to
perform specified actions upon lines or fields of lines which contain instances of those patterns. Awk makes
certain data selection and transformation operations easy to express; for example, the awk program

length > 72

prints all input lines whose length exceeds 72 characters; the program

NF % 2 == 0

prints all lines with an even number of fields; and the program

{ $1 = log($1); print }

replaces the first field of each line by its logarithm.

Awk patterns may include arbitrary boolean combinations of regular expressions and of relational operators
on strings, numbers, fields, variables, and array elements. Actions may include the same pattern-matching
constructions as in patterns, as well as arithmetic and string expressions and assignments, if-else, while,
for statements, and multiple output

1. Introduction

Awk is a programming language designed to make many common information retrieval and text manipulation tasks
easy to state and to perform.

The basic operation of awk is to scan a set of input lines in order, searching for lines which match any of a set of patterns
which the user has specified. For each pattern, an action can be specified; this action will be performed on each line that matches the pattern.

Readers familiar with the UNIX program grep[1] will recognize the approach, although in awk the patterns may be more general than in grep,
and the actions allowed are more involved than merely printing the matching line. For example, the awk program

{print $3, $2}

prints the third and second columns of a table in that order.

The program

$2 ~ /A|B|C/

prints all input lines with an A, B, or C in the second field.