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
Rename a file name Aswarth Shell Programming and Scripting 0 06-04-2008 11:18 AM
rename file to file.ext.datetime tripsat Shell Programming and Scripting 2 09-20-2007 01:12 PM
Not able to rename file MANISH KU Shell Programming and Scripting 2 06-27-2007 01:07 AM
File rename redlotus72 UNIX for Dummies Questions & Answers 1 03-09-2006 07:52 AM
Rename part of multiple files sajjad02 Shell Programming and Scripting 4 02-22-2005 09:30 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-28-2005
Registered User
 

Join Date: Jun 2005
Posts: 1
Stumble this Post!
Question Help with multiple file rename - change case of part of file name

Hi there,

I hope someone can help me with this problem :

I have a directory (/var/www/file/imgprofil) which contains about 10000 JPG files. They have a naming convention thus :

prefix-date-key-suffix.jpg

they all have the prefix p-20050608-
then AAAA is a 4 letter code
the suffix is either -g / -v / -m or no suffix
the file extension is always .jpg

sample :

p-20050608-AFWC-g.jpg
p-20050608-AFWC-m.jpg
p-20050608-AFWC-v.jpg
p-20050608-AFWC.jpg
p-20050608-AFWD-g.jpg
p-20050608-AFWD-m.jpg
p-20050608-AFWD-v.jpg
p-20050608-AFWD.jpg


My problem is that some of the files have 1 or more lower case letters in the 4 letter code, so they are like this :

p-20050608-Ajyd-g.jpg
p-20050608-Ajyd-m.jpg
p-20050608-Ajyd-v.jpg
p-20050608-Ajyd.jpg
p-20050608-Ajyw-g.jpg
p-20050608-Ajyw-m.jpg
p-20050608-Ajyw-v.jpg
p-20050608-Ajyw.jpg


Now, Unix being the case sensitive angel that it is I need to transform these lowercase "keys" filenames into uppercase, so the sample would be :

p-20050608-AJYD-g.jpg
p-20050608-AJYD-m.jpg
p-20050608-AJYD-v.jpg
p-20050608-AJYD.jpg
p-20050608-AJYW-g.jpg
p-20050608-AJYW-m.jpg
p-20050608-AJYW-v.jpg
p-20050608-AJYW.jpg


While of course keeping the lowercase prefix (p-) and suffix (-g /-m / -v) and the lowercase file extension (.jpg)

I hope this is clear and that someone can help with this,

Thanks,

Steve
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 06-28-2005
Registered User
 

Join Date: May 2005
Posts: 46
Stumble this Post!
Use awk

for old_name in `ls -l path_to_dir/*jpg`
do
new_name=`awk -F"-" 'BEGIN { OFS="-"} {$3=toupper($3);print $0}' $old_name`
mv $old_name $new_name
done

Last edited by rahul123_libra; 06-28-2005 at 06:37 AM.
Reply With Quote
  #3 (permalink)  
Old 06-28-2005
Registered User
 

Join Date: Jan 2005
Posts: 212
Stumble this Post!
Something using sed:

for filename in *.jpg;
do
echo $filename | sed -e 's/Aiyd/AIYD/g'
done
Reply With Quote
  #4 (permalink)  
Old 06-29-2005
Registered User
 

Join Date: May 2005
Posts: 46
Stumble this Post!
THis is for 1 particular case

Quote:
Originally Posted by vibhor_agarwali
Something using sed:

for filename in *.jpg;
do
echo $filename | sed -e 's/Aiyd/AIYD/g'
done

This is for 1 particular case ( aiyd ) and not for all the options

Isnt it ?
Reply With Quote
  #5 (permalink)  
Old 06-29-2005
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,661
Stumble this Post!
Apart from that, how will you change the name of the file with a sed. It requires a mv very specifically.

-vino
Reply With Quote
  #6 (permalink)  
Old 06-30-2005
Registered User
 

Join Date: Jan 2005
Posts: 212
Stumble this Post!
Perfectly Agree,

But i could accomplish only that much with my knowledge of sed.
Can anyone give a general one with sed.

mv is taken for granted.

for filename in *.jpg;
do
newname=`echo $filename | sed -e 's/Aiyd/AIYD/g'`
mv $filename newname
done
Reply With Quote
  #7 (permalink)  
Old 06-30-2005
Ygor's Avatar
Moderator
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,243
Stumble this Post!
Quote:
Originally Posted by rahul123_libra
for old_name in `ls -l path_to_dir/*jpg`
do
new_name=`awk -F"-" 'BEGIN { OFS="-"} {$3=toupper($3);print $0}' $old_name`
mv $old_name $new_name
done
This has some errors in that awk line. Firstly, it's attempting to process the JPG file itself rather than the file name.
Also, using $3 sometimes matches more than the 4 letter code (where there is no suffix).

Perhaps try:
Code:
for i in *.jpg
do
  eval $(echo $i|awk '{printf "mv %s %s\n", $0,\
    substr($0,1,11) toupper(substr($0,12,4)) substr($0,16)}')
done
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 08:50 PM.


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

Content Relevant URLs by vBSEO 3.2.0