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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-28-2005
steve7 steve7 is offline
Registered User
  
 

Join Date: Jun 2005
Posts: 1
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
  #2 (permalink)  
Old 06-28-2005
rahul123_libra rahul123_libra is offline
Registered User
  
 

Join Date: May 2005
Posts: 46
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 09:37 AM..
  #3 (permalink)  
Old 06-29-2005
vibhor_agarwali vibhor_agarwali is offline
Registered User
  
 

Join Date: Jan 2005
Posts: 259
Something using sed:

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

Join Date: May 2005
Posts: 46
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 ?
  #5 (permalink)  
Old 06-29-2005
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

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

-vino
  #6 (permalink)  
Old 06-30-2005
vibhor_agarwali vibhor_agarwali is offline
Registered User
  
 

Join Date: Jan 2005
Posts: 259
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
  #7 (permalink)  
Old 06-30-2005
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,409
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
Closed Thread

Bookmarks

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 07:19 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