-2

I was trying to write the following into a file using sed, in a Windows .bat file:

<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Test|AnyCPU'">

I came across a problem when doing this, and that problem is that, in Windows, double quoting escaping is up to the program's implementation, according to this answer. So writing the sequence into a text file was a real headache on sed for Windows.

Edit: Note that this question is NOT about how to escape double quotes in batch, but how to escape double quotes in sed for Windows, so this answer does not resolve the question.

For anyone wondering how to resolve this, see the accepted answer.

Compo
  • 36,585
  • 5
  • 27
  • 39

1 Answers1

1

So let's see how I solved this. This is the test batch file and I'll explain its meaning below:

SET ConfigurationName="Test"
sed "1 a  <PropertyGroup Condition=\""'$(Configuration)|$(Platform)'" == "'%ConfigurationName%|AnyCPU'\"">" "C:\Users\x\outputfile.txt" -i

In Windows' sed, the escaping of double quotes is done using \". Usually, at least in the Visual Studio Code's terminal, I could simply write the arguments for sed with single quotes, but I cannot do the same in a .bat file. This means that I need to escape the double quotes.

As you can see, the sequence \"" is needed in order to escape the " character, and then keep writing in the file. If you don't, then $ is interpreted as a new command and Windows won't recognize it.

Another thing to mention is that < and > characters do not need to be escaped if written inside double quotes in sed.