How to use variables in sed on Windows?
Sebastian Wright
I'm writing a batch script to automate build events on Windows, and as part of it I need to change some lines in a few files. So I'm going to use sed. But how do I use variables in sed inside a batch script?
51 Answer
Refer to the variables using the usual %varname% syntax used by cmd.exe. As shown in this example, I've defined two variables old and new and substituted them into a sed expression on the command line. Typing original into the input, sed echos back new and improved.
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
c:\Users\Nicole>set old=original
c:\Users\Nicole>set new=new and improved
c:\Users\Nicole>sed "s/%old%/%new%/"
original
new and improved
^Z 4