在终端中批量重命名,通过从 .csv 文件附加前缀。

huangapple go评论52阅读模式
英文:

Batch rename in Terminal by appending prefix from .csv file

问题

首先步骤:
需要将国家代码前缀添加到每个相应的文件中。
例如:
EN_01-NameOfProject_Data Set 1-01.png

第二步:
需要删除部分 _Data Set X-01
结果将是:
EN_01-NameOfProject.png

我请求帮助执行上述两个步骤,以便更好地了解该过程。
我需要通过 macOS 终端完成这个操作。我已经使用 Homebrew 安装了 rename,命令如下:

brew install rename
英文:

Edit (to improve question):

在终端中批量重命名,通过从 .csv 文件附加前缀。

In the image above you can see the naming of a series of files.

Each file has a filename of:
01-NameOfProject_Data Set X-01.png
where X is sequential from 1 to 24
(please note! NOT 01, 02, ..., 24)

I have created two .csv files (to explore which works better upon your answer) as shown in the picture.

First step:
I need to add the country-code prefix to each corresponding file.
E.g.
EN_01-NameOfProject_Data Set 1-01.png

Second step:
I need to remove the part _Data Set X-01
The result would be
EN_01-NameOfProject.png

I am kindly asking for help in the above two steps so I can better understand the procedure.
I need to do this via macOS Terminal. I have install rename via Homebrew with
brew install rename

Original question:
I have a list of files that look like this:

01-NameOfProject_Data Set 1-01.png
01-NameOfProject_Data Set 2-01.png
01-NameOfProject_Data Set 3-01.png
...
01-NameOfProject_Data Set 24-01.png

I also have an Excel file (which I can save as .csv).
It has a single column and exactly as many rows as the amount of the aforementioned files.
Each cell has a language shortcode.
It looks like this:

EN
GR
IT
...
BU

The Excel file in the correct (corresponding) order as the files meaning that:
01-NameOfProject_Data Set 1-01.png is indeed in English (EN) and so on.

How would you batch rename the files so you'd end up with something like:

EN_01-NameOfProject_new-text.png
GR_01-NameOfProject_new-text.png
IT_01-NameOfProject_new-text.png
...
BU_01-NameOfProject_new-text.png

答案1

得分: 0

brew install rename

在你满意的情况下,移除 -n 开关,也就是_干跑_,以便真正重命名

#!/usr/bin/env bash

c=0 arr=( * )
while IFS=, read -r _ country; do
    rename -n "s/^(\d+).*/${country}_${1}NameOfProject_new-text.png/" "${arr[c++]}"
done < /tmp/file.csv

输出

rename(01-NameOfProject_Data Set 1-01.png, EN_NameOfProject_new-text.png)
rename(01-NameOfProject_Data Set 2-01.png, GR_NameOfProject_new-text.png)
rename(01-NameOfProject_Data Set 24-01.png, IT_NameOfProject_new-text.png)
rename(01-NameOfProject_Data Set 3-01.png, BU_NameOfProject_new-text.png)

如果顺序不是_自然_的,那么使用:

#!/usr/bin/env bash

readarray -td '' files < <(
    ls --zero -v -- *.png
)
c=0
while IFS=, read -r _ country; do
    rename -0 -n "s/^(\d+).*/${country}_${1}NameOfProject_new-text.png/" "${files[c++]}"
done < /tmp/file.csv

输出

rename(01-NameOfProject_Data Set 1-01.png, EN_NameOfProject_new-text.png)
rename(01-NameOfProject_Data Set 2-01.png, GR_NameOfProject_new-text.png)
rename(01-NameOfProject_Data Set 3-01.png, IT_NameOfProject_new-text.png)
rename(01-NameOfProject_Data Set 24-01.png, BU_NameOfProject_new-text.png)
英文:
brew install rename

Remove -n switch, aka dry-run when your attempts are satisfactory to rename for real.

#!/usr/bin/env bash

c=0 arr=( * )
while IFS=, read -r _ country; do
    rename -n &quot;s/^(\d+).*/${country}_NameOfProject_new-text.png/&quot; &quot;${arr[c++]}&quot;
done &lt; /tmp/file.csv

Output

rename(01-NameOfProject_Data Set 1-01.png, EN_NameOfProject_new-text.png)
rename(01-NameOfProject_Data Set 2-01.png, GR_NameOfProject_new-text.png)
rename(01-NameOfProject_Data Set 24-01.png, IT_NameOfProject_new-text.png)
rename(01-NameOfProject_Data Set 3-01.png, BU_NameOfProject_new-text.png)

If order is not natural, then use:

#!/usr/bin/env bash

readarray -td &#39;&#39; files &lt; &lt;(
    ls --zero -v -- *.png
)
c=0
while IFS=, read -r _ country; do
    rename -0 -n &quot;s/^(\d+).*/${country}_NameOfProject_new-text.png/&quot; &quot;${files[c++]}&quot;
done &lt; /tmp/file.csv

Output

rename(01-NameOfProject_Data Set 1-01.png, EN_NameOfProject_new-text.png)
rename(01-NameOfProject_Data Set 2-01.png, GR_NameOfProject_new-text.png)
rename(01-NameOfProject_Data Set 3-01.png, IT_NameOfProject_new-text.png)
rename(01-NameOfProject_Data Set 24-01.png, BU_NameOfProject_new-text.png)

huangapple
  • 本文由 发表于 2023年3月7日 03:25:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75655006.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定