如何重命名bash中的文件以增加名称?

编程入门 行业动态 更新时间:2024-10-23 18:32:53
本文介绍了如何重命名bash中的文件以增加名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有几千个文件,命名如下:

I have a few thousand files named as follows:

Cyprinus_carpio_600_nanopore_trim_reads.fasta Cyprinus_carpio_700_nanopore_trim_reads.fasta Cyprinus_carpio_800_nanopore_trim_reads.fasta Cyprinus_carpio_900_nanopore_trim_reads.fasta Vibrio_cholerae_3900_nanopore_trim_reads.fasta

对于前两个单词的80个变体(80个不同的种类),我想重命名所有这些文件,以使数字增加100-例如:

for 80 variations of the first two words (80 different species), i would like to rename all of these files such that the number is increased by 100 - for example:

Vibrio_cholerae_3900_nanopore_trim_reads.fasta

将成为

Vibrio_cholerae_4000_nanopore_trim_reads.fasta

Cyprinus_carpio_300_nanopore_trim_reads.fasta

将成为

Cyprinus_carpio_400_nanopore_trim_reads.fasta

不幸的是,我无法弄清楚如何重命名它们,我对遵循 unix.stackexchange/questions/40523/rename-files-在文件名内递增编号

Unfortunately I can't work out how to get to rename them, i've had some luck with following the solutions on unix.stackexchange/questions/40523/rename-files-by-incrementing-a-number-within-the-filename

但是我无法在名称中使用它,如果有帮助,我可以在Ubuntu 18.04上运行

But i can't get it to work for the inside of the name, i'm running on Ubuntu 18.04 if that helps

推荐答案

如果您可以掌握Perl风格的rename版本,则很简单,就像这样:

If you can get hold of the Perl-flavoured version of rename, that is simple like this:

rename -n 's/(\d+)/$1 + 100/e' *fasta

示例输出

'Ciprianus_maximus_11_fred.fasta' would be renamed to 'Ciprianus_maximus_111_fred.fasta' 'Ciprianus_maximus_300_fred.fasta' would be renamed to 'Ciprianus_maximus_400_fred.fasta' 'Ciprianus_maximus_3900_fred.fasta' would be renamed to 'Ciprianus_maximus_4000_fred.fasta'

如果您看不懂Perl,则表示... 执行以下单个替换.无论何时在一行中看到一串彼此相邻的数字(\d+),请记住它们(因为我将其放在括号中),然后将其替换为该串数字的计算表达式($1)加100." .

If you can't read Perl, that says... "Do a single substitution as follows. Wherever you see a bunch of digits next to each other in a row (\d+), remember them (because I put that in parentheses), and then replace them with the evaluated expression of that bunch of digits ($1) plus 100.".

如果空运行看起来正确,则删除-n.唯一的棘手部分" 是替换结尾处使用e,这意味着求值替换中的表达式-或者我称其为 计算得出的替换" .

Remove the -n if the dry-run looks correct. The only "tricky part" is the use of e at the end of the substitution which means evaluate the expression in the substitution - or I call it a "calculated replacement".

更多推荐

如何重命名bash中的文件以增加名称?

本文发布于:2023-11-04 05:28:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1557072.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重命名   名称   文件   bash

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!