Rename file using sed command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Rename file using sed command
# 1  
Old 04-28-2010
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!
# 2  
Old 04-28-2010
Hi, try this code without sed:

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

# 3  
Old 04-28-2010
Hi,

try this ...

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


Last edited by pravin27; 04-28-2010 at 06:08 AM..
# 4  
Old 04-28-2010
And another one, try it first without the coloured part:
Code:
ls DOC*.xml' |
awk -v dat=$(date +"DOC%Y%m%d%H%M%S") '
{print "mv " $0 " " dat sprintf("%02d",++c) ".xml"}
' | sh

# 5  
Old 04-28-2010
Quote:
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 ----------

Quote:
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
I tried to use this, but got error " syntax error at line 6: `i=$' unexpected" in my unix box.

Any advise?
# 6  
Old 04-28-2010
Quote:
Originally Posted by fanny_tirtasari
Hi Franklin, i copied paste your code but end up with "ksh: syntax error: `(' unexpected
".

Any advise?
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

# 7  
Old 04-28-2010
Quote:
#!/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

I tried this one but got below error :
file*: No such file or directory
renamef2.sh: syntax error at line 13: `(' unexpected
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