Posted by: rydow | August 24, 2007

Awk reminder.

Since I don’t us awk very regularly nowadays I thought it would be nice to have the basic use case somwhere. So here are some examples:

  • Get the process id of syslogd, and yes i know that it could be done simpler with the right ps switches, but the point is demonstrate awk right!
    ps -e | grep -v awk | awk 'BEGIN{FS=" "}/syslogd/{printf $1"\n"}'
    The BEGIN{…} clause may be skipped in this case since whitespace is default field separator. The stuff inbetween the slashes is a regexp and the grep -v is to not get the awk process itself in the answer.
  • awk 'BEGIN {FS = "-"; max = 0;}{if ($2 > max) max = $2;} END {print $1 "-" max}'
    This one uses calculations on a list loocking like this:
    image1-800
    image1-1024
    image1-1280

That’s all for now, have a nice weekend!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: