使用bash脚本更改文件名:通过下划线替换逗号和空格

发布于 2025-02-13 01:41:19 字数 964 浏览 1 评论 0原文

我的文件是格式,

Country, City S1.txt

例如

USA, Los Angeles S1.txt
USA, San Francisco S3.txt
UK, Glouchester S4.txt
Argentina, Buenos Aires S7.txt

我希望将它们更改为

Country_City_S1.txt

例如

USA_Los_Angeles_S1.txt
USA_San_Franciso_S3.txt
UK_Glouchester_S4.txt
Argentina_Buenos_Aires_S7.txt

要删除逗号,我使用以下sed命令:

sed -i 's/,//g'

用下划线替换空格,我使用以下sed命令:

sed -i 's/ /_/g'

问题:有没有办法将上述两个命令组合到一个命令中?还是可以完成以上操作的整洁方法?

注意:

  1. 我更改了原始帖子的标题,以更好地反映我需要做的事情。对此我的混乱感到抱歉。

  2. 我现在了解我需要的东西,这是一个bash脚本,而不是更改文件名的 sed

  3. 我感谢所有已提出建议的 sed 命令的人。

  4. 我更喜欢在bash脚本中使用 mv 命令 *对于 .txt中的f;做MV .......;完成

My files are of the format

Country, City S1.txt

e.g.

USA, Los Angeles S1.txt
USA, San Francisco S3.txt
UK, Glouchester S4.txt
Argentina, Buenos Aires S7.txt

I wish to change them to

Country_City_S1.txt

e.g.

USA_Los_Angeles_S1.txt
USA_San_Franciso_S3.txt
UK_Glouchester_S4.txt
Argentina_Buenos_Aires_S7.txt

To remove the comma I use the following sed command:

sed -i 's/,//g'

To replace the whitespaces with underscore, I use the following sed command:

sed -i 's/ /_/g'

Question: Is there a way to combine the above two commands into one? Or is there a neater way to accomplish the above?

Notes:

  1. I have changed the title of my original post to better reflect what I need to be done. I am sorry for any confusion caused as a result of the change.

  2. I understand now what I need, which is a bash script and not sed to change filenames.

  3. I thank all those who have replied with their suggested sed commands.

  4. I prefer to use the mv command in a bash script as in for example
    *for f in .txt; do mv .......; done

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

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

发布评论

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

评论(4

花心好男孩 2025-02-20 01:41:19

用单个下划线替换空间和逗号的任何组合:

sed -E 's/[, ]+/_/g'

请参见实时演示


上使用此正则命名的文件重命名文件,任何 *nix OS(MacOS不使用重命名):

for f in *.txt; do mv $f $(echo "$f" | sed -E 's/[, ]+/_/g'); done

如果您的 *nix具有Rename命令(brew install inname如果您是macos):

rename 's/[, ]+/_/g' *.txt

Replace any combination of spaces and commas with a single underscore:

sed -E 's/[, ]+/_/g'

See live demo.


To rename files using this regex on any *nix OS (macos doesn't ship with rename):

for f in *.txt; do mv $f $(echo "$f" | sed -E 's/[, ]+/_/g'); done

If your *nix has the rename command (brew install rename if you're macos):

rename 's/[, ]+/_/g' *.txt
薄荷梦 2025-02-20 01:41:19

合并您的两个sed表达式:

sed 's/,//g; s/ /_/g'

Merge your two sed expressions:

sed 's/,//g; s/ /_/g'
独孤求败 2025-02-20 01:41:19

这可能对您有用(GNU SED):

sed 's/,//g;y/ /_/' file

删除逗号并将空间转换为下划线。

也许是:

sed -E s/,? +/_/g' file

重命名文件名:也许:

sed -E 'h;s/,? +//g;H;g;s/(.*)\n(.*)/mv "\1" "\2"/e' <<<"$filename"

This might work for you (GNU sed):

sed 's/,//g;y/ /_/' file

Remove commas and translate spaces to underscores.

Or perhaps:

sed -E s/,? +/_/g' file

To rename file names, perhaps:

sed -E 'h;s/,? +//g;H;g;s/(.*)\n(.*)/mv "\1" "\2"/e' <<<"$filename"
命比纸薄 2025-02-20 01:41:19

这里的一行解决方案:

find *.txt | sed -E 'p;s/[, ]+/_/g' | tr '\n' '\0' | xargs -0 -n2 mv

这是一个非常有用的命令模板,用于重命名文件:

find ... | sed 'p;s/...' | tr '\n' '\0' | xargs -0 -n2 mv

One line solution here:

find *.txt | sed -E 'p;s/[, ]+/_/g' | tr '\n' '\0' | xargs -0 -n2 mv

This is a very useful command template for batch renaming files:

find ... | sed 'p;s/...' | tr '\n' '\0' | xargs -0 -n2 mv
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文