Friday, November 12, 2010

Fun with process substitution

I was introduced to process substitution recently at work, it's a great way to avoid temporary files for some simple cases.

For example, say I wanted to know what the column header changes were between two files (same data, different code used to extract them). The files are tab delimited and I have a script I use (frequently) that prints out the index and name for the headers in an input file - columnHeaders.sh.

So, if I want to see what's different between two files, in the past I'd create two output files using my columnHeaders.sh script and then use diff, kompare or comm to compare them.

You can eliminate the temporary files using a technique called process substitution.

> diff <(columnHeaders.sh file1) <(columnHeaders.sh file2)
2,3c2,3
> 0 blarg
> 1 blorg
--
< 0 blorg
< 1 blarg
In this case we see two columns have been swapped between file1 and file2.

Take a look at the Advanced Bash Scripting Guide for more examples.

The columnHeaders.sh script basically does this (but allows user specified delimiters):
> head -1 <input file> | perl -F'\t' -lane 'print $n++,"\t$_" for @F'

Friday, September 17, 2010

Bash command line parameter parsing (getopts)

If I only need a single optional parameter I'll often just check to see if the input positional parameter of interest is set (e.g. script that accepts one required field and one optional field):

if [[ ! -n "$2" ]];
then
echo "I received the second parameter:$2"
fi

But if you want to do something a bit more complex, getopts is your friend.

For example, say you want to have the user input their first name, last name and a "keep my data private" flag you could do something like this:

while getopts f:l:p flag
do
case $flag in
f)
FIRSTNAME=$OPTARG;;
l)
LASTNAME=$OPTARG;;
p)
PRIVATE=1;;
?)
echo "$0 -f <first name> -l <last name> -p"
echo -e "\t-p [flag] keep my data private"
exit
done

The getopts command is fairly straightforward (man getopts for more details). If an option requires an argument then a colon is placed after it's letter designation ('f' and 'l' in the above example).

You can check for required parameters by looking at which variables were set:

if [[-z "$FIRSTNAME" || -z "$LASTNAME" ]];
then
echo "missing required parameter"
fi

Wrap that all up into a neat script with a subroutine that outputs a usage statement and you're home free:

function usage_and_exit()
{
echo "$0 -f <first name> -l <last name> -p"
echo -e "\t-p [flag] keep my data private"
exit
}

while getopts f:l:p flag
do
case $flag in
f)
FIRSTNAME=$OPTARG;;
l)
LASTNAME=$OPTARG;;
p)
PRIVATE=1;;
?)
usage_and_exit;;
done

if [[ -z "$FIRSTNAME" || -z "$LASTNAME" ]];
then
echo "missing a required parameter (firstname and lastname are required)"
usage_and_exit
fi

if [[ $PRIVATE -ne 0 ]];
then
echo "protecting private data"
fi

Wednesday, August 18, 2010

MySQL: the mystery of unsetable global variables

I just tried updating our mysql server to accept very long connections. I have a ton of jobs running, so I wanted to set the wait_timeout variable (via the mysql shell) to something reasonable for these jobs. The default of 8 hours is not sufficient in some rare cases so I tried to set the timeout higher:

mysql> SHOW VARIABLES LIKE 'wait_timeout';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| wait_timeout | 28800 |
+--------------------------+-------+
1 row in set (0.00 sec)

mysql> SET GLOBAL wait_timeout=86400;
mysql> SHOW VARIABLES LIKE 'wait_timeout';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| wait_timeout | 28800 |
+--------------------------+-------+
1 row in set (0.00 sec)

What? Why wasn't it set?

Well, the reason is that the SHOW VARIABLES command defaults to the session variables. So, the local session wait_timeout is still 2880, but the global wait_timeout was actually updated correctly:

mysql> SHOW GLOBAL VARIABLES LIKE 'wait_timeout';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| wait_timeout | 86400 |
+--------------------------+-------+
1 row in set (0.00 sec)

Wednesday, July 14, 2010

Basic MySQL 'top' command

Ever wanted to keep an eye on the running processes in a MySQL database? How about something that works a little like the top command, but without any of the bells and whistles?

Well, here you go:

> while [[ 1 ]];
do
clear;
mysql -u <username> -p<password> -h <host> -e "show full processlist";
sleep 1;
done


Remember to replace the <username> <password> and <host> variables with the values for your database. Also, if you don't like the bounding box on the mysql output, you can have a cleaner output by using redirection instead of the -e flag:

mysql -u <username> -p<password> -h <host> <<< "show full processlist";

Wednesday, June 16, 2010

Case insensitive regex in bash

By default regexes in [[ ]] are case sensitive. If you want to match in a case insensitive way you have to set the shell option nocasematch.


mytext="eXistenZ"
if [[ $mytext =~ existenz ]];
then
echo "yep"
else
echo "nope"
fi

If you run the above script you should get "nope" as the output. For case insensitive matching just insert this into the script prior to the regex:

shopt -s nocasematch;


You can unset the nocasematch shell option using the following: shopt -u nocasematch

Here's a more complete example:

mytext="eXistenZ"
function testText
{
if [[ $mytext =~ existenz ]];
then
echo "yep"
else
echo "nope"
fi;
}
testText
shopt -s nocasematch
testText


If you run that script then the output is:

nope
yep

Sunday, June 13, 2010

R related links

These are just a few R links I think are interesting and worth a further read:

  1. Colour palettes in R.

  2. Using R for introductory statistics

  3. Web-friendly visualisations in R

  4. A different way to view probability densities

  5. Thoughts on Making Data Work



Also of note is Google's announcement (on June 2nd) that they were working with the USPTO to make all granted patents, trademarks and published applications freely available for download - see this post.

Tuesday, June 1, 2010

I love subshells

Sometimes it's the little things in life that give us the greatest pleasures. I love subshells. There, I admitted it.

Say you want to do something with the output of a program but would like to prepend some output/text that also needs to be operated on. The standard idiom would be to create a file with the prepended output, append your output to this and then operate on the file, but you can remove the intermediate files using a subshell. Here's a contrived example:


> (echo -e "some\toutput\tto\tappend"; perl -lane 'print join("\t", log($F[0])/log(10), @F[1 .. #$F])' ;) | a2ps


Something else I use subshells for a lot is to launch programs in a different directory without having to cd there and then back again:


> (cd ~/workspace/; eclipse &)


like I said; it's the little things.