简体   繁体   English

从bash文件名中删除字符

[英]remove characters from filename in bash

I am trying to remove specific characters from a file in bash but am not getting the desired result. 我正在尝试从bash的文件中删除特定字符,但没有得到期望的结果。

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. 我试图将*移到末尾并使用/_variants_/和相同的结果。 Thank you :). 谢谢 :)。

${var%%*foo} removes a string ending with foo from the end of the value of var . ${var%%*foo}var的值末尾删除以foo结尾的字符串。 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 . 我猜您希望${var##*foo}从一开始就修剪到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_}"
 find . -type f -name "*.vcf.gz" -exec bash -c 'var="$1";mv $var ${var/TSVC_variants_/}' _ {} \;

可以为你做工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM