The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Script to rename files cpreovol Shell Programming and Scripting 3 04-01-2008 08:45 AM
rename a lot of files again ajp7701 Shell Programming and Scripting 11 03-31-2008 04:36 PM
script to rename files with current date and copy it. logic0 UNIX for Dummies Questions & Answers 6 05-01-2007 02:13 AM
Rename Files with a script ? yoveln OS X (Apple) 1 02-08-2004 09:45 AM
How to rename files? CompuTelSystem UNIX for Dummies Questions & Answers 9 05-14-2002 12:28 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 01-21-2008
Registered User
 

Join Date: Jan 2008
Posts: 2
Script to rename files

Have files of the sort 3p1522015.dgn and need to have them renamed to 152201.dgn. Essentially dropping the 1st 2 characters and the last. I'm relatively new to UNIX and uncertain of where to start. I hope this provides enough detail.

Thanks
Reply With Quote
Forum Sponsor
  #2  
Old 01-21-2008
Moderator
 

Join Date: Dec 2003
Location: /dev/fl
Posts: 1,046
Easy to do using bash shell

Code:
bash-3.2$ filename="3p1522015.dgn"
bash-3.2$ echo $newfilename
3p1522015.dgn
bash-3.2$ newfilename=${filename:2:6}.dgn
bash-3.2$ echo $newfilename
152201.dgn
Reply With Quote
  #3  
Old 01-21-2008
Registered User
 

Join Date: Jan 2008
Location: London e Milano
Posts: 71
newfilename=${filename:2:6}.dgn

don't understand this line ? is this built into bash as i don't see a command...? even thought that was regular expression!

thanx in advance!
Reply With Quote
  #4  
Old 01-21-2008
Registered User
 

Join Date: Jan 2008
Posts: 12
try this ..

if your filenames are of fixed length and you just need to remove the first two chars and last char and rename it , then this might help,


team$ filename="3p1522015.dgn"
team$ basename $filename
3p1522015.dgn
team$ basename $filename | cut -c 3-8
152201
team$ newfilename=`basename $filename | cut -c 3-8`.dgn
team$ echo $newfilename
152201.dgn
team$ unset filename newfilename
team$
Reply With Quote
  #5  
Old 01-22-2008
Moderator
 

Join Date: Dec 2003
Location: /dev/fl
Posts: 1,046
Quote:
Originally Posted by stevie_velvet View Post
newfilename=${filename:2:6}.dgn

don't understand this line ? is this built into bash as i don't see a command...? even thought that was regular expression!
Yes, this is bash syntax. It takes characters 2 through 6 of filename, appends ".dgn" and assigns it to newfilename. Google for bash regular expressions and you wish find a number of tutorials on the subject.
Reply With Quote
  #6  
Old 01-22-2008
Registered User
 

Join Date: Dec 2005
Posts: 74
it is worthy to learn such features of bash, at some situation bash can perform some operation very easily and efficiently compare to sed and awk.specially when you write a bash script you would prefer to use a bash inbuilt command than invoking some other tool or command


Rakesh UV
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
regex, regular expressions

Thread Tools
Display Modes




All times are GMT -7. The time now is 04:42 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0