0

I have the following sed command that change the chromosome name:

for file in /myoldpath/*.bam; do filename=`echo $file | cut -d "." -f 1`; samtools view -H $file | sed -e 's/SN:\([0-9XY]\)/SN:chr\1/' -e 's/SN:MT/SN:chrM/' | samtools reheader - $file > /mynewpath/${filename}_chr.bam; done  

My quesion is how to insert the result in a new path while keeping the variable $filename as part of every new file name? It always inserts the result in /myoldpath/ or leterally "filename.chr.bam" in the /mynewpath/. am i missing something in the syntax of that part $file > /mynewpath/${filename}_chr.bam?

any help would be appreciated

Iurii Drozdov
  • 1,685
  • 1
  • 12
  • 23
Alexis_543
  • 33
  • 8

1 Answers1

0

Try to change filename=echo $file | cut -d "." -f 1; to filename=$(echo $file| rev| cut -d "/" -f 1|rev|cut -d "." -f1 );

Iurii Drozdov
  • 1,685
  • 1
  • 12
  • 23
  • Thank you @lurii for the response. I tried this one but i got an output in the form of "_chr_bam". here is what i tried: `for file in /myoldpath/*.bam; do filename=$(echo $file | cut -d "." -f 1); samtools view -H $file | sed -e 's/SN:([0-9XY])/SN:chr\1/' -e 's/SN:MT/SN:chrM/' | samtools reheader - $file > /mynewpath/${filename}_chr.bam; done` – Alexis_543 Jan 06 '17 at 09:57
  • what is a file name look like if you change your command to /mynewpath/${file}_chr.bam ? – Iurii Drozdov Jan 06 '17 at 10:23
  • you can try this filename=$(echo $file| rev| cut -d "/" -f 1|rev|cut -d "." -f1 ); – Iurii Drozdov Jan 06 '17 at 10:38