You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
<strong>Unix</strong> Tools<br />
END { print max, maxline }<br />
# Print every line that has at least one field<br />
NF > 0<br />
# Print every line longer than 80 characters<br />
length($0) > 80<br />
# Print <strong>the</strong> number of fields in every line and <strong>the</strong> line<br />
AWK – Programming Language<br />
File processing programming language:<br />
- generates reports<br />
- matches patterns<br />
- validates data<br />
- filters data for transmission<br />
{print NF, $0}<br />
# Print <strong>the</strong> first two fields in opposite order<br />
{print $2, $1}<br />
# Replace first field by <strong>the</strong> line number<br />
{$1 = NR; print}<br />
# Erase second field<br />
{$2 = ""; print }<br />
! Program Structure<br />
pattern ( action )<br />
pattern ( action )<br />
…<br />
The pattern or action may be omitted, but not both<br />
An awk program has <strong>the</strong> following structure:<br />
- a BEGIN section – run before input lines are read<br />
- a record section<br />
- an END section – run after all files are processed<br />
! Lexical Units<br />
Awk programs are made up of lexical units called tokens:<br />
- numerical constants – decimal or floating e.g. 12.<br />
- string constants – sequence of characters e.g. "ab"<br />
- keywords<br />
- BEGIN END FILENAME FS NF NR OFS ORS OFMT RS<br />
- break close continue exit exp for getline if in length log next<br />
number print printf split sprintf substr while<br />
- identifiers – variables k arrays<br />
- operators<br />
- assignment += -= *= /= %= ++ --<br />
- arithmetic<br />
- relational<br />
- logical && | !<br />
- regular expression matching<br />
- record and field tokens<br />
- $0 current input record<br />
- $1 first field, $2 second field, etc<br />
- NF number of fields, $NF last field (not defined in BEGIN or END pattern)<br />
- NR number of records (lines so far)<br />
- RS record separator (set to newline)<br />
- FS field separator (set to space)<br />
- RS =" " makes an empty line <strong>the</strong> RS<br />
- OFS output field separator<br />
- ORS output record separator<br />
- comments<br />
46