Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove lines containing keyword from a file

Tags:

ant

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.

like image 643
clamp Avatar asked Jun 15 '12 11:06

clamp


People also ask

How do you delete a line in Notepad ++ that contains a specific text string?

Method 1: Remove lines using Bookmark feature in Notepad++ Click to select the Mark tab. Enable the Bookmark line checkbox. Set Search Mode to Normal. From the Search menu, click Bookmark, and click Remove Bookmarked Lines.

How do I delete all marked lines in Notepad ++?

Is there a way to do this in Notepad++? In notepad++, there is a way to bookmark lines with a search, and then delete those bookmarked lines. No regex necessary. If a search is not needed, the you can bookmark the lines to remove with Ctrl+F2 , and then use menu Search => Bookmark => Remove Bookmarked Lines .


1 Answers

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.

So, for example, copy or move the file using a filterchain containing your linecontains filter.

Use the negate parameter on your linecontains filter to exclude lines containing that string.

Example:

<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>

Before:

$ cat file.txt
abc
assert
def
assert
ghi assert
jkl

After:

$ 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>
like image 56
sudocode Avatar answered Sep 19 '22 08:09

sudocode