Noob splitText question

I am a beginner with nextflow.

I have a file with sample ids, one per line. I read from this with splitText() but the values this returns each end with a newline character. How can I get rid of these? The documentation says “Text chunks returned by the splitText operator are always terminated by a \n newline character” but unhelpfully does not suggest what to do about this. Is there a better function to use (something like Python’s readlines?) Can I do a string substitution? I’ve been looking for an answer now for hours and hours and appreciate any pointers, thanks.

You can use the trim method to get rid of the newline character. Try creating a text file named input.txt with 4 words, one word per line, then run the Nextflow code below:

Channel
  .fromPath('input.txt')
  .splitText()
  .map { it.trim() }
  .view()

You’ll notice the newline is gone (when compared to running the code above without the .map { it.trim() } line.

1 Like