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

Monday, May 20, 2013

Using awk to work on data files

In order to average numbers in a file we can use awk as so:
$ cat meyer-heavy.txt | awk '{ sum+=$1;count++ } END {print sum/count}'
To find the largest value:
$ cat meyer-heavy.txt | awk 'BEGIN{max=-9999} $1 > max {print $1; max=$1}' 
Improved version (only prints largest value, not intermediate ones): 
$ cat meyer-heavy.txt | awk 'BEGIN{max=-9999} $1 > max {max=$1} END{print max}'
To find the smallest value:
$ cat meyer-heavy.txt | awk 'BEGIN{min=9999} $1 < min { print $1; min=$1 }' 
Fixed version (doesn't assign blank lines as min value): 
$ cat meyer-heavy.txt | awk 'BEGIN{min=9999} /\-[0-9]+/&&$1 < min { min=$1 } END{print min}'
Where $1 is for column 1, use $2 for column 2 etc...

Thursday, April 4, 2013

Parsing columns in BASH

I sometimes need "cut" functionality that works for fields with more than a single space. For instance, I sadly have a script just to kill nepomukindexer because it constantly goes berzerk and uses all my 8gb of ram. 
So I need to parse the process IDs of all the processes that contain the word nepomukindexer (there are hundreds at times).

First try

I figured cut would do the trick.
ps -ef | grep nepomukindexer | cut -d " " -f 5 
This only works if there are exactly 4 spaces between the two fields, and that isn't always the case.

Second try

I still don't understand why sed doesn't seem to work with the "+" regular expression symbol the way I expect. The following seems like it should replace all instances of one or more spaces with a single space, but it does nothing.
ps -ef | grep nepomukindexer | sed 's/ +/ /g'
Edit: I just found a workaround for this. I changed this to
ps -ef | grep nepomukindexer | sed 's/  */ /g'
                                      ^^ two spaces
and it worked, the only difference being the matching part which says match a single space followed by 0 or more additional spaces, and replace the whole mess with a single space.

Third try

The solution I found is to use awk.
ps -ef | grep nepomuk | awk -F" " '{print $2}'
Using this command, I pipe the results through kill and all is well.