Awk is still cool.

home | blog | Teh Internet | guest blog |rants | placeholder | political | projects | Gwen and Liam | Citadel patched | Tools | Scouts


- Careful Chrome users, this search box might be "Not secure"



Needed a fast way to get average, min and max.
Awk to the rescue.... Thanks damiles!
http://blog.damiles.com/2008/10/awk-calculate-mean-min-and-max/
And, as I try to do - in case the link goes away, here are the goods from the link above:
awk ‘{if(min==""){min=max=$1}; if($1>max) {max=$1}; if($1< min) {min=$1}; total+=$1; count+=1} END {print total/count, min, max}’ data.txt

Short, sweet, and you can understand it - even with it being a pseudo one liner.
You can do the same thing in any language, but Awk probably beat you to it :-)
Plus, if you do some monitoring and graphing of memory stats, I bet you can put that to some good use!

More fun:
Who is connected to port 80, from where, and how many:
netstat -aonp | grep ":80" | awk '{print $5}' | sort | cut -d":" -f1 | uniq -c


Percent swap used:
free | grep 'Swap' | awk '{t = $2; f = $4; print ((1 - f/t)*100)}'

Convert spaces in a log file to pipe for easy csv:
cat logfile | awk '{$1=$1; print}' OFS="|" > output.csv

Add text to beginning of every line in a file:
awk '{print "text to add" $0}' inputfile

Get the last argument in a line:
cat infile | awk '{print $NF}'
- second to last arg in line?:
cat infile | awk '{print $(NF-1)}'



[æ]