Passing enviornment variable to sed

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.


Tags: , , ,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: