Computing.Net > Forums > Unix > script to change case of extensions

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

script to change case of extensions

Reply to Message Icon

Name: Peter
Date: May 13, 2003 at 12:40:31 Pacific
OS: unix
CPU/Ram: 1.7
Comment:

I need a script to change the case of file extensions from upper to lower case. I don't think I can do it just in the ordinary bash shell, and I know nothing about awk



Sponsored Link
Ads by Google

Response Number 1
Name: Rao
Date: May 13, 2003 at 14:01:36 Pacific
Reply:

#!/bin/ksh
if [[ $# != 1 ]]
then
echo "Usage: $0 filename"
exit
fi

echo $1 | cut -d. -f1 | read PREFIX
echo $1 | cut -d. -f2 | read SUFFIX
echo $SUFFIX | tr "[:upper:]" "[:lower:]" | read suffix
echo $PREFIX.$suffix


0

Response Number 2
Name: James Boothe
Date: May 14, 2003 at 09:10:20 Pacific
Reply:

The solution above would fail on a filename like: my.test.TXT

korn shell has built-in functions that make this easy and efficient, and avoids all the piped processes.

typeset -l will force to lowercase. After testing, remove the echo to enable the mv command.

#!/bin/ksh

if [ $# -ne 1 ] ; then
   echo "Usage: $0 filename"
   exit
fi

typeset -l SUFFIX

PREFIX=${1%.*}
SUFFIX=${1#$PREFIX}

echo mv -i $1 $PREFIX$SUFFIX

exit 0


0

Response Number 3
Name: jakub
Date: May 20, 2003 at 05:39:06 Pacific
Reply:

Rao:
This cunstruction may work, but often fails:

echo $1 | cut -d. -f1 | read PREFIX

more is here:
http://www.tldp.org/LDP/abs/html/gotchas.html#BADREAD0


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: script to change case of extensions

Script to change values linebyline www.computing.net/answers/unix/script-to-change-values-linebyline/8308.html

script to change file names www.computing.net/answers/unix/script-to-change-file-names/8327.html

shell script to get value of field www.computing.net/answers/unix/shell-script-to-get-value-of-field/6716.html