0

I'm trying to take the filename of each file in a directory and 'rename' it to create a respective output file when running through a program. When running the script I get the error Permission denied, for the line that is meant to be doing the renaming.

outputname=basename $file | sed -e "s/_Aligned.sortedByCoord.out.bam/_.gtf/"

For example one file is named 92_Aligned.sortedByCoord.out.bam, I want the output to be 92_.gtf. Another file is called 10.5_rep1_Aligned.sortedByCoord.out.bam, and the output should be called 10.5_rep1_.gtf.

Am I doing it wrong? Not sure if sed is the right way as I'm technically not renaming the file, but creating another file from that name and changing it?

sian
  • 77
  • 7
  • What do you think `outputname=basename $file` does? – oguz ismail Oct 23 '19 at 11:44
  • here $file refers to file=`ls $root/Genome/*.bam`, so for each file it will take only the last part of the file path, e.g 92_Aligned.sortedByCoord.out.bam – sian Oct 23 '19 at 11:50

1 Answers1

1

You want to force evaluation on basename-sed pipe. ( with '$(' ) and handle the quoting)

outputname=$(basename $file | sed -e "s/_Aligned.sortedByCoord.out.bam/_.gtf/")

Or using Bash built-in only, which will be much FASTER.

file_base=${file##*/}
outputname=${file_base/_Aligned.sortedByCoord.out.bam/_.gtf}
dash-o
  • 13,723
  • 1
  • 10
  • 37