いくつかの計算に基づいてファイル名を変更する

いくつかの計算に基づいてファイル名を変更する

次のファイルがあります。

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

など..

ここで、インデックス番号を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

おすすめ記事