通过更改前缀来重命名一组文件

发布于 2024-12-22 16:32:50 字数 654 浏览 0 评论 0原文

我有一组四个目录,

 English.lproj
 German.lproj
 French.lproj
 Italian.lprj

每个目录都包含一系列 XML,名为

 2_symbol.xml
 4_symbol.xml
 5_symbol.xml
 ... and so on ...

I need to rename all of those files into 另一个数字模式, 因为确定这些数字的代码已经改变。 所以新的数字模式就像这样

 1_symbol.xml
 5_symnol.xml
 3_symbol.xml
 ... and so on ...

,没有适用于确定这个系列的算法,因为这个 我考虑将两个数字系列存储到数组中的原因。

我正在考虑使用简单的 bash 脚本快速完成此操作。 我认为我需要一个数组来存储旧的数字模式和另一个 数组来存储新的数字模式,以便我可以执行一个循环来提出

 # move n_symbol.xml newdir/newval_symbol.xml

任何建议?

谢谢,干杯。

-k-

I've got a set of four directories

 English.lproj
 German.lproj
 French.lproj
 Italian.lprj

each of this contains a serie of XMLs named

 2_symbol.xml
 4_symbol.xml
 5_symbol.xml
 ... and so on ...

I need to rename all of these files into another numerical pattern,
because the code that determined those numbers have changed.
so the new numerical pattern would be like

 1_symbol.xml
 5_symnol.xml
 3_symbol.xml
 ... and so on ...

so there's no algorithm applicable to determine this serie, because of this
reason I thought about storing the two numerical series into an array.

I was thinking to a quick way of doing it with a simple bash script.
I think that I'd need an array to store the old numerical pattern and another
array to store the new numerical pattern, so that I can perform a cycle to make

 # move n_symbol.xml newdir/newval_symbol.xml

any suggestion?

thx n cheers.

-k-

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雪落纷纷 2024-12-29 16:32:50

为此,您不需要 bash,任何 POSIX 兼容的 shell 都可以。

repls="1:4 2:1 4:12 5:3"

for pair in $repls; do
  old=${pair%:*}
  new=${pair#*:}
  file=${old}_symbol.xml
  mv $file $new${file#$old}
done

编辑:您需要注意覆盖文件。例如,上面的代码片段 clobbers 4_symbol.xml。

for pair in $repls; do
  ...
  mv $file $new${file#$old}.tmp
done
for f in *.tmp; do
  mv $f ${f%.tmp}
done

you don't need bash for this, any POSIX-compatible shell will do.

repls="1:4 2:1 4:12 5:3"

for pair in $repls; do
  old=${pair%:*}
  new=${pair#*:}
  file=${old}_symbol.xml
  mv $file $new${file#$old}
done

edit: you need to take care of overwriting files. the snippet above clobbers 4_symbol.xml, for example.

for pair in $repls; do
  ...
  mv $file $new${file#$old}.tmp
done
for f in *.tmp; do
  mv $f ${f%.tmp}
done
小女人ら 2024-12-29 16:32:50

以下脚本将随机调整“lproj”目录中所有 xml 文件的符号名称。

#!/bin/bash

shuffle() { # Taken from http://mywiki.wooledge.org/BashFAQ/026
    local i tmp size max rand

    size=${#array[*]}
    max=$(( 32768 / size * size ))

    for ((i=size-1; i>0; i--)); do
        while (( (rand=$RANDOM) >= max )); do :; done
        rand=$(( rand % (i+1) ))
        tmp=${array[i]} array[i]=${array[rand]} array[rand]=$tmp
    done
}

for file in *lproj/*.xml; do # get an array of symbol names
    tmp=${file##*/}
    array[$((i++))]=${tmp%%_*}
done

shuffle # shuffle the symbol name array

i=0
for file in *lproj/*.xml; do # rename the files with random symbols
    echo mv "$file" "${file%%/*}/${array[$((i++))]}_${file##*_}"
done

注意:当您对结果满意时,删除mv前面的echo并重新运行脚本使更改永久生效。

脚本输出

$ ./randomize.sh
mv 1.lproj/1_symbol.xml 1.lproj/16_symbol.xml
mv 1.lproj/2_symbol.xml 1.lproj/12_symbol.xml
mv 1.lproj/3_symbol.xml 1.lproj/6_symbol.xml
mv 1.lproj/4_symbol.xml 1.lproj/4_symbol.xml
mv 2.lproj/5_symbol.xml 2.lproj/14_symbol.xml
mv 2.lproj/6_symbol.xml 2.lproj/1_symbol.xml
mv 2.lproj/7_symbol.xml 2.lproj/3_symbol.xml
mv 2.lproj/8_symbol.xml 2.lproj/7_symbol.xml
mv 3.lproj/10_symbol.xml 3.lproj/10_symbol.xml
mv 3.lproj/11_symbol.xml 3.lproj/11_symbol.xml
mv 3.lproj/12_symbol.xml 3.lproj/2_symbol.xml
mv 3.lproj/9_symbol.xml 3.lproj/8_symbol.xml
mv 4.lproj/13_symbol.xml 4.lproj/13_symbol.xml
mv 4.lproj/14_symbol.xml 4.lproj/15_symbol.xml
mv 4.lproj/15_symbol.xml 4.lproj/9_symbol.xml
mv 4.lproj/16_symbol.xml 4.lproj/5_symbol.xml

The following script will randomly shuffle the symbol names of all xml files across 'lproj' directories.

#!/bin/bash

shuffle() { # Taken from http://mywiki.wooledge.org/BashFAQ/026
    local i tmp size max rand

    size=${#array[*]}
    max=$(( 32768 / size * size ))

    for ((i=size-1; i>0; i--)); do
        while (( (rand=$RANDOM) >= max )); do :; done
        rand=$(( rand % (i+1) ))
        tmp=${array[i]} array[i]=${array[rand]} array[rand]=$tmp
    done
}

for file in *lproj/*.xml; do # get an array of symbol names
    tmp=${file##*/}
    array[$((i++))]=${tmp%%_*}
done

shuffle # shuffle the symbol name array

i=0
for file in *lproj/*.xml; do # rename the files with random symbols
    echo mv "$file" "${file%%/*}/${array[$((i++))]}_${file##*_}"
done

Note: Remove the echo in front of the mv when you are satisfied with the results and re-run the script to make the changes permanent.

Script Output

$ ./randomize.sh
mv 1.lproj/1_symbol.xml 1.lproj/16_symbol.xml
mv 1.lproj/2_symbol.xml 1.lproj/12_symbol.xml
mv 1.lproj/3_symbol.xml 1.lproj/6_symbol.xml
mv 1.lproj/4_symbol.xml 1.lproj/4_symbol.xml
mv 2.lproj/5_symbol.xml 2.lproj/14_symbol.xml
mv 2.lproj/6_symbol.xml 2.lproj/1_symbol.xml
mv 2.lproj/7_symbol.xml 2.lproj/3_symbol.xml
mv 2.lproj/8_symbol.xml 2.lproj/7_symbol.xml
mv 3.lproj/10_symbol.xml 3.lproj/10_symbol.xml
mv 3.lproj/11_symbol.xml 3.lproj/11_symbol.xml
mv 3.lproj/12_symbol.xml 3.lproj/2_symbol.xml
mv 3.lproj/9_symbol.xml 3.lproj/8_symbol.xml
mv 4.lproj/13_symbol.xml 4.lproj/13_symbol.xml
mv 4.lproj/14_symbol.xml 4.lproj/15_symbol.xml
mv 4.lproj/15_symbol.xml 4.lproj/9_symbol.xml
mv 4.lproj/16_symbol.xml 4.lproj/5_symbol.xml
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文