parsed.org

Tips by tag: awk

find all ocurrences for a command (in this case python) :

awk '{for(i = 11; i <= NF; i++) printf $i " "; printf "n"}' < <(ps aux | grep python)

kill all ocurrences for a command (python again) :

ps aux | grep python | awk {'print "kill " $2'} | bash

also can be added the -9 signal

ps aux | grep python | awk {'print "kill -9 " $2'} | bash
awkprocessps
Change Field Separator in Data by cygnus on Jan 12, 2005 10:27 AM

You can use awk to change the field separator in a data stream:

$ cat foo
a,b,c,d,e,f
a,b,c,d,e,f
$ awk 'BEGIN { FS = ","; OFS = ".."; } { $1 = $1; print }' foo
a..b..c..d..e..f
a..b..c..d..e..f

(You just have to touch one of the fields to get it to process the line.)

awkcommandsfieldsparsingshell
Find the Hog by xinu on Sep 10, 2005 12:00 AM

If you need to find the process hogging your CPU, try this:

$ ps aux | awk '!/root|nobody/ { if ($4>2) {print $2}}'
awkcpuownerpipeprocessrootshelluser
Log Chopping by xinu on Jan 12, 2005 11:01 AM

Given a log file with a date in the first column, chop up the file into separate file:

$ awk '/^[0-9]/ {print $0 > $1".log"}' logfile.txt

If you have a string that has characters that the shell won't like, you can do a substitution on them:

$ awk '{gsub("/","_",$1); print $1 > $1".log"}' logfile.txt
awkchoplogshellsplit
Multiple Delimiters by xinu on Jan 12, 2005 12:32 PM

If you need to fetch a substring, but you have more than one delimiter, throw them all in there on the -F option to awk.

Given the text <VirtualHost 192.168.1.0:80>:

$ awk -F" |>" '/^<Virtual/ {print $2}'

Would return 192.168.1.0:80.

awkdelimitershell
Numbering A File by xinu on Feb 27, 2007 11:09 AM

Adding line numbers to a file is easy if you use awk or grep. Adjust the padding in the printf (%2d) for the amount of padding:

awk '{no=no+1; printf "%2d : %s\n", no, $0}' filename > filename.out

Command line taken (and slightly modified) from the UGU mailing list; credit goes to pradeep@unixguru.zzn.com.

awkenumeration
Purge Uninstalled Packages by cygnus on Jan 12, 2005 11:04 AM

Use this command to purge old configs and files from your system that are in uninstalled packages:

$ dpkg --get-selections | awk '/deinstall/ {print $1}' | xargs dpkg --purge
awkcommandsconfigurationdebiandpkgpackagespurgexargs
RSS