根据一些计算重命名文件名

根据一些计算重命名文件名

我有一些这样的文件:

file.i001.trusted.txt
file.i002.trusted.txt
...
...
file.i212.trusted.txt

ETC..

现在我想将索引号从i001更改为i030,将A101更改为A130,将i031更改为i060,将A201更改为A230。

我主要在 FreeBSD 下使用“renamex”(它有正则表达式支持)。

Usage: renamex [OPTIONS] filename ...
OPTIONS:
  -f, --file              Load file names from the file
  -l, --lowercase         Lowercase the file name
  -u, --uppercase         Uppercase the file name
  -s/PATTERN/STRING[/SW]  Replace the matching PATTERN with STRING.
                          The SW could be:
                          [i] ignore case when searching
                          [b] backward searching and replacing
                          [s] change file's suffix name
                          [r] PATTERN is regular expression
                          [e] PATTERN is extended regular expression
                          [g] replace all occurrences in the filename
                          [1-9] replace specified occurrences in the filename
  -R, --recursive         Operate on files and directories recursively
  -o, --owner OWNER       Change file's ownership (superuser only)
  -v, --verbose           Display verbose information
  -t, --test              Test only mode. Do not change any thing
  -h, --help              Display this help and exit
  -V, --version           Output version information and exit
  -A, --always            Always overwrite the existing files
  -N, --never             Never overwrite the existing files
Please see manpage regex(7) for the details of extended regular expression.

你有什么建议?

编辑:范围可能不同。所以不会总是 30 件。例如A1是30个项目,A2是40个项目,A3是25个等等......

答案1

zsh

autoload zmv # best in ~/.zshrc
zmv -n '(file.)i(<->)(.trusted.txt)' '$1A$(($2+30+70*(($2-1)/30+1)))$3'

(高兴时删除-n或管道sh)。

那会运行:

mv -- file.i001.trusted.txt file.A101.trusted.txt
mv -- file.i002.trusted.txt file.A102.trusted.txt
[...]
mv -- file.i029.trusted.txt file.A129.trusted.txt
mv -- file.i030.trusted.txt file.A130.trusted.txt
mv -- file.i031.trusted.txt file.A201.trusted.txt
mv -- file.i032.trusted.txt file.A202.trusted.txt
[...]
mv -- file.i059.trusted.txt file.A229.trusted.txt
mv -- file.i060.trusted.txt file.A230.trusted.txt
mv -- file.i061.trusted.txt file.A301.trusted.txt
mv -- file.i062.trusted.txt file.A302.trusted.txt
[...]
mv -- file.i211.trusted.txt file.A801.trusted.txt
mv -- file.i212.trusted.txt file.A802.trusted.txt

如果您只想处理前 60 个,则可以替换<->为。<1-60>

如果批次并不总是 30 个,您始终可以运行多个zmvs

i=1 j=100
for batch (30 40 30 50) {
  zmv -n "(file.)i(<$i-$((i+batch-1))>)(.trusted.txt)" \
         '$1A$(($2+j))$3'
  ((i += batch, j += 100 - batch))
}

这使:

mv -- file.i001.trusted.txt file.A101.trusted.txt
[...]
mv -- file.i030.trusted.txt file.A130.trusted.txt
mv -- file.i031.trusted.txt file.A201.trusted.txt
[...]
mv -- file.i070.trusted.txt file.A240.trusted.txt
mv -- file.i071.trusted.txt file.A301.trusted.txt
[...]
mv -- file.i100.trusted.txt file.A330.trusted.txt
mv -- file.i101.trusted.txt file.A401.trusted.txt
[...]
mv -- file.i150.trusted.txt file.A450.trusted.txt

相关内容