【问题标题】:bash to rename specific file type in directorybash 重命名目录中的特定文件类型
【发布时间】:2016-11-24 14:49:52
【问题描述】:

我正在尝试使用bash 重命名特定的file type,即.bam.bai,并且无法删除文件名中第二个下划线_ 之后的文本。目前我只得到一个在第一个_ 之后重命名的文件,我认为其他两个文件被删除,因为它们是重复的。谢谢你,祝你假期愉快:)。

/home/cmccabe/example/folder 中的文件

IonXpress_007_MEVxz_R_2016_11_18_10_45_10_user_S5-00580-14-Medexome.bam.bai
IonXpress_008_MEVxx_R_2016_11_18_10_45_10_user_S5-00580-14-Medexome.bam.bai
IonXpress_009_MEVxy_R_2016_11_18_10_45_10_user_S5-00580-14-Medexome.bam.bai

期望的输出

IonXpress_007.bam.bai
IonXpress_008.bam.bai
IonXpress_009.bam.bai

重击

for file in /home/cmccabe/example/folder/*.bam.bai; do
mv -- "$file" "${file%%_[0-9][0-9][0-9]_*}.bam.bai
done

电流输出

IonXpress

【问题讨论】:

  • 除了mv 命令中缺少双引号字符之外,您是否尝试过常识选项,例如在循环中包含echo mv -- "$file" "${file%%_[0-9][0-9][0-9]_*}.bam.bai" 之类的调试行?
  • 您的代码从新名称中删除了 _009_ 子字符串。 ${file%%_[0-9][0-9][0-9]_*} 扩展为 IonXpress。

标签: bash


【解决方案1】:

如果您有 rename 实用程序,那么您可以使用:

rename -n 's/^([^_]+_[^_]+)_.+$/$1.bam.bai/' *.bam.bai

'IonXpress_007_MEVxz_R_2016_11_18_10_45_10_user_S5-00580-14-Medexome.bam.bai' would be renamed to 'IonXpress_007.bam.bai'
'IonXpress_008_MEVxx_R_2016_11_18_10_45_10_user_S5-00580-14-Medexome.bam.bai' would be renamed to 'IonXpress_008.bam.bai'
'IonXpress_009_MEVxy_R_2016_11_18_10_45_10_user_S5-00580-14-Medexome.bam.bai' would be renamed to 'IonXpress_009.bam.bai'

如果您没有rename,那么您可以使用循环遍历这些文件并使用cut:

for f in *.bam.bai; do echo mv "$f" "$(cut -d_ -f1-2 <<< "$f").bam.bai"; done

mv IonXpress_007_MEVxz_R_2016_11_18_10_45_10_user_S5-00580-14-Medexome.bam.bai IonXpress_007.bam.bai
mv IonXpress_008_MEVxx_R_2016_11_18_10_45_10_user_S5-00580-14-Medexome.bam.bai IonXpress_008.bam.bai
mv IonXpress_009_MEVxy_R_2016_11_18_10_45_10_user_S5-00580-14-Medexome.bam.bai IonXpress_009.bam.bai

如果您对输出感到满意,请在 mv 之前删除 echo。

【讨论】:

  • 使用rename 实用程序有效,但它只重命名stdout 中的文件,而不是目录中的文件。谢谢你:)。
  • 这是因为我使用rename -n 进行试运行。只需使用rename 's/^([^_]+_[^_]+)_.+$/$1.bam.bai/' *.bam.bai 重命名它们。
  • 谢谢,不熟悉rename,但感谢您介绍我和帮助:)。
【解决方案2】:

${file%%_[0-9][0-9][0-9]_*} 删除任何字符,直到最左边的下划线字符,包括三位数字及其周围的下划线。因此,它扩展到IonXpress。

相反,我的代码从右到左删除任何字符,直到第二个下划线后跟 MEV 子字符串:

for file in /home/cmccabe/example/folder/*.bam.bai
do
      mv -- "$file" "${file%%_MEV*}".bam.bai
done

另一种选择是从文件名中选择最左边的 13 个字符:

  mv -- "$file" "${file::13}".bam.bai

【讨论】:

    【解决方案3】:

    一个可靠的方法是:

    #!/bin/bash
    mydir=/home/cmccabe/example/folder
    regex="^([^_]+_[^_]+)"                 # Match a sequence of several not "_"
                                           # followed by a "_" and by
                                           # a second sequence of not "_".
    
    cd "$mydir"                            # work only on files inside mydir.
    shopt -s nullglob                      # Make the pattern null if no file.
    
    for file in ./[^.].bam.bai; do         # To avoid matching a renamed file.
        [[ $file =~ $regex ]];             # test if the file match the regex.
        f="./${BASH_REMATCH[1]}.bam.bai"
        echo \
        mv "./$file" "$f"                  # execute the rename.
    done
    

    【讨论】:

      猜你喜欢
      • 2017-06-21
      • 2014-04-27
      • 2015-08-02
      • 2016-06-27
      • 2021-11-27
      • 2016-08-03
      • 2018-04-10
      • 2011-10-20
      • 2019-02-05
      相关资源
      最近更新 更多