Using bash variables in sed command

Posted on March 10, 2014, 12:43 PM

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.