如何使用 bash/sed 脚本删除 mp3 文件的第一条和第二条下划线?

How can I remove the first and second underline of a mp3 file using bash/sed script?

我有类似的 mp3 This_is_my_Mp3.mp3 我想要这个 这是我的 Mp3.mp3

感谢帮助

简单明了:

% sed -e 's/_/ /g' -e 's/ / - /2' <<<'This_is_my_Mp3.mp3'
This is - my Mp3.mp3

第二个表达式上的 /2 标志,它只会替换第二次出现的模式。

当您在 'fields' 上工作时(这意味着您在水平而不是垂直上处理文本)awk 通常是更好的选择

如果您想批量重命名它们,请执行以下操作: 这个版本将只编辑流,所以你可以看到结果是什么(没有重命名文件!):

for file in *.mp3; do
sed -e 's/_/ /g' -e 's/ / - /2' <<<"$file"
done

使用mv,这个版本实际上会重命名当前目录下的文件(谨慎使用!):

for file in *.mp3; do 
mv "$file" "$(sed -e 's/_/ /g' -e 's/ / - /2' <<<$file)"
done