0

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 :).

2
  • The code you posted could not possibly have trimmed the directory part from the full file name. Commented May 9, 2016 at 18:31
  • @chris : Is TSVC_variants_ constant here? Commented May 9, 2016 at 19:25

3 Answers 3

4

${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_}" 
Sign up to request clarification or add additional context in comments.

Comments

1
 find . -type f -name "*.vcf.gz" -exec bash -c 'var="$1";mv $var ${var/TSVC_variants_/}' _ {} \; 

may do the job for you .

Comments

1

Simple way of doing this for a single file or if other files are named TSVC_variants_* in the same directory as the script or terminal.

Here is a simple one liner that will remove the TSVC_variants_* from all files with that listed in the front of the name.

rename 's/TSVC_variants_//;' * 

output: IonXpress_004.vcf.gz

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.