Showing posts with label awk. Show all posts
Showing posts with label awk. Show all posts

Friday, April 11, 2008

Using Awk to Grab a Column

A lot of times when hacking on the command-line, I need to filter a particular column coming in from a pipe. A good example would be grabbing a pid from ps output or cat'ing a file with mysql process status and grabbing mysql thread id's for a subsequent kill. I generally use awk to do this because it's installed everywhere, and it makes doing this very simple. Take the following example:

user@host.org [~]# ps aux
root 2 0.0 0.0 0 0 ? S Mar09 1:04 [migration/0]
root 3 0.0 0.0 0 0 ? SN Mar09 0:10 [ksoftirqd/0]
root 4 0.0 0.0 0 0 ? S Mar09 1:14 [migration/1]
root 5 0.0 0.0 0 0 ? SN Mar09 0:10 [ksoftirqd/1]


Now to grab the pid's (2,3,4,5):

[~]# ps aux | awk '{print $2}'

2
3
4
5

One step further to kill those pid's (you wouldn't want to kill these, but...)

[~]# ps aux | awk '{print $2}' | xargs kill -15