|
Simple awk Scripts
- An awk script contains patterns and actions
awk '/sey/ { print }' /etc/passwd
awk /sey/ /etc/passwd
ls -l | awk '{ sum += $5; print sum; }'
- An action with no pattern applies to every line.
- An action preceded by a pattern like /sey/ is performed
for each line containing the string.
- Actions are always surrounded by braces.
- Variables are always strings, but can be used as numbers.
- Initially a variable is empty with numeric value 0.
- It is easy to write an averaging script
- Special patterns BEGIN and END can be used to specify
actions to perform before reading the input and after
all the input has been processed.
- ls -l | awk '{ sum += $5; } END { print sum; }'
- The record number is available as NR
awk 'NR <= 10' file
|