Save part of string

Hi!

I have run into an issue and need some help.

I’m creating a workflow where a part of the output of one process is the input of another process. For the first process I’m saving the output as a .txt file. The content of the file is “Project id: 001”. However I only want the actual project id as input for the next process, therefore I only want to save “001”.

The code (related to this issue) I have so far is the following:

   // create a channel for input of project id
    project_ch = Channel.fromPath('./results/output.txt').splitText()

This gives me “Project id: 001”. However, I cannot seem to understand how I can remove a part of the string so that it gives me “001”. If possible I would like this to be done by adjusting the channel.

Can someone please guide me on how to solve this, thank you :slight_smile:

The simplest solution is to use the .map operator to apply String functions to the splitText() output.

project_ch = Channel.fromPath('./results/output.txt')
    .splitText()
    .map { str -> str.tokenize(':')[1].trim() }
    .view()

You can also use Seqera’s Ask AI to provide more string manipulation suggestions.

The Nextflow documentation also gives a list of recommended String functions: Types — Nextflow documentation

Thank you for that solution!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.