일부 계산을 기반으로 파일 이름 바꾸기

일부 계산을 기반으로 파일 이름 바꾸기

다음과 같은 파일이 있습니다.

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

관련 정보