Using bash variables in sed command

Bash commands such as awk and sed are very handy tools when dealing with text files. For example, if line 30~70 in a pw.out file are the atom coordinates, we can simply type in the terminal:
$ sed -n '30,70' pw.out

and line 30~70 will show up on the terminal.

Things get tricker when the line numbers are variables. Often times we want to use sed in a bash script, and we need to use bash variables. However sed doesn't understand the normal bash variable substituion: $variable. For example, bash doesn't recognize: sed -n '${linehead},${linetail}p' file.

I got stuck on this for a while and more than one time, so I decided to write it down here. The way to get around is to first created a string that contains the whole sed argment (sedarg below), and put the whole string after sed command with eval in front.

#!/bin/bash
sedarg="-e ${line_head,${linetail}p' file
eval sed -n "$sedarg"

There you go. I love sed. I could've loved it even more if this wasn't an issue.

Variable substitution in awk is totally another issue (which is as annoying). I will write it down next time when I have to google it again.

[VMD] Run vmd from Mac terminal

Our work can be done faster and more efficiently if we do everything through the terminal. To ask vmd to open a file through MacOS X terminal, just type in:
$/Applications/VMD\ 1.9.1.app/Contents/Resources/VMD.app/Contents/MacOS/VMD
One-liner!

Of course, I prefer a shorter version of the command vmd. Add the alias below to the .bashrc:
alias vmd='/Applications/VMD\ 1.9.1.app/Contents/Resources/VMD.app/Contents/MacOS/VMD'


Now we are set to run vmd on Mac terminal. To open a .pdb file directly using vmd:
$vmd openthis.pdb
I love one-liner!

[New!]
To load a saved vmd state:
$vmd -e saved_state.vmd