4

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?

3
  • Are you trying to remove ALL line breaks or just empty lines?
    – matpie
    Nov 19, 2009 at 21:17
  • just empty lines. I want a file which looks like the following... <blankline> <blankline> hello world <blankline> <blankline> replaced with a file which has only 1 line in it .. that being hello world Nov 19, 2009 at 21:43
  • A possibly important thing to know is what OS the file is valid on- meaning is the new line character \n, or is it \r\n? Nov 20, 2009 at 14:17

9 Answers 9

13

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.

1
  • Yes this does work! Thank you Alexander! My boss is still going to kick my @$$ for not having this done yesterday but at least it will work now. Thanks again. Nov 20, 2009 at 14:09
8

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").

1

I think your issue is with using the start-of-line/end-of-line characters. What you are looking for is blank lines, but what your regular expression appears to search for is a start-of-line followed by 1 or more new lines and/or tabs followed by end-of-line. Shouldn't it be start-of-line followed by end-of-line with nothing in between? So match="^$".

^ The above assumes the regex engine you're using is treating ^$ as start/end of line instead of start/end of the entire input string. If it's the entire input string, your regex would only match empty files, not files that contain some content but also some blank lines.

6
  • Yes I've tried what you said in the first paragraph and still nothing ... I'm using the regex tasks from the Ant build tool. I believe however that what you said in the second paragraph may be right ... that it's matching the start/end of the string which is why I'm not getting what I want. I've tried the regex a hundred different ways and was not sure if someone had a solution to this same problem. Nov 19, 2009 at 19:40
  • if that is indeed the problem, set the multiline flag (I believe it is flags="m" but check the docs at ant.apache.org/manual/OptionalTasks/replaceregexp.html . Nov 19, 2009 at 19:44
  • Yes I've tried that as well and still nothing. However the docs do give the option for single line and multi-line and still they do not work for me. Nov 19, 2009 at 20:12
  • did you try with flags="m" AND match="^$"? Nov 19, 2009 at 20:14
  • Yes I have and still nothing :( Nov 19, 2009 at 20:21
1

You can use <filterchain>, reading only the first line and stripping out all the line feeds. Example:

<loadfile property="new.property" srcfile="${input.file}">
    <filterchain>
        <headfilter lines="1" />
        <striplinebreaks />
    </filterchain>
</loadfile>
0

From the documentation:

Similar to regexp type mappers this task needs a supporting regular expression library and an implementation of org.apache.tools.ant.util.regexp.Regexp

Do you have these?

0

The correct regex for a blank line is

^[ \t]*$\r?\n

You might have to escape the backslashes in the string, like so:

match="^[ \\t]*$\\r?\\n"
1
  • I tried this as well and still nothing. I believe this has to be a problem with the way Ant handles new-line characters. I've tried iterating over the contents of the file with a "\n" delimeter and still it gives me only one string ... while the file is clearly 10 lines long with only 3 of those having content. The code I used for this is as follows <loadfile property="lines" srcFile="${tempFile}"/> <ac:foreach list = "${lines}" delimiter = "\n" param = "rule_line" target = "outputLinesToFile" /> Nov 19, 2009 at 21:23
0

Try the following regular expression:

<replaceregexp file="${outputFile}"
    match="^\s*\n"
    replace=""
    byline="true" />

That should remove all empty lines and those containing only whitespace.

0

Have you tried the flags="g" option?

g - change globally

for reference http://ant.apache.org/manual/Tasks/replaceregexp.html

0

The following will work on Windows and Unix/Linux EOL characters and will replace the match (blank lines) with nothing (versus replacing with the first capturing group \1):

<replaceregexp file="${outputFile}" match="^\s*\r?\n" flags="g" replace="" />

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.