【问题标题】:rename all files of a specific type in a directory重命名目录中特定类型的所有文件
【发布时间】:2017-06-21 20:13:06
【问题描述】:

我正在尝试使用bash 重命名目录中与特定模式匹配的所有.txt 文件。我在下面的两次尝试已从目录中删除了文件并引发了错误。谢谢你:)

输入

16-0000_File-A_variant_strandbias_readcount.vcf.hg19_multianno_dbremoved_removed_final_index_inheritence_import.txt
16-0002_File-B_variant_strandbias_readcount.vcf.hg19_multianno_dbremoved_removed_final_index_inheritence_import.txt

期望的输出

16-0000_File-A_multianno.txt
16-0002_File-B_multianno.txt

Bash 尝试 1 this removes the files from the directory

for f in /home/cmccabe/Desktop/test/vcf/overall/annovar/*_classify.txt ; do
 # Grab file prefix.
 p=${f%%_*_}
 bname=`basename $f`
 pref=${bname%%.txt}
 mv "$f" ${p}_multianno.txt
done

Bash 尝试 2 Substitution replacement not terminated at (eval 1) line 1.

for f in /home/cmccabe/Desktop/test/vcf/overall/annovar/*_classify.txt ; do
 # Grab file prefix.
 p=${f%%_*_}
 bname=`basename $f`
 pref=${bname%%.txt}
 rename -n 's/^$f/' *${p}_multianno.txt
done

【问题讨论】:

    标签: bash rename


    【解决方案1】:

    您不需要循环。 rename一个人就可以做到:

    rename -n 's/(.*?_[^_]+).*/${1}_multianno.txt/g' /home/cmccabe/Desktop/test/vcf/overall/annovar/*_classify.txt
    

    正则表达式的意思大致是, 捕获从开始到第二个_ 的所有内容, 匹配其余部分, 并替换为捕获的前缀并附加_multianno.txt

    使用-n 标志,此命令将打印它在不实际执行的情况下会执行的操作。 当输出看起来不错时,删除-n 并重新运行。

    【讨论】:

    • 非常感谢您的大力帮助和解释,我很感激他们:)
    猜你喜欢
    • 1970-01-01
    • 2020-12-17
    • 2014-04-27
    • 2014-05-16
    • 2015-02-28
    • 2014-11-08
    • 2014-10-09
    • 2014-10-14
    • 2015-04-26
    相关资源
    最近更新 更多