从文件中删除包含关键字的行 [英] Remove lines containing keyword from a file
问题描述
我想删除从文本文件包含特定关键字的所有行。
I would like to remove all lines from a textfile which contain a certain keyword.
到目前为止,我只找到了:
So far I only found:
<linecontains>
<contains value="assert"/>
</linecontains>
但我不知道如何删除这些行。
but I don't know how to remove those lines.
推荐答案
您将使用在 filterchain <该过滤器/一>,在支撑filterchain元件,一个任务就是内置的毗连,复制,的LoadFile,LoadProperties,移动的任务。
You would use that filter in a filterchain, in a task which supports the filterchain element, i.e. the built-in Concat, Copy, LoadFile, LoadProperties, Move tasks.
因此,例如,复制或使用含有您linecontains滤波器filterchain移动文件
So, for example, copy or move the file using a filterchain containing your linecontains filter.
使用否定你的
参数排除包含该字符串的行。 linecontains
过滤器
Use the negate
parameter on your linecontains
filter to exclude lines containing that string.
例如:
<project default="test">
<target name="test">
<copy tofile="file.txt.edit" file="file.txt">
<filterchain>
<linecontains negate="true">
<contains value="assert"/>
</linecontains>
</filterchain>
</copy>
</target>
</project>
在
$ cat file.txt
abc
assert
def
assert
ghi assert
jkl
在
$ cat file.txt.edit
abc
def
jkl
要回答你的后续问题在一个目录应用于选定的文件:
To answer your followup question on applying to selected files in a directory:
<copy todir="dest">
<fileset dir="src">
<include name="**/*.txt"/>
</fileset>
<filterchain>
<linecontains negate="true">
<contains value="assert"/>
</linecontains>
</filterchain>
</copy>
这篇关于从文件中删除包含关键字的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!