Thursday 27 October 2011

Awk in Linux

###Awk###
Features:
1. Field/Column processor
2. Supports egrep-compatible (POSIX) RegExes
3. Can return full lines like grep
4. Awk runs 3 steps:
a. BEGIN - optional
b. Body, where the main action(s) take place
c. END - optional
5. Multiple body actions can be executed by separating them using semicolons. e.g.
'{ print $1; print $2 }'
6. Awk, auto-loops through input stream, regardless of the source of the stream.
e.g. STDIN, Pipe, File
Usage:
1. awk '/optional_match/ { action }' file_name | Pipe
2. awk '{ print $1 }' grep1.txt
Note: Use single quotes with awk, to avoid shell interpolation of awk's variables
3. awk '{ print $1,$2 }' grep1.txt
Note: Default input and output field separators is whitespace
4. awk '/linux/ { print } ' grep1.txt - this will print ALL lines containing
'linux'
5. awk '{ if ($2 ~ /Linux/) print}' grep1.txt
6. awk '{ if ($2 ~ /8/) print }' /var/log/messages - this will print the entire
line for log items for the 8th
7. awk '{ print $3 }' /var/log/messages | awk -F: '{ print $1}'

No comments:

Post a Comment