Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Renaming many files in a folder 2

Status
Not open for further replies.

jckrell2

Programmer
Nov 4, 2003
13
US
Hello,

I need to know how to rename a bunch of files i have.
All the files are currently in the same format and I need to do 3 things to the name. First I need to add a leading W to the name, then where the name ends with the extention .dwg i need to make this uppercase (.DWG) and the 3rd thing is I need to add a leading 0 to part of the name.
All the files are in the same format.

Here is an example of a filename i have.

21-11-000-01-103-01.dwg

All the files are similar to this and in the same format of two digits a dash, two more digits, a dash, 3 digits, a dash....etc.. just like the example above. Also they all end with .dwg.

What I need is for this filename to look like this

W21-11-00-01-103-01.DWG

So, the filename needs a leading W to go before the original filename. The 3rd set of digits (the -000) would need to become -00 (cut off the leading digit) and then the .dwg would need to become .DWG.

One more example would be

54-21-061-50-125-30.dwg

Would become:

W54-21-61-50-125-30.DWG

I would appreciate any help with this. I do not know much about shell scripting and I need to do this kind of stuff sometimes. I'm not sure if it would be better to do this in several steps with several scripts..or all together..but..I would really appreciate anybody that would know how to do this.

Thanks

Johnny
 
Here's an example, with lots of echo commands to show each step in action
Code:
#!/bin/sh

# test name
a="21-11-000-01-103-01.dwg 54-21-061-50-125-30.dwg"

# get them all
# a=`ls *.dwg`

for i in $a; do
  j=$i
  echo $j
  j=`echo $j | sed 's/-0/-/1'`
  echo $j
  j="w"$j
  echo $j
  j=`echo $j | tr '[a-z]' '[A-Z]'`
  echo $j
  echo mv $i $j
done

--
 
You can determine the new file name with one sed statement.

[tt]
a="21-11-000-01-103-01.dwg 54-21-061-50-125-30.dwg"

for i in $a; do
j=`echo $i | sed -e 's/^\([^-]*-[^-]*-\)./W\1/' -e 'y/dwh/DWH/'`
echo mv $i $j
done
[/tt]

[tt]s/^\([^-]*-[^-]*-\)./W\1/ [/tt] Prefix with W and suppress first char after second '-'
[tt]y/dwh/DWH/ [/tt] Change lowercase chars dwh to uppercase


Jean Pierre.
 
Or you can do the following:
j=`echo $i | sed -e 's/^/W/' -e 's/-[0-9]/-/2' -e 'y/dwg/DWG/'

with the following results:
mv 21-11-000-01-103-01.dwg W21-11-00-01-103-01.DWG
mv 54-21-061-50-125-30.dwg W54-21-61-50-125-30.DWG


where:
s/^/W/ inserts W at beginning
s/-[0-9]/-/2 substitutes the second occurrence of -N by -
s/dwg/DWG/ substitutes dwg by DWG
 
Thanks PC888.
I must read again man pages.

Jean Pierre.
 
Hello,

Thanks for all the great info. I did use Salem's code and it
worked perfectly. Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top