substitute file names using rename

蓝咒 提交于 2019-12-26 12:10:31

问题


I want to rename files names by substituting all the characters starting from "_ " followed by eight capital letter and keep only the extension.

4585_10_148_H2A119Ub_GTCTGTCA_S51_mcdf_mdup_ngsFlt.fm
4585_10_148_H3K27me3_TCTTCACA_S51_mcdf_mdup_ngsFlt.fm
4585_27_128_Bap1_Bethyl_ACAGATTC_S61_mcdf_mdup_ngsFlt.fw
4585_32_148_1_INPUT_previous_AGAGTCAA_S72_mcdf_mdup_ngsFlt.bw

expected output

4585_10_148_H2A119Ub.fm
4585_10_148_H3K27me3.fm
4585_27_128_Bap1_Bethyl.fm
4585_32_148_1_INPUT_previous.fm

回答1:


Try this:

for f in *; do
    target=$(echo "${f}" | sed -E 's/_[[:upper:]]{8}.*\././')
    mv "${f}" "${target}"
done

The key thing is the -E argument to sed, since it enables expanded regular expressions.




回答2:


You can also use rename (a.k.a. prename or Perl rename) like this:

rename --dry-run 's|_[[:upper:]]{8}.*\.|.|' *

Sample Output

'4585_10_148_H2A119Ub_GTCTGTCA_S51_mcdf_mdup_ngsFlt.fm' would be renamed to '4585_10_148_H2A119Ub.fm'
'4585_32_148_1_INPUT_previous_AGAGTCAA_S72_mcdf_mdup_ngsFlt.bw' would be renamed to '4585_32_148_1_INPUT_previous.bw'

Remove the --dry-run and run again for real, if the output looks good.

This has several added benefits:

  • that it will warn and avoid any conflicts if two files rename to the same thing,
  • that it can rename across directories, creating any necessary intermediate directories on the way,
  • that you can do a dry run first to test it,
  • that you can use arbitrarily complex Perl code to specify the new name.

On a Mac, install it with homebrew using:

brew install rename



回答3:


You may try this.

for i in *.fm; do mv $i $(echo $i | sed 's/_GTCTGTCA_S51_mcdf_mdup_ngsFlt//g'); done;
for i in *.fm; do mv $i $(echo $i | sed 's/_TCTTCACA_S51_mcdf_mdup_ngsFlt//g'); done;


来源:https://stackoverflow.com/questions/49407882/substitute-file-names-using-rename

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!