<p>Inside your script's command string, <em>assembly_id</em> is actually a <a href="https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html" rel="nofollow noreferrer"><code>java.lang.String</code></a> so trying to manipulate it with a Bash operation (i.e. using <code>${assembly_id%%_*}</code>) won't produce the expected result. Here's one way to return a substring up to the first underscore; note that we can use single quotes around our sed expression:</p>
<pre><code>process publish_gene_sequences {

    publishDir params.publish_dir

    input:
    tuple val(assembly_id), val(gene_id), path(&quot;gene.fasta&quot;)

    output:
    path(&quot;<span class="math-container">${gene_id}/$</span>{assembly_id}_${gene_id}.fasta&quot;)

    script:
    def assembly = assembly_id.substring(0, assembly_id.indexOf('_'))

    &quot;&quot;&quot;
    mkdir &quot;<span class="math-container">${gene_id}"
    cp gene.fasta "$</span>{gene_id}/<span class="math-container">${assembly_id}_$</span>{gene_id}.fasta&quot;
    sed -i 's/^&gt;/&gt;<span class="math-container">${assembly}_$</span>{gene_id}_/g' &quot;<span class="math-container">${gene_id}/$</span>{assembly_id}_${gene_id}.fasta&quot;
    &quot;&quot;&quot;
}
</code></pre>
<p>Note also that in the above example, copying the file can be avoided by applying the expression directly and redirecting stdout:</p>
<pre><code>script:
def assembly = assembly_id.substring(0, assembly_id.indexOf('_'))

&quot;&quot;&quot;
mkdir &quot;<span class="math-container">${gene_id}"
sed \\
    -e 's/^&gt;/&gt;$</span>{assembly}_<span class="math-container">${gene_id}_/g' \\
    gene.fasta \\
    &gt; "$</span>{gene_id}/<span class="math-container">${assembly_id}_$</span>{gene_id}.fasta&quot;
&quot;&quot;&quot;
</code></pre>

CC BY-SA 4.0
2022-11-02T16:18:47.363Z
That makes a lot more sense thank you for clarifying! That solution worked for me!
rimo
CC BY-SA 4.0
2022-11-02T00:01:38.293Z
2022-11-02T14:46:43.203Z
2022-11-02T14:46:43.203Z
Steve
<p>I was wondering if someone can help me figure out this error message. I’m at the end of my Nextflow pipeline and I want to change the header in a FASTA file and it works when I use this command:</p>
<p><code>sed -i &quot;s/^&gt;/&gt;${assembly_id}_${gene_id}_/g&quot; &quot;${assembly_id}/${gene_id}/${assembly_id}_${gene_id}.fasta&quot;</code></p>
<p>but it prints out much more than I actually need (i.e., “goodID_extra_extra_extra”). That’s just part of the file name (it includes what assembler was used) but I only want it to add “goodID_” to the header.</p>
<p>I tried using this command:</p>
<p><code>sed -i &quot;s/^&gt;/&gt;${assembly_id%%_*}_${gene_id}_/g&quot; &quot;${assembly_id}/${gene_id}/${assembly_id}_${gene_id}.fasta&quot;  </code></p>
<p>but I get this error message:</p>
<pre><code>N E X T F L O W  ~  version 21.10.6
Launching `geneTargets.nf` [amazing_elion] - revision: b875027a52
Script compilation error
- file : /Users/rimo/Desktop/AWS/nextflow_work/PROJECTS/gene_targets/geneTargets.nf
- cause: Unexpected input: '{' @ line 94, column 32.
   process publish_gene_sequences {
                                  ^

1 error
</code></pre>
<p>Can someone explain why this is happening? If this is something I can’t do without breaking the script that’s okay the header will still do it’s purpose I just wanted it to be clearer/less noise.</p>
<p>EDIT: this is the process block</p>
<pre><code>process publish_gene_sequences {
publishDir params.publish_dir
input:
    tuple val(assembly_id), val(gene_id), path(&quot;gene.fasta&quot;)
output:
    path(&quot;<span class="math-container">${gene_id}/$</span>{assembly_id}_<span class="math-container">${gene_id}.fasta")
script:
    """
    mkdir -p $</span>{gene_id}
    cp gene.fasta &quot;<span class="math-container">${gene_id}/$</span>{assembly_id}_<span class="math-container">${gene_id}.fasta"
    sed -i "s/^&gt;/&gt;$</span>{assembly_id}_<span class="math-container">${gene_id}_/g" "$</span>{gene_id}/<span class="math-container">${assembly_id}_$</span>{gene_id}.fasta&quot;
    &quot;&quot;&quot;
 }
</code></pre>

CC BY-SA 4.0
2022-11-01T18:51:01.770Z
Can you please share a minimal reproducible example? For now, I would guess this has something to do with escaping like this example here: https://github.com/nextflow-io/nextflow/issues/2082#issuecomment-1025569003
mribeirodantas
CC BY-SA 4.0
2022-11-01T20:34:00.597Z
I added the entire process block where the line of code I'm trying to use is @mribeirodantas
rimo
CC BY-SA 4.0
2022-11-01T21:08:09.963Z
Ideally, a minimal reproducible example would include fold structure, some sample files and a workflow call to this process block. Trying to guess things on my side, adding `-e` to the sed line after `-i` gave me no errors but it's guess work unless I can try to reproduce what you're finding in your machine :-\
mribeirodantas
<fasta><nextflow><errors><command-line><sed>
Using SED command in Nextflow script
# Using SED command in Nextflow script

(Asked by: username_0 on 2022-11-01)

I was wondering if someone can help me figure out this error message. I’m at the end of my Nextflow pipeline and I want to change the header in a FASTA file and it works when I use this command:
sed -i "s/^>/>${assembly_id}_${gene_id}_/g" "${assembly_id}/${gene_id}/${assembly_id}_${gene_id}.fasta"
but it prints out much more than I actually need (i.e., “goodID_extra_extra_extra”). That’s just part of the file name (it includes what assembler was used) but I only want it to add “goodID_” to the header.
I tried using this command:
sed -i "s/^>/>${assembly_id%%_*}_${gene_id}_/g" "${assembly_id}/${gene_id}/${assembly_id}_${gene_id}.fasta"  
but I get this error message:
N E X T F L O W  ~  version 21.10.6
Launching `geneTargets.nf` [amazing_elion] - revision: b875027a52
Script compilation error
- file : /Users/rimo/Desktop/AWS/nextflow_work/PROJECTS/gene_targets/geneTargets.nf
- cause: Unexpected input: '{' @ line 94, column 32.
   process publish_gene_sequences {
                                  ^

1 error

Can someone explain why this is happening? If this is something I can’t do without breaking the script that’s okay the header will still do it’s purpose I just wanted it to be clearer/less noise.
EDIT: this is the process block
process publish_gene_sequences {
publishDir params.publish_dir
input:
    tuple val(assembly_id), val(gene_id), path("gene.fasta")
output:
    path("${gene_id}/${assembly_id}_${gene_id}.fasta")
script:
    """
    mkdir -p ${gene_id}
    cp gene.fasta "${gene_id}/${assembly_id}_${gene_id}.fasta"
    sed -i "s/^>/>${assembly_id}_${gene_id}_/g" "${gene_id}/${assembly_id}_${gene_id}.fasta"
    """
 }

**Tags:** fasta, nextflow, errors, command-line, sed
> **Comments:**
> **username_1**: Can you please share a minimal reproducible example? For now, I would guess this has something to do with escaping like this example here: https://github.com/nextflow-io/nextflow/issues/2082#issuecomment-1025569003  
> **username_0**: I added the entire process block where the line of code I'm trying to use is @mribeirodantas  
> **username_1**: Ideally, a minimal reproducible example would include fold structure, some sample files and a workflow call to this process block. Trying to guess things on my side, adding `-e` to the sed line after `-i` gave me no errors but it's guess work unless I can try to reproduce what you're finding in your machine :-\  

---

## 1 answers

### Answer by username_2 on 2022-11-02. Score: 5 | Accepted answer

Inside your script's command string, assembly_id is actually a java.lang.String so trying to manipulate it with a Bash operation (i.e. using ${assembly_id%%_*}) won't produce the expected result. Here's one way to return a substring up to the first underscore; note that we can use single quotes around our sed expression:
process publish_gene_sequences {

    publishDir params.publish_dir

    input:
    tuple val(assembly_id), val(gene_id), path("gene.fasta")

    output:
    path("${gene_id}/${assembly_id}_${gene_id}.fasta")

    script:
    def assembly = assembly_id.substring(0, assembly_id.indexOf('_'))

    """
    mkdir "${gene_id}"
    cp gene.fasta "${gene_id}/${assembly_id}_${gene_id}.fasta"
    sed -i 's/^>/>${assembly}_${gene_id}_/g' "${gene_id}/${assembly_id}_${gene_id}.fasta"
    """
}

Note also that in the above example, copying the file can be avoided by applying the expression directly and redirecting stdout:
script:
def assembly = assembly_id.substring(0, assembly_id.indexOf('_'))

"""
mkdir "${gene_id}"
sed \\
    -e 's/^>/>${assembly}_${gene_id}_/g' \\
    gene.fasta \\
    > "${gene_id}/${assembly_id}_${gene_id}.fasta"
"""

> **Comments:**
> **username_0**: That makes a lot more sense thank you for clarifying! That solution worked for me!  

---

