ant:用单行替换双空行

ant: replace double empty line with single

我正在尝试设置一个遍历每个文件的脚本,并用一个空行替换所有连续的空行。类似于:

aaa



bbb

转换为

aaa

bbb

到目前为止我有:

<replace file="web.xml" value="">
            <replacefilter>
                <replacetoken>\n\n</replacetoken>
                <replacevalue>\n</replacevalue>
            </replacefilter>
        </replace>

但它根本不起作用

到目前为止唯一的方法是:

<target name="-remove-blank">
        <replaceregexp file="${basedir}/temp/WEB-INF/web.xml"
               match="(\r?\n)\s*\r?\n" 
               flags="g"
               replace="" 
        />
    </target>

但这会删除所有空行,而我需要它只在有 2 个或更多连续空行时才删除

 <replaceregexp
  file="some.file"
  match="(${line.separator}){2}"
  replace="${line.separator}"
  flags="g"
 />

无效。
-- 编辑--
给定文件

line1

line2

line3

line4


line5



line6

并使用:

<replaceregexp
 file="foo.txt"
 match="^(${line.separator}){2,}"
 replace="${line.separator}"
 flags="mg"
/>

或在 windows 上(最好使用 ${line.separator} 并让 ant 完成工作):

<replaceregexp
 file="foo.txt"
 match="^(\r\n){2,}"
 replace="\r\n"
 flags="mg"
/>

有效:

line1

line2

line3

line4

line5

line6

如果行中包含空格,您必须使用:

match="^(\s*${line.separator}){2,}"

match="^(\s*\r\n){2,}"