Skip to content

Function to remove specific lines from a file (and return an ArrayList) #39

Description

@MarieLataretu

In combination with listToMD5(), I wrote a Groovy utils function to remove specific lines from a file and return an ArrayList:

  // Function to filter certain lines from a file and return an ArrayList
  // Remember that the index starts at 0 in Groovy
  public static ArrayList excludeLines(String inFilePath, linesToExclude) {
      def inputLines = new File(inFilePath).readLines()
      if (linesToExclude.size() >= 0){
          Set<Integer> setOfLines = (0..inputLines.size()-1) as Set
          def newSetOfLines = setOfLines.minus(linesToExclude)
          return inputLines.getAt(newSetOfLines)
      }
      else {
          return inputLines
      }
  }

Test usage:

{ assert listToMD5(UTILS.excludeLines("$outputDir/foo.txt", [1,3,7,8,9,11,12,13,14,15,20,21])) == listToMD5(UTILS.excludeLines("some/other/local.txt", [1,3,7,8,9,11,12,13,14,15,20,21])) },
# or to compare 
{ assert snapshot(
    listToMD5(UTILS.excludeLines("$outputDir/foo.txt", [1,3,7,8,9,11,12,13,14,15,20,21]))
  ).match() }

The Groovy code could probably be improved, still, it might be useful this as an nft-utils function.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions