How to use variables in sed on Windows?

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?

5

1 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

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like