[英]Bash Rename Files Script Not Working
I have the following script and for some reason it is not working 我有以下脚本,由于某种原因,它无法正常工作
find . -name '*---*' | while read fname3
do
new_fname3=`echo $fname3 | tr "---" "-"`
if [ -e $new_fname3 ]
then
echo "File $new_fname3 already exists. Not replacing $fname3"
else
echo "Creating new file $new_fname3 to replace $fname3"
mv "$fname3" $new_fname3
fi
done
However if I use 但是,如果我使用
find . -name '*---*' | while read fname3
do
new_fname3=`echo $fname3 | tr "-" "_"`
if [ -e $new_fname3 ]
then
echo "File $new_fname3 already exists. Not replacing $fname3"
else
echo "Creating new file $new_fname3 to replace $fname3"
mv "$fname3" $new_fname3
fi
done
The script works but I end up with 3 underscores " _ " how can I replace the 3 dashes "---" with a single dash? 该脚本可以工作,但是我最终得到3个下划线“ _ ”,如何用一个破折号代替3个破折号“ ---”?
Thanks, 谢谢,
Have a look at man tr
. 看看
man tr
。 tr
will just replace single characters. tr
只会替换单个字符。
Use something like perl -wpe "s/---/-/"
instead. 使用类似
perl -wpe "s/---/-/"
。
Also have a look at man 1p rename
. 也看看
man 1p rename
。 It is doing pretty much exactly what you want: 它几乎可以完全满足您的要求:
rename 's/---/-/' *---*
I believe you need to change the tr
for a sed
substitution: 我相信您需要更改
tr
以进行sed
替代:
tr '---' '-'
should be changed to sed -e 's/---/-/g
tr '---' '-'
应该更改为sed -e 's/---/-/g
As an example of the difference: 作为区别的一个例子:
$ echo "a---b" | tr '---' '-'
tr: unrecognised option '---'
try `tr --help' for more information
$ echo "a---b" | sed -e 's/---/-/g'
a-b
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.