Fun with awk: awk is a neato language created by Aho, Weinberger, and Kernighan. Thus the name. Its purpose is to act as a quick and dirty text processing language. however, over time there have been extensions made to it -- new awk (nawk) was created to make awk a more C like system with more regular expressions and cooler utilities for stuff like file handles etc etc... In addition, the Gnu hordes behind Richard Stallman worked with awk to form gawk which must have some differences; but I don't know what they are beyond the Gnu style of arguments.
So, how does one use awk?
From the man page: An AWK program is a sequence of pattern {action} pairs and function definitions. Short programs are entered on the command line usually enclosed in ' ' to avoid shell interpretation.
So, an awk program in its simplest case is created by typing awk '{some commands}' buffer.
The next questions are, what commands? and what is the buffer? First we wil answer the question of buffer: The buffer is whatever is fed to awk as input. Here are a couple of examples which will also illustrate a little bit of simple awk syntax.
rabbit<2:05pm>:/home/trey% ps acx | grep xterm | awk '{print $1}'
The preceding command shows that the buffer awk works upon need not be a file of text. This makes a process listing of all processes including those which I have no control over. This outputs a table of many processes that looks something like:
7882 ? S 0:01 xterm
Except that there are many of these columns. The pipe sends this output to grep which seeks out the characters pine. Finally, whatever is left is sent to awk which simply prints out the first column: which is in this case 7882... What can we do with this? Use it to quickly kill a process by simply appending the following: | xargs kill. And poof! my terminal is goine.
Another way to send awk data to play with is to give it a file. If I have a comma separated file of social security numbers associated to their wesid's and Names. Something like this:102-77-3908,288233,Ashton Trey Belew
Well, you dont honestly care about their names, nor even their wesid's; so do this: rabbit<2:21pm>:/home/trey% awk -F, '{print $1}' goofyfile
And out comes all the social security numbers.
So, what do we do? How about this:
ps -ef | grep pine | awk '{if ($5 ~ /^[A-Z]/) print $1}' | xargs kill
Here is what I would do if I wanted every username who has had their username diabled on the mail server through an @ or _.
awk -F: '{if ($1 ~ /^_|@/) print $1}' ./passwd
And here I can restore the lines of the passwd file which have had their usernames changed; note, though that this is only those in group 10603...
awk -F: '{if ($4 == 10603) if ($1 ~ /^@/) print substr($0,2,length($0))}' ./passwd