Replacing the part of file name?

Hi All

One of my script generate following files. These files has static TIMESTAMP 20080227.

AccAdd_20080227_1000.dat
AccBal_20080227_1000.dat
Acc_20080227_1000.dat
AccGrpMem_20080227_1000.dat
AccToCust_20080227_1000.dat

What i need to do is, once the file has been generated, it should change the timestamo (20080227) & replace it with the parameter which i am passing. for example if i pass script parameter ... 20070909... then it should be like..

AccAdd_20070909_1000.dat
AccBal_20070909_1000.dat
Acc_20070909_1000.dat
AccGrpMem_20070909_1000.dat
AccToCust_20070909_1000.dat

Can someone tell me which comand should be ussed?

Regards,

Amit

if you have Python , you can use the script here
example usage

# ls -1 Acc*dat
AccAdd_20080227_1000.dat
AccBal_20080227_1000.dat
AccToCust_20080227_1000.dat
Acc_20080227_1000.dat

# parameter="20090101"
# filerenamer.py -p "_.*_" -e _"$parameter"_ -l "Acc*.dat"
==>>>>  [ /home/AccToCust_20080227_1000.dat ]==>[ /home/AccToCust_20090101_1000.dat ]
==>>>>  [ /home/test/Acc_20080227_1000.dat ]==>[ /home/Acc_20090101_1000.dat ]
==>>>>  [ /home/AccBal_20080227_1000.dat ]==>[ /home/AccBal_20090101_1000.dat ]
==>>>>  [ /home/AccAdd_20080227_1000.dat ]==>[ /home/AccAdd_20090101_1000.dat ]

# filerenamer.py -p "_.*_" -e _"$parameter"_  "Acc*.dat"
/home/AccToCust_20080227_1000.dat  is renamed to  /home/AccToCust_20090101_1000.dat
/home/Acc_20080227_1000.dat  is renamed to  /home/Acc_20090101_1000.dat
/home/AccBal_20080227_1000.dat  is renamed to  /home/AccBal_20090101_1000.dat
/home/AccAdd_20080227_1000.dat  is renamed to  /home/AccAdd_20090101_1000.dat

# ls -1 Acc*dat
AccAdd_20090101_1000.dat
AccBal_20090101_1000.dat
AccToCust_20090101_1000.dat
Acc_20090101_1000.dat

Hi Ghost,

No i do not have python. I have tried using sed command. But it is not working.

Regards,

Amit

Amit,

Try using the below in your script

timeparam=$1
ls Acc* | while read filename
do
firstname=`echo $filename | awk -F'' '{print $1}'`
lastname=`echo $filename | awk -F'
' '{print $3}'`
newfilename=`echo $firstname``echo $timeparam``echo $lastname`
mv $filename $newfilename
done

Try this, first without the last pipe command (| sh) to be sure you get the right mv command:

ls -1 *_20080227_* |
awk -F"_" -v old="20080227" -v new="20070909" '$2==old{print "mv "$0 " "$1"_"new"_"$3}' | sh

Regards

# echo AccToCust_20090101_1000.dat |sed 's|\(.*\)_\(.*\)_\(.*\)|\1_20090201_\3|'
AccToCust_20090201_1000.dat

Thank you guys... I got expected result. Thank you veru much

oldtimestamp="20080227"
newtimestamp="20070909"
for file in `ls $log_path`
do
prefix=${file%%"$oldtimestamp"}
lastfix=${file##"$oldtimestamp"}
/usr/bin/mv "$file" "${prefix}${newtimestamp}${lastfix}" >/dev/null 2>&1
if [[ $? -ne 0 ]]; then
# add log here
fi
done