Rename file using sed command

Greetings,

I am very new to the UNIX shell scripting and would like to learn. However, I am currently stuck on how to process the below sample :

Filename : DOCabcdef24387987ab90d.xml
Pattern "DOC"+any character using [a-z] and [0-9]+".xml"

And i want to change the second part of that file (any character using [a-z] and [0-9)) become 2010042216000001 (indicating current date and time with pattern YYYYMMDDHHmmss and two digit sequence number).

So the expected result :
File1 : DOC2010042216000001.xml

And in my folder i have more than 100 files that has name like above.
And I want to rename it using script.

I tried to figure out above case using sed command but still dont understand how to use it.

If someone can kindly advise me on how to approach this issue using sed or any other command, that would be much appreciated!

Hi, try this code without sed:

i=1
for f in DOC*.xml; do
  mv "$f" "DOC$(date +%Y%m%d%H%M%S)$(printf "%02d" $i).xml"
  i=$((i+1))
done

Hi,

try this ...

#!/bin/sh

ls -ltr file* | awk '{print $9}' > temp
i=0
while read filename
do
i=`expr $i + 1`
if [ $i == 100 ]
then
i=0
fi
curr_date=`date '+%Y%m%d%H%M%S'`
echo "old filename = $filename newfilename = DOC"$curr_date$(printf "%02d" $i)".xml"
sleep 1
done < temp

And another one, try it first without the coloured part:

ls DOC*.xml' |
awk -v dat=$(date +"DOC%Y%m%d%H%M%S") '
{print "mv " $0 " " dat sprintf("%02d",++c) ".xml"}
' | sh

Hi Franklin, i copied paste your code but end up with "ksh: syntax error: `(' unexpected
".

Any advise?

---------- Post updated at 06:10 PM ---------- Previous update was at 06:01 PM ----------

I tried to use this, but got error " syntax error at line 6: `i=$' unexpected" in my unix box.

Any advise?

Hmm, works fine for me, try this, but try first without the coloured part:

ls DOC*.xml' |
awk -v dat=$(date +"DOC%Y%m%d%H%M%S") ' {
of=sprintf("%02d.xml",++c)
print "mv " $0 " " dat of
}' | sh

I tried this one but got below error :
file*: No such file or directory
renamef2.sh: syntax error at line 13: `(' unexpected

#!/bin/sh

ls -ltr DOC*.xml | awk '{print $9}' > temp
i=0
while read filename
do
i=`expr $i + 1`
if [ $i == 100 ]
then
i=0
fi
curr_date=`date '+%Y%m%d%H%M%S'`
newfile="DOC"$curr_date$(printf "%02d" $i)".xml"
mv $filename $newfile
sleep 1
done < temp

rm temp

Hi Franklin,

No luck, still get the same error for withouth colour part and the new one that you suggest. :frowning:

---------- Post updated at 06:22 PM ---------- Previous update was at 06:18 PM ----------

Hi Pravin,

It is shown "renamef2.sh: syntax error at line 12: `(' unexpected"
Im not sure why your code doesnt work in my unix box... :frowning:

Try this .I have create another variable for the number.
Hope this will work on your unix box

#!/bin/sh

ls -ltr DOC*.xml | awk '{print $9}' > temp
i=0
while read filename
do
i=`expr $i + 1`
if [ $i == 100 ]
then
i=0
fi
curr_date=`date '+%Y%m%d%H%M%S'`
curr_number=`printf "%02d" $i`
newfile="DOC"$curr_date$curr_number".xml"
mv $filename $newfile
sleep 1
done < temp

rm temp
cat script1
 
#!/bin/bash
mydir="/root/o"
cd $mydir
ls -1 | grep "DOC*.*" | grep -v filestmp > filestmp
echo ""
echo "Files"
echo "--------------"
cat filestmp
sleep 2
seqnumber=1
while read file
      do
        newfilename=$(echo $file | sed "s/$file/DOC$(date '+%Y%M%d%H%M%S')0$seqnumber.xml/")
        mv -f $file $newfilename
        ((++seqnumber))
    done < filestmp
echo ""
echo "New Files"
echo "--------------"
ls -1 | grep "DOC*.*" | grep -v filestmp
rm -f filestmp
# ls -1tr | grep -v script*
DOCabcdef24387987ab90dasa.xml
DOCabcdef24387987ab90dasaasa.xml
DOCabcdef24387987ab90dasaasaasa.xml
[root@sistem1lnx o]# ./script1
 
Files
--------------
DOCabcdef24387987ab90dasaasaasa.xml
DOCabcdef24387987ab90dasaasa.xml
DOCabcdef24387987ab90dasa.xml
 
New Files
--------------
DOC2010392813394501.xml
DOC2010392813394502.xml
DOC2010392813394503.xml

Try to quote the date command like:

dat="$(date +"DOC%Y%m%d%H%M%S")"

...and remove the quote (typo) after ls DOC*.xml in my previous posts:

ls DOC*.xml |
awk -v dat="$(date +"DOC%Y%m%d%H%M%S")" ' {
of=sprintf("%02d.xml",++c)
print "mv " $0 " " dat of
}' | sh

Hi Pravin,

finally works fine.
But i need to change this :
if [ $i == 100 ] become if [ $i = 100 ]

thanks very much for your help and kindness :smiley:

---------- Post updated at 07:04 PM ---------- Previous update was at 06:58 PM ----------

Hi Franklin,

Not sure what is wrong with my unix box, but the result for the latest code come up with below error :

awk: syntax error near line 1
awk: bailing out near line 1

:frowning:

---------- Post updated at 07:12 PM ---------- Previous update was at 07:04 PM ----------

Hi ygemici,

i got below error if i run your code in my unix box :

renamef4.sh: filestmp: Permission denied

Files
--------------
cat: cannot open filestmp
renamef4.sh: filestmp: No such file or directory

New Files
--------------

You may be using a shell the other posters are not expecting. Be sure there are no spaces between = and $( as in dat=$(

Do you know if you have bash, bourne, ksh, zsh....? which one?

Use nawk or /usr/xpg4/bin/awk on Solaris.

Hi Franklin,

Still no luck :(.

I used /usr/xpg4/bin/awk and the result is like following :
/usr/xpg4/bin/awk: syntax error Context is:
>>> 20100428193054 {of=sprintf("%02d.xml",++c) print <<<

And when using nawk the result is like following :
nawk: syntax error at source line 1
context is
{of=sprintf("%02d.xml",++c) >>> print <<< "mv " $0 " " dat of}
nawk: illegal statement at source line 1

---------- Post updated at 07:35 PM ---------- Previous update was at 07:33 PM ----------

Hi Jim,

There are no spaces between = and 4( in dat=$(.
Im sure of that.

but not sure for bahs, bourne or zsh.
But the other program in my unix box are having ext ksh or sh or prog.

what about Scrutinizer's solution?
Did you try that?

Hi Anchal and scrutinizer,

i did try the solution but come up with below error :
" syntax error at line 6: `i=$' unexpected" in my unix box

Is there write permission yourself in dir?
please the output in your dir that script works

ls -ld

Are you still facing issue with the solution which I have provided ?