Passing enviornment variable to sed

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

# for i in {2..24}; do cp test1.com test$i.com; 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$i.com ; 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.

Advertisements

Tags: , , ,

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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: