Archive for the 'awk' Category

My Version Increment Script

Friday, December 7th, 2007

How and when do you increment the version number on an internal web application? If you are not selling your software, not publishing the source but simply providing a service to bunch of users, there is really no such thing as a “release”. All you have is continuous and gradual trickle of patches and updates getting deployed to the production server.

Still, does patching currently used code on the prooduction server constitute a release? I don’t know, but where I work we kinda pretend that it does. So whenever we roll out some new code into production, we nudge the version number up.

Depending on the size of the patch, we either increment the major, minor or the build number. Typically, if we touch the database schema, it’s a major number. If we add new functionality, or fundamentally change how something works, it’s a minor increment. If it’s a minor bugfix or a cosmetic change we increment the build number.

This is of course independent of the internal revision numbers in subversion which are a whole different story. Since I was tired of editing the version and changelog files each time I would roll out a patch, I wrote this little script to help me out. For the record, our version file looks somewhat like this:

version 3.2.5

There is nothing else in the file, and it gets grabbed by scripts in few places for display and etc. The changelog looks like this:

version 1.1.2 - some stuff [Thu Dec 6 12:30:11 EST 2007]
version 1.1.3 - bla bla bla - [Thu Dec 6 12:32:05 EST 2007]
version 1.2.0 - some more stuff [Thu Dec 6 12:33:31 EST 2007]

It’s a plain text file that sits in the root directory, and is linked somewhere in the footer in case users want to see what was changed recently. The script itself is a mix of bahs, awk and zenity goodness. Directory names were changed to protect the guilty more than the innocent:

#!/bin/bash
 
function changelog 
{
 
	ver=`cat ~/project/version`
	comment=$(zenity --entry --text "$ver comment:" --title "Changelog")
	d=`date`
	echo $ver - $comment [$d] >> ~/project/changelog.txt
}
 
ans=$(zenity  --list  --text "Increment What?" --radiolist  --column "Pick" --column "Option" TRUE "Build Number (0.0.x)" FALSE "Minor Version (0.x.0)" FALSE "Major Version (x.0.0)");
 
 
case "$ans" in
	'Build Number (0.0.x)')
		awk 'BEGIN{FS="."} {print $1 "." $2 "." $3+1}' ~/project/version >  /tmp/version && mv /tmp/version ~/project/version && changelog;	
		;;
 
	'Minor Version (0.x.0)')
			awk 'BEGIN{FS="."} {print $1 "." $2+1 ".0"}' ~/project/version >  /tmp/version && mv /tmp/version ~/project/version && changelog;
			;;
 
	'Major Version (x.0.0)')
			awk 'BEGIN{FS=" "} {print "version " int($2+1) ".0.0"}' ~/project/version >  /tmp/version && mv /tmp/version ~/project/version && changelog;
			;;
esac

When you run it, it should pop up a nice dialog asking you which version number do you want to increment:

Choose Which Version to Increment

Then once you choose, it will prompt you for a comment for the changelog:

Comment for the Changelog

There are probably better ways of doing this, and incrementing your version number but this is how I have been doing it lately. How do you do it?

Awk One Liners

Friday, March 30th, 2007

I’ve been doing this stuff all day, so let me show you few nifty awk tricks. For these examples lets assume we have a tab delimited file with n columns and m rows.

To take an average of each row (average all the values from each column of a given row do the following:

awk '{sum=0; for(i=1; i<=NF; i++){sum+=$i}; sum/=NF; print sum}' file

The NF variable is a reserved awk word which expands to the number of fields (or columns) in the current row. If you want to take an average of all the columns do:

awk '{for(i=1; i<=NF; i++){sum[i]+=$i}} END {for(i=1; i<=NF; i++){printf sum[i]/NR "\t"}}' file

The NR variable is another awk built-in, that gives you number of records (or rows) read. You can exploit NR To add line numbers to your file do:

awk '{print NR, $0}' file

Of course the same can be accomplished by:

cat -n file

To find the number of lines in a file you can do:

awk 'END {print NR}' file

If you want to find the combined number of lines in all the files passed in on the command line do:

awk 'END {print FNR}' file1 file2 file3 ...

A lot of data manipulation can be done with some awk magic, and simple unix commands such as grep, paste, wc and etc..