Changing file names with AWK

Dear All,

I have some thousands of files in a folder and i need to change those file names without opening the file (no need to change anything in the file content, need to change the file name only). The filenames are as follows:

Myfile_name.1_parameter
Myfile_name.2_parameter
Myfile_name.3_parameter
.
.
.
.
Myfile_name.100_parameter
.
.
.
.
.
.
Myfile_name.1000_parameter
.
.
.
.
..
Myfile_name.2000_parameter...and so on.

For other calculation, i need to take these files one by one and do the calculations. But these files are arranged in the following manner (OS-Linux):
Myfile_name.10_parameter
Myfile_name.11_parameter
Myfile_name.12_parameter
.
.
.
Myfile_name.100_parameter
Myfile_name.101_parameter
Myfile_name.102_parameter
.
.
.
Myfile_name.1000_parameter
Myfile_name.1001_parameter
Myfile_name.1002_parameter
.
.
.
.
.
Myfile_name.1_parameter
Myfile_name.20_parameter
Myfile_name.21_parameter ...so on and so forth...

I need to change these file names as follows:

Myfile_name.0001_parameter
Myfile_name.0002_parameter
Myfile_name.0003_parameter
...
...
...
Myfile_name.0100_parameter
...
...
Myfile_name.0200_parameter
..
..
..
Myfile_name.1000_parameter
...
...
...
...
Myfile_name.2000_parameter...an so on.

Simply i need to covert the filename with four digit numbers in it instead of one or two or three digit number in the filename.

Can any one help me to do the same using awk?

Expecting your reply and thanks in advance.

Warm regards
Fredrick.

Please post your code you have tried first.

Thanks.

the code i have tried is here

for i in *_parameter
do
  j=$(printf "%03d.%s" ${i%.*} ${i#*.})
  [[ $i != $j ]] && mv $i $j
done

Thanks
Fredrick.

Try this

for FILE in * # or whatever you want to select the files
do
    EXT=${FILE##*.}
    N=${EXT%_*}
    END=${EXT#$N}
    NEWNAME="${FILE%$EXT}$(printf '%04d\n' $N)$END"
    echo "FILE=$FILE  - NEWNAME=$NEWNAME"
#    mv "$FILE" "$NEWNAME" # Uncomment this line to really rename files
done
for FILE in *_parameter
do
  NEW=`echo $FILE |awk -F[._] '{printf "%s_%s.%04d_%s",$1,$2,$3,$4}' `
#    mv "$FILE" "$NEW"  # Uncomment this line to really rename files
done

Thank you very much for your reply. Its giving me syntax error - unterminated regexp. here is the code

#!/usr/bin/awk

for FILE in *_CA_torsion
do
  NEW=`echo $FILE |awk -F[._] '{printf "%s_%s.%04d_%s",$1,$2,$3,$4}'`;
  mv "$FILE" "$NEW"
done

Expecting your reply and thanks in advance.

Warm Regards
Fredrick

---------- Post updated at 02:14 PM ---------- Previous update was at 02:11 PM ----------

Sorry semicolon ";" in the above code is typo error.

Thanks & Regards
Fredrick.

Try this:

ls -1 Myfile_name*parameter | awk -F"[._]" '{ printf "mv Myfile_name.%d_parameter Myfile_name.%04d_parameter\n" ,$3,$3 }' | sh

If there are many files and wild char cannot able to expand , use find

ie, instead of ls -1 Myfile_nameparameter , use find . -name "Myfile_nameparameter"

If you are concerned about performance, try using direct syscall to execute a temp script contain the commands

ls -1 Myfile_name*parameter | awk -F"[._]" '{ printf "mv Myfile_name.%d_parameter Myfile_name.%04d_parameter\n" ,$3,$3 >>"file_out"} END { system("sh file_out") } '