-1

I'm trying to rename files in folders for a bioinformatics project. There is a folder for each sample which is named, the files in the folders all have the same names which is no good for downstream processing so I am trying to rename them with a prefix. The prefix is chopped from the $folder, if I test it using for example " rename 's/^/bc01_/' * " it works fine, if I try to use the parameter expansion as mentioned it doesn't work, but I don't understand why? The error message given is below the code chunk, but the "$sampleID" is declared so I'm not understanding the error msg?

Any help much appreciated!

ls /homex/ddxxa/"$project"/medaka/barcode01Chop/

calls_to_draft.bam.bai  
consensus.fasta  
consensus.fasta.gaps_in_draft_coords.bed
consensus_probs.hdf

project=$1
refseq=$2 

for folder in /homex/ddxxa/"$project"/medaka/*Chop/ ; # $folder = barcode01Chop

do 

    sampleID=`echo "$folder" | sed -e 's/^.*\(barcode\)/bc/'| sed -e 's/Chop\/$//'`
# $sampleID = bc01

    rename 's/^/${sampleID}_/' *

# files renamed to 
bc01_calls_to_draft.bam.bai  
bc01_consensus.fasta  
bc01_consensus.fasta.gaps_in_draft_coords.bed
bc01_consensus_probs.hdf

done

Error msg:

Global symbol "$sampleID" requires explicit package name (did you forget to declare "my $sampleID"?) at (user-supplied code).

  • Please add sample input (no descriptions, no images, no links) and your desired output for that sample input to your question (no comment). – Cyrus Aug 14 '23 at 21:07
  • 1
    `rename 's/^/${sampleID}_/'` - the `rename` script is wrapped in single quotes; you need to use double quotes for the variable reference (`${sampleID}`) to be replaced with the variable's value – markp-fuso Aug 14 '23 at 21:14
  • @markp-fuso Thanks! I had a feeling it was going to be something in the syntax and something simple to fix, thanks for the help :) – fip_researcher Aug 14 '23 at 22:05
  • 1
    Mind, `rename` is serious overkill for just adding a prefix. `for path in "$folder"/*; do file=${path##*/}; [[ $file = $sampleId_* ]] && continue; mv -- "$folder/$file" "$folder/${sampleId}_$file"; done` -- that way you also can't prefix the sampleId more than once if you run the script multiple times against the same folder. – Charles Duffy Aug 14 '23 at 22:20
  • @CharlesDuffy I had actually had multiple prefixes in one of my test iterations. Thanks for the tip! – fip_researcher Aug 15 '23 at 17:09

0 Answers0