Awk is a pattern action language

AWK

A programming language for handling common data manipulation tasks with only a few lines of program
Awk is a pattern action language
The language looks a little like C but automatically handles input, field splitting, initialization, and memory management
Built-in string and number data types
No variable type declarations
Awk is a great prototyping language
Start with a few lines and keep adding until it does what you want

History
Originally designed/implemented in 1977 by Al Aho, Peter Weinberger, and Brian Kernigan
In part as an experiment to see how grep and sed could be generalized to deal with numbers as well as text
Originally intended for very short programs
But people started using it and the programs kept getting bigger and bigger!
In 1985, new awk, or nawk, was written to add enhancements to facilitate larger program development
Major new feature is user defined functions
Other enhancements in nawk include:
Dynamic regular expressions
Text substitution and pattern matching functions
Additional built-in functions and variables
New operators and statements
Input from more than one file
Access to command line arguments
nawk also improved error messages which makes debugging considerably easier under nawk than awk
On most systems, nawk has replaced awk
On ours, both exist

Structure of an AWK Program

An Awk program consists of:
An optional BEGIN segment
For processing to execute prior to reading input
pattern – action pairs
Processing for input data
For each pattern matched, the corresponding action is taken
An optional END segment
Processing after end of input data

Pattern-Action Structure

Every program statement has to have a pattern, an action, or both
Default pattern is to match all lines
Default action is to print current record
Patterns are simply listed; actions are enclosed in { }s
Awk scans a sequence of input lines, or records, one by one, searching for lines that match the pattern
Meaning of match depends on the pattern
/Beth/ matches if the string “Beth” is in the record
$3 > 0 matches if the condition is true

Cours gratuitTélécharger le cours complet

Télécharger aussi :

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *