The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 11:45 AM
rename a lot of files again ajp7701 Shell Programming and Scripting 11 03-31-2008 07:36 PM
script to rename files with current date and copy it. logic0 UNIX for Dummies Questions & Answers 6 05-01-2007 05:13 AM
Rename Files with a script ? yoveln OS X (Apple) 1 02-08-2004 12:45 PM
How to rename files? CompuTelSystem UNIX for Dummies Questions & Answers 9 05-14-2002 03:28 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-21-2008
Dinkster Dinkster is offline
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
  #2 (permalink)  
Old 01-21-2008
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,913
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
  #3 (permalink)  
Old 01-21-2008
stevie_velvet stevie_velvet is offline
Registered User
  
 

Join Date: Jan 2008
Location: London e Milano
Posts: 85
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!
  #4 (permalink)  
Old 01-22-2008
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,913
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.
  #5 (permalink)  
Old 01-22-2008
uvrakesh uvrakesh is offline
Registered User
  
 

Join Date: Dec 2005
Posts: 75
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
  #6 (permalink)  
Old 01-21-2008
Rajesh Kumar Rajesh Kumar is offline
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$
Closed Thread

Bookmarks

Tags
regex, regular expressions

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:27 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0