The UNIX System

The UNIX System

what can yo do with awk?

awk operation:
scans a file line by line
splits each input line into fields
compares input line/fields to pattern
performs action(s) on matched lines
Useful for:
transform data files
produce formatted reports
Programming constructs:
format output lines
arithmetic and string operations
conditionals and loops

Basic awk program
consists of patterns & actions:
pattern {action}

if pattern is missing, action is applied to all lines
if action is missing, the matched line is printed
must have either pattern or action

Example:
awk ‘/for/’ testfile
prints all lines containing string “for” in testfile

Input file
A field is a unit of data in a line
Each field is separated from the other fields by the field separator
default field separator is whitespace
A record is the collection of fields in a line
A data file is made up of records

Buffers

awk supports two types of buffers:
record and field

field buffer:
one for each fields in the current record.
names: $1, $2, …

record buffer :
$0 holds the entire record

Some system variable

FS Field separator (default=whitespace)
RS Record separator (default=\n)

NF Number of fields in current record
NR Number of the current record

OFS Output field separator (default=space)
ORS Output record separator (default=\n)

FILENAME Current filename

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 *