Use sed to edit xml file

General usage of GNU sed on Windows

sed -i -e "s|what|replacewith|g" file

-i replaces the original file

-e runs expression

s|what|replacewith|g replaces all / globally what occurrences with replacewith

You can avoid g if you want to stop at the first occurence.

More complications

If you have quotes inside what you escape them with \”

Backslash char is to be escaped with \\


sed -i -e "s|<Project ToolsVersion=\"4.0\" DefaultTargets=\"Build\" xmlns=\"\">|<Project ToolsVersion=\"4.0\" DefaultTargets=\"Build\" xmlns=\"\">\n <PropertyGroup Label=\"UserMacros\">\n <TSMVERSION Condition=\"'$(VERSION_BASED_OUTPUT)'=='1' And '$(BUILD_TSM_VERSION)'=='712'\">712</TSMVERSION>\n <TSMVERSION Condition=\"'$(VERSION_BASED_OUTPUT)'=='1' And '$(BUILD_TSM_VERSION)'=='713'\">713</TSMVERSION>\n <TSMVERSION Condition=\"'$(VERSION_BASED_OUTPUT)'=='1' And '$(BUILD_TSM_VERSION)'=='720'\">720</TSMVERSION>\n <BUILDFLAGVER Condition=\"'$(BUILD_TSM_VERSION)'=='712'\">BUILD_TSM_V712</BUILDFLAGVER>\n <BUILDFLAGVER Condition=\"'$(BUILD_TSM_VERSION)'=='713'\">BUILD_TSM_V713</BUILDFLAGVER>\n <BUILDFLAGVER Condition=\"'$(BUILD_TSM_VERSION)'=='720'\">BUILD_TSM_V720</BUILDFLAGVER>\n </PropertyGroup>|g" %1
sed -i -e "s|<OutputPath>bin\\Debug\\</OutputPath>|<OutputPath>bin$(TSMVERSION)\\Debug\\</OutputPath>\n <IntermediateOutputPath>obj$(TSMVERSION)\\Debug\\</IntermediateOutputPath>|g" file.xml

End of line char is usually \n

You have to check the end of line char using

od -xc file

This entry was posted in Uncategorized and tagged , , . Bookmark the permalink.

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