Showing posts with label Bash. Show all posts
Showing posts with label Bash. Show all posts

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.

[awk] using shell variable

Shell variable can be ported into awk command by using "-v" flag:
#!/bin/bash
var=100
awk -v var=$var '{print $1*var}' file.dat
An important note here is that within the awk command, calling the var does not require the '$' sign.

If there are more than one shell variable to call, use -v again and put space in between:
awk -v var=$var -v var2=$var2 '{print $1*var, var2}' file.dat
This will print $1*$var and $var2.