replace the - with _ in a file name

Hello friends,
I have around 1000 files in a directory with different kind of file names like Championships-2009--How-to-get-in-27781, wiki1.jpg,used_shutterstock_2881985_4.jpg,stock-43047295.jpg, corporate-hospitality---19538 etc.

I need a script which can replace only one - (dash) near to digits only from those files names which are ending with only digits.
Like Championships-2009--How-to-get-in-27781 to Championships-2009--How-to-get-in_27781 and corporate-hospitality---19538 to corporate-hospitality--_19538

Please help to solve this.
Thanks in advance.

 
echo "Championships-2009--How-to-get-in-27781" | sed 's/\(.*\)-\([0-9].*\)/\1_\2/' 
 
echo "corporate-hospitality---19538" | sed 's/\(.*\)-\([0-9].*\)/\1_\2/' 

But how do you find all the files with name ending with -[0-9]digit in a directory??

---------- Post updated at 07:01 PM ---------- Previous update was at 03:58 PM ----------

any help ??

 
ls -l *_[0-9]*

That's incorrect. The * allows a string of arbitrary length and of arbitrary characters to follow a digit. It will absolutely not insure that the name matched ends with digits.

Regards,
Alister

Thanks for reply.
I think you misunderstood my question. I want to replace only one - with _ of file names, like Time-to-Panic--585 . It should change to Time-to-Panic-_5852 and there are more that 1000 files in a directory with various name patterns, like sharp3dphone--2_0.jpeg, Paying-the-piper-6091--big, Z06-Corvette--4471 etc. So i need a script which can find out only those files whose names are ending with -[0-9] pattern and replace only one - with _ it means -[0-9] to _[0-9].

Please try to solve it.

Hello Alister,

Sorry that is my ignorance.

Vikky,

Something like this:

 
for file in `ls *-[0-9]* | grep -e "-[0-9]*[0-9]$"`
do
echo $file
mv $file `echo $file | sed 's/\(.*\)-\([0-9].*\)/\1_\2/'`
done

Solved. Hey Thanks a lot panyam. Its working fine. This problem has been solved. Thanks again, you are a champ buddy :wink: