Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting new lines with regular expression from Ant task

Tags:

regex

newline

ant

I've posted to 2 forums already (CodeRanch and nabble) and no one has responded with an answer .... so stack overflow ... you are my last hope. All I'm trying to do is delete the "\n" from a file using an Ant task. There simply is a bunch of empty lines in a file and I don't want them there anymore ... here is the code I used ..

    <replaceregexp file="${outputFile}"
     match="^[ \t\n]+$"
     replace=""
     byline="true"/>

It's not picking up the regular expression and I've tried a hundred different ways and I can't figure it out. Any ideas?

like image 952
Christopher Dancy Avatar asked Nov 30 '22 06:11

Christopher Dancy


2 Answers

You may use a FilterChain (specifically an IgnoreBlank TokenFilter) to do precisely what you need:

<copy file="${input.file}" toFile="${output.file}">
  <filterchain>
    <ignoreblank/>
  </filterchain>
</copy>

ignoreblank will also remove lines consisting entirely of white space, but looking at your regular expression it seems that is what you want.

like image 86
Alexander Pogrebnyak Avatar answered Dec 08 '22 00:12

Alexander Pogrebnyak


This works:

<replaceregexp file="..."
               match="(\r?\n)\s*\r?\n" 
               flags="g"
               replace="\1" />

The point is to match line ends manually and globally (flags="g").

like image 33
Borek Bernard Avatar answered Dec 07 '22 23:12

Borek Bernard