Awk Tricks
Delete occurances of a string
awk '{ gsub(/string/,""); print }'
Replace text1 with text2
awk '{ gsub(/text1/,text2); print }'
Replace text1 with text2 only lines that contain Hello
awk '/Hello/ { gsub(/text1/,text2); print }'
Replace text1 with text2 only lines that do NOT contain Hello
awk '!/Hello/ { gsub(/text1/,text2); print }'
Swap first field with the second field
awk '{ temp = $1; $1 = $2; $2 = temp; print }'
Delete the second field
awk '{ $2 = ""; print }'
Remove duplicate, nonconsecutive lines
awk '!a[$0]++'
Show lines based on field-criteria
- Show processes in uninterruptible sleep (usually IO):
ps aux|awk '{if ($8 == "D") print}'
Use substr with print
Useful for when combining with uniq -c for log file analysis:
awk '{print substr($4,0,6)}' squidaccess.log | uniq -c
2571 [14/J
7365 [15/J
8085 [16/J
6537 [17/J
200 [18/J
90 [19/J
Substitute characters before extracting data
Used here for named query logging analysis:
awk '/query:/ { gsub(/#/," "); print $2 " "$5}' named.log | sort | uniq -c|sort -n
All "#" are changed to a space to allow $2 to print just the ip address.
See Also
http://www.catonmat.net/blog/awk-one-liners-explained-part-two/