Issue
I am trying to remove specific characters from a file in bash
but am not getting the desired result.
bash
for file in /home/cmccabe/Desktop/NGS/API/test/*.vcf.gz; do
mv -- "$file" "${file%%/*_variants_}.vcf.gz"
done
file name
TSVC_variants_IonXpress_004.vcf.gz
desired resuult
IonXpress_004.vcf.gz
current result (extention in filename repeats)
TSVC_variants_IonXpress_004.vcf.gz.vcf.gz
I have tried to move the *
to the end and to use /_variants_/
and the same results. Thank you :).
Solution
${var%%*foo}
removes a string ending with foo
from the end of the value of var
. If there isn't a suffix which matches, nothing is removed. I'm guessing you want ${var##*foo}
to trim from the beginning, up through foo
. You'll have to add the directory path back separately if you remove it, of course.
mv -- "$file" "/home/cmccabe/Desktop/NGS/API/test/${file##*_variants_}"
Answered By - tripleee Answer Checked By - Mary Flores (WPSolving Volunteer)