# It should be possible to remove lines using filter(closure)

**URL:** <https://discuss.gradle.org/t/it-should-be-possible-to-remove-lines-using-filter-closure/518>\
**Category:** Old Forum Archive\
**Created:** [July 3, 2014, 9:00am UTC](https://discuss.gradle.org/t/it-should-be-possible-to-remove-lines-using-filter-closure/518 "2014-07-03T09:00:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jochen\_Berger](https://avatars.discourse-cdn.com/v4/letter/j/a9adbd/32.png) [@Jochen\_Berger](https://discuss.gradle.org/u/Jochen_Berger)\
**Post date:** [July 3, 2014, 9:00am UTC](https://discuss.gradle.org/t/it-should-be-possible-to-remove-lines-using-filter-closure/518/1 "2014-07-03T09:00:00Z")

</div>

When copying files using the Copy task, it is possible to filter the files’ content using a filter closure that is called with each line of the source. It is currently not possible to remove a line altogether. I propose that if the closure returns null, the line should be removed.

---

<div class="post-metadata">

**Author:** ![bmuschko](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/bmuschko/32/5458_2.png) [@bmuschko](https://discuss.gradle.org/u/bmuschko)\
**Post date:** [July 3, 2014, 1:06pm UTC](https://discuss.gradle.org/t/it-should-be-possible-to-remove-lines-using-filter-closure/518/2 "2014-07-03T13:06:00Z")

</div>

Did you already try to use ‘FileCopyDetails’ for this yet? Here’s an example:

```gradle
task myCopyTask(type: Copy) {
  from("yourDir") {
    eachFile { FileCopyDetails details ->
      if(details.file.text.contains("some word")) {
        // add custom code that modifies text
         details.file.text = modifiedText
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Jochen\_Berger](https://avatars.discourse-cdn.com/v4/letter/j/a9adbd/32.png) [@Jochen\_Berger](https://discuss.gradle.org/u/Jochen_Berger)\
**Post date:** [July 3, 2014, 2:40pm UTC](https://discuss.gradle.org/t/it-should-be-possible-to-remove-lines-using-filter-closure/518/3 "2014-07-03T14:40:00Z")

</div>

No, I didn’t think of that, thanks. So ‘FileCopyDetails.getFile()’ returns the target file? Anyway, I suggest that if the filter Closure returns ‘null’, the line should be removed.

---

<div class="post-metadata">

**Author:** ![bmuschko](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/bmuschko/32/5458_2.png) [@bmuschko](https://discuss.gradle.org/u/bmuschko)\
**Post date:** [July 3, 2014, 3:35pm UTC](https://discuss.gradle.org/t/it-should-be-possible-to-remove-lines-using-filter-closure/518/4 "2014-07-03T15:35:00Z")

</div>

Correct, ‘FileCopyDetails.getFile()’ is the target file. You could probably also go for one of the Ant filters. This should do the job:

```gradle
filter(LineContains, negate: true, contains: ['some word'])

```

This filter class needs to be imported via

```gradle
import org.apache.tools.ant.filters.LineContains

```

---

<div class="post-metadata">

**Author:** ![Jochen\_Berger](https://avatars.discourse-cdn.com/v4/letter/j/a9adbd/32.png) [@Jochen\_Berger](https://discuss.gradle.org/u/Jochen_Berger)\
**Post date:** [July 8, 2014, 3:24pm UTC](https://discuss.gradle.org/t/it-should-be-possible-to-remove-lines-using-filter-closure/518/5 "2014-07-08T15:24:00Z")

</div>

Thanks, that one looks fine. I’d still like to be able to remove lines with the ‘filter(Closure)’ method, but the workarounds you proposed are very helpful.

---

<div class="post-metadata">

**Author:** ![bmuschko](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/bmuschko/32/5458_2.png) [@bmuschko](https://discuss.gradle.org/u/bmuschko)\
**Post date:** [July 8, 2014, 3:32pm UTC](https://discuss.gradle.org/t/it-should-be-possible-to-remove-lines-using-filter-closure/518/6 "2014-07-08T15:32:00Z")

</div>

Sounds reasonable to me. Would you be interested in working on a pull request for this? If yes, have a look at the [Gradle Project & Developer Guidelines](https://github.com/gradle/gradle/blob/master/CONTRIBUTING.md). We are excited about every contribution.

---

<div class="post-metadata">

**Author:** ![Jochen\_Berger](https://avatars.discourse-cdn.com/v4/letter/j/a9adbd/32.png) [@Jochen\_Berger](https://discuss.gradle.org/u/Jochen_Berger)\
**Post date:** [July 9, 2014, 8:46am UTC](https://discuss.gradle.org/t/it-should-be-possible-to-remove-lines-using-filter-closure/518/7 "2014-07-09T08:46:00Z")

</div>

I could create a patch if that helps. Sorry, but I don’t intend to go through all the legal/CLA stuff at the moment. I’m still exhausted from the last time I had to do that for ASF.
