批处理重命名匹配文件使用第一字段替换为搜索条件

发布于 2025-01-29 02:27:30 字数 1321 浏览 4 评论 0原文

我有很多文件选择,例如。

foo_de.vtt,foo_en.vtt,foo_es.vtt,foo_fr.vtt,foo_pt.vtt,baa_de.vtt,baa_en.vtt,baa_en.vtt,baa_es.vtt,baa_es.vtt >

我创建了一个选项卡分离的文件,filenames.txt包含当前字符串和替换字符串EG。

foo    1000
baa    1016
...etc

我想重命名所有文件以获取以下内容:

1000_de.vtt,1000_en.vtt,1000_es.vtt,1000_fr.vtt,1000_PT.VTT,1016_DE.VTT,1016_en.en.en.vtt,1016_ES.ES.VTT,1016_ES.VTT,10116__116__116__1116_FR。 vtt,1016_pt.vtt

我知道我可以使用诸如重命名的实用程序按术语手动执行术语: 重命名's/foo/1000/g' *.vtt

我可以将其链接到awk命令中,以便它可以通过filenames.txt运行? 还是只是尴尬地做的更简单的方法?我知道我可以重命名尴尬,例如:

find . -type f | awk -v mvCmd='mv "%s" "%s"\n' \
    '{ old=$0;
       gsub(/foo/,"1000");
       printf mvCmd,old,$0;
     }' | sh

如何使尴尬处理fileNames.txt并一口气做所有这些? A>是相似的,但使用了SED。我觉得被分开的标签应该很容易尴尬吗?

有史以来第一个帖子,请保持温柔!

解决方案

感谢您的所有帮助。最终,我能够通过调整您的答案来解决以下内容:

while read new old; do
  rename "s/$old/$new/g" *.vtt;
done < filenames.txt

I have a very large selection of files eg.

foo_de.vtt, foo_en.vtt, foo_es.vtt, foo_fr.vtt, foo_pt.vtt, baa_de.vtt, baa_en.vtt, baa_es.vtt, baa_fr.vtt, baa_pt.vtt... etc.

I have created a tab separated file, filenames.txt containing the current string and replacement string eg.

foo    1000
baa    1016
...etc

I want to rename all of the files to get the following:

1000_de.vtt, 1000_en.vtt, 1000_es.vtt, 1000_fr.vtt, 1000_pt.vtt, 1016_de.vtt, 1016_en.vtt, 1016_es.vtt, 1016_fr.vtt, 1016_pt.vtt

I know I can use a utility like rename to do it manually term by term eg:
rename 's/foo/1000/g' *.vtt

could i chain this into an awk command so that it could run through the filenames.txt?
or is there an easier way to do it just in awk? I know I can rename with awk such as:

find . -type f | awk -v mvCmd='mv "%s" "%s"\n' \
    '{ old=$0;
       gsub(/foo/,"1000");
       printf mvCmd,old,$0;
     }' | sh

How can I get awk to process filenames.txt and do all of this in one go?
This question is similar but uses sed. I feel that being tab separated this should be quite easy in awk?

First ever post so please be gentle!

Solution

Thanks for all your help. Ultimately I was able to solve by adapting your answers to the following:

while read new old; do
  rename "s/$old/$new/g" *.vtt;
done < filenames.txt

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

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

发布评论

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

评论(2

醉南桥 2025-02-05 02:27:30

我假设TSV文件中的字符串是文字(不是Regexes或Globs),并且要替换的零件可以位于文件名中的任何位置。

话虽如此,您可以将MV与Shell Globs和BASH参数扩展:

#!/bin/bash

while IFS=

或使用GNU Rename (更多性能):

while IFS=
\t' read -r old new
do
    for f in *"$old"*.vtt
    do
        mv "$f" "${f/"$old"/$new}"
    done
done < file.tsv

或使用GNU Rename (更多性能):


\t' read -r old new
do
    rename "$old" "$new" *"$old"*.vtt
done < file.tsv
\t' read -r old new do for f in *"$old"*.vtt do mv "$f" "${f/"$old"/$new}" done done < file.tsv

或使用GNU Rename(更多性能):

I'm assuming that the strings in the TSV file are literals (not regexes nor globs) and that the part to be replaced can be located anywhere in the filenames.

With that said, you can use mv with shell globs and bash parameter expansion:

#!/bin/bash

while IFS=

Or with GNU rename (more performant):

while IFS=
\t' read -r old new
do
    for f in *"$old"*.vtt
    do
        mv "$f" "${f/"$old"/$new}"
    done
done < file.tsv

Or with GNU rename (more performant):


\t' read -r old new
do
    rename "$old" "$new" *"$old"*.vtt
done < file.tsv
\t' read -r old new do for f in *"$old"*.vtt do mv "$f" "${f/"$old"/$new}" done done < file.tsv

Or with GNU rename (more performant):

装纯掩盖桑 2025-02-05 02:27:30

这可能对您有用(gnu sed and Rename):

sed -E 's#(.*)\t(.*)#rename -n '\''s/\1/\2/'\'' \1*#e' ../file

这构建了一个脚本,该脚本使用file在当前目录中重命名文件以匹配和替换文件名的部分。

一旦您对结果感到满意,请删除-n,并将颁布重命名。

This might work for you (GNU sed and rename):

sed -E 's#(.*)\t(.*)#rename -n '\''s/\1/\2/'\'' \1*#e' ../file

This builds a script which renames the files in the current directory using file to match and replace parts of the filenames.

Once you are happy with the results, remove the -n and the renaming will be enacted.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文