Archive for June, 2011

Passing enviornment variable to sed

2 June 2011

I have a sample which has a line as follows
Now I want to make 24 copies of i.e. and so on, so that each of them carry respective test$i.chk line.
i.e. should contain
%chk=test2.chk should contain
%chk=test3.chk should contain
and so on.
Creating copies of the is simple

# for i in {2..24}; do cp test$; done

Now I need to replace test1.chk in each file with appropriate test$i.chk as explained above. I will use sed to accomplish this

# for i in {2..24}; do sed -i 's/^%chk=test1.chk$/%chk=test'"$i"'.chk/g' test$ ; done

Notice that I simple quoted the $i with double quote by turning off the strong quoting of a single quote. Its simple bash mechanism to selectively expand the $ into variable argument.