Challenge to change file names

Hi,

How can I change following file name in a bash script?

From file names:

myfile-module-1.0-3.0.el6.x86_64.package

To file names:

myfile-module1_0-1.0-3.0.el6.x86_64.package
                            ^ ^ ^ ^ ^ ^ ^ ^

Basically, the digit 1.0 is a version number, the digit 3.0 is a release number, I need to add the version number to module name and change the version number "." to "_". So the module name "module" becomes "module1_0", the rest are identical.

Thank you.

Kind regards.

Is gawk available?

echo 'myfile-module-1.0-3.0.el6.x86_64.package' | awk -F- '{$2=$2 gensub(/\./,"_","",$3)}1' OFS="-"
myfile-module1_0-1.0-3.0.el6.x86_64.package

Bash pattern replacement:

filename="myfile-module-1.0-3.0.el6.x86_64.package"
echo ${filename/./_}

Oh I misread the requirement. Please disregard this post. Thanks mirni for pointing that out :b:

but that is not what he/she wanted. The version number is to be kept intact, but module name is to be changed.

Brilliant, that works well. The only thing I forgot to mention was that the version number could be 2 digits 1.0, or 3 digits 1.1.1, or 4 digits 1.2.1.1. Can it be adapted to different digits of version numbers?

Thank you so much, very appreciated.

Cheers.

Easy. Just add "g" for "global" substitution:

awk -F- '{$2=$2 gensub(/\./,"_","g",$3)}1' OFS="-"

Ahh, that's the tricky. I played with awk, but could not figure it out. Lots to learn :-). Thank you so much.

Cheers.

I believe bipinijith was on the right track, but didn't quite follow through with what was requested. This uses a shell expansion that is not defined by the standards, but is available in recent versions of both ksh and bash:

for i in *-module-*
do      echo mv $i ${i/-module-/-module1_0-}
done

Remove the echo after verifying that the mv commands it will print do what you want to have done.

But you hardcoded version 1.0 in there. What if it's 2.3.4? Although the parameter expansion supports nested vars, like ${i/foo/${bar}} , you have to grab the version somehow.

Hi mirni,
Quoting from the original request: "So the module name "module" becomes "module1_0", the rest are identical." I just did what was requested.

sed 's/-\([0-9][0-9]*\).\([0-9][0-9]*\)-/\1_\2-\1.\2-/'

Try this if you want to use sed.

A little easier to read, assuming your sed supports -r option.
If your sed does not support -r option, you should switch to GNU sed.

sed -r 's/-([0-9]+).([0-9]+)-/\1_\2-\1.\2-/'