Rename file using sed command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Rename file using sed command
# 8  
Old 04-28-2010
Code:
#!/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


Last edited by pravin27; 04-28-2010 at 07:21 AM..
# 9  
Old 04-28-2010
Quote:
Originally Posted by Franklin52
Hmm, works fine for me, try this, but try first without the coloured part:
Code:
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 Franklin,

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

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

Quote:
Originally Posted by pravin27
Code:
#!/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 Pravin,

It is shown "renamef2.sh: syntax error at line 12: `(' unexpected"
Im not sure why your code doesnt work in my unix box... Smilie
# 10  
Old 04-28-2010
Try this .I have create another variable for the number.
Hope this will work on your unix box

Code:
#!/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

# 11  
Old 04-28-2010
MySQL

Code:
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

Code:
# ls -1tr | grep -v script*
DOCabcdef24387987ab90dasa.xml
DOCabcdef24387987ab90dasaasa.xml
DOCabcdef24387987ab90dasaasaasa.xml

Code:
[root@sistem1lnx o]# ./script1
 
Files
--------------
DOCabcdef24387987ab90dasaasaasa.xml
DOCabcdef24387987ab90dasaasa.xml
DOCabcdef24387987ab90dasa.xml
 
New Files
--------------
DOC2010392813394501.xml
DOC2010392813394502.xml
DOC2010392813394503.xml

# 12  
Old 04-28-2010
Quote:
Originally Posted by fanny_tirtasari
Hi Franklin,

No luck, still get the same error for withouth colour part and the new one that you suggest
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:

Code:
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

# 13  
Old 04-28-2010
Quote:
Originally Posted by pravin27
Try this .I have create another variable for the number.
Hope this will work on your unix box

Code:
#!/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

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 Smilie

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

Quote:
Originally Posted by Franklin52
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:

Code:
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 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

Smilie

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

Quote:
Originally Posted by ygemici
Code:
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

Code:
# ls -1tr | grep -v script*
DOCabcdef24387987ab90dasa.xml
DOCabcdef24387987ab90dasaasa.xml
DOCabcdef24387987ab90dasaasaasa.xml

Code:
[root@sistem1lnx o]# ./script1
 
Files
--------------
DOCabcdef24387987ab90dasaasaasa.xml
DOCabcdef24387987ab90dasaasa.xml
DOCabcdef24387987ab90dasa.xml
 
New Files
--------------
DOC2010392813394501.xml
DOC2010392813394502.xml
DOC2010392813394503.xml

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
--------------
# 14  
Old 04-28-2010
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?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

sed replace to rename each line a file

Have a file in this format This is line one ; line_one This is line two ; line_two This is line three ; line_three This is line four ; line four. I'm trying to make each line a new file called line_one line_two line_three line_four. Tried using split -1 but then I'm back needing to rename... (3 Replies)
Discussion started by: jimmyf
3 Replies

2. Shell Programming and Scripting

Command / script to partially rename file

Hi I have numerous files names product_host_result_B1000842.txt product_host_result_B1000847.txt product_host_result_C1000842.txt product_host_result_C1000848.txt etc. I need them renamed so that the 'product_host_result' becomes 'output_product_host' but the rest of the filename is... (6 Replies)
Discussion started by: Grueben
6 Replies

3. UNIX for Dummies Questions & Answers

Rename scripts using xargs/sed

Morning all I've got loads of scripts but the names are too long! I've stuck the list in a flat file (names) and I'm trying to read that in line by line and create the new names (in to directory new) from the list. It looks like this: xargs -n1 -I{} <names cat {} | sed... (1 Reply)
Discussion started by: Grueben
1 Replies

4. Shell Programming and Scripting

sed file rename

Ubuntu -very new to shell scripts/Linux I have many pictures with "FAMILY", "family" mixed in the file name and not all in the same directory; I want to remove "family" case insensitive from the filenames; find /media/Rock/pics/pics_bak/ -type f "*family*" | sed 's#family##gI' # works for... (2 Replies)
Discussion started by: jennyjones
2 Replies

5. Shell Programming and Scripting

Rename file using sed or awk

I have a filename like 1_DATE_3_4.5_888 and I want to modify the date field (ie the last 4 digits ) alone and remove the last field. Old filename:1_DATE_3_4.5_888 Given date (for eg):120606259532 modified date:120606259899 new filename:1_<modified date>_3.4.5 (14 Replies)
Discussion started by: sandy88
14 Replies

6. Shell Programming and Scripting

try to batch rename using sed (if this is best)

hi gooday I need some help with a rename I am attempting. I'd like to rename a bunch of files in a folder example list.dat.old to list_N.dat query.dat.old to query_N.dat note the two periods in (.dat.old) to become _N.dat I tried using sed like this ls *.dat.old | sed... (3 Replies)
Discussion started by: johnstrong
3 Replies

7. Shell Programming and Scripting

Multiple file rename (change in filename in unix with single command

Dear All, Please help ! i ham having 300 file with E.G. PMC1_4567.arc in seq. like PMC1_4568.arc,PMC1_4569.arc ...n and so on.. i want all those file to be rename like PMC_4567.arc ,PMC_4568.arc .. mean i want to remove 1 from first file name .. pls help.. (6 Replies)
Discussion started by: moon_22
6 Replies

8. UNIX for Advanced & Expert Users

Unix Command to rename a file in a zipped folder

Hi, I am into automation work. Is there any UNIX command to rename a file in a zipped folder, without unzipping it..??? Thanks in advance.. (1 Reply)
Discussion started by: Victoria.Sam
1 Replies

9. Shell Programming and Scripting

mv command to rename multiple files that retain some portion of the original file nam

Well the title is not too good, so I will explain. I need to move (rename) files using a simple AIX script. ???file1.txt ???file2.txt ???file1a.txt ???file2a.txt to be: ???renamedfile1'date'.txt ???renamedfile2'date'.txt ???renamedfile1a'date'.txt ???renamedfile2a'date'.txt ... (4 Replies)
Discussion started by: grimace15
4 Replies

10. Shell Programming and Scripting

awk/sed Command : Parse parameter file / send the lines to the ksh export command

Sorry for the duplicate thread this one is similar to the one in https://www.unix.com/shell-programming-scripting/88132-awk-sed-script-read-values-parameter-files.html#post302255121 Since there were no responses on the parent thread since it got resolved partially i thought to open the new... (4 Replies)
Discussion started by: rajan_san
4 Replies
Login or Register to Ask a Question