Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you remove empty or blank lines from a text file using MSBuild?

I would like to remove all blank lines (lines that just have some number of spaces and a newline character) from a list of files using MSBuild.

What is the best way to accomplish this?

I recognize that I could write a MSBuild plug in C# or VB.NET that will do this using simple Regex replacement, but would prefer a solution that doesn't need me to do this.

If there's a open source MSBuild plugin that does this - I'd welcome that solution as well.

like image 820
Adam Avatar asked Sep 20 '25 07:09

Adam


1 Answers

@Ludwo is right, you have to take into account white-space characters. Moreover to replace any text with empty string you need to use ReplacementTextEmpty property instead of passing empty string to ReplacementText property. So, the following target should solve the problem:

<Target Name="Minify">
  <ItemGroup>
    <File Include="**\*.cs" />
  </ItemGroup>
  <FileUpdate
    Files="@(File)"
    Regex="(\n\s*\n)+"
    Multiline="False"
    ReplacementTextEmpty="True"/>
</Target>

You just need to call the target via MSBuild:

msbuild MyProject.csproj /t:Minify
like image 152
Alexander Avatar answered Sep 21 '25 20:09

Alexander