renaming file

Dear Friends,
Need your help once again.

I have this file name

e.g.1) report_12.rp_1
e.g.2) remark_mm.rmr_3

I want it to be renamed as

report_12_1.rp
remark_mm_3.rmr

If you have the file name in $filename you can do the following.

head1=${filename%_[0-9]}
tail=${filename#$head1}
head2=${head1%.*}
ext=${head1#$head2}
mv "$filename" "${head2%$ext}$tail$ext"

This assumes the final suffix is a single digit. The construct ${var#pat} means the value of $var with any match on the pattern pat removed from the beginning; ${var%pat} does the corresponding substitution on the end of the value. See the manual page for your shell for more information.

With sed:

sed 's/\(.*\)\(\..*\)\(_.*\)/\1\3\2/'

For example:

echo report_12.rp_1 | sed 's/\(.*\)\(\..*\)\(_.*\)/mv & \1\3\2/' | sh

Regards

if you have Python , you can download this script.
example usage for same file name structure:

# ls -1 re*
remark_mm.rmr_19
remark_mm.rmr_3
report_12.rp_1

# filerenamer.py -p "(.*_.*)(\..*)(_.*)$" -e "\\1\\3\\2" -l "re*_*"  #remove -l to commit
==>>>>  [ /home/remark_mm.rmr_3 ]==>[ /home/remark_mm_3.rmr ]
==>>>>  [ /home/remark_mm.rmr_19 ]==>[ /home/remark_mm_19.rmr ]
==>>>>  [ /home/report_12.rp_1 ]==>[ /home/report_12_1.rp ]

# ls -1 re*
remark_mm_19.rmr
remark_mm_3.rmr
report_12_1.rp