Re: Filename conversion to uppercase

From: Bill Thompson (bill.thompson@GOODYEAR.COM)
Date: Tue May 04 2004 - 07:19:06 EDT


This is an FYI for all of you shell scripters out there.

All the methods presented to convert a text string to uppercase characters work equally well however the time they take to do the
conversion varies greatly. If you're converting a handful of files you'll never see the difference. However, if you're converting a
large number of files the time difference can be astonishing.

To test this, I created five scripts that performed the following steps:

- - - - snip - - - -
#!/bin/sh
for FILE in $(ls); do
    UPPER=<conversion method>
done
- - - - snip - - - -

The five scripts contained the following conversion methods:

script 1: typeset -u UPPER
              UPPER=$FILE

script 2: UPPER=$(echo "$FILE" | tr "[:upper:]" "[:lower:]")

script 3: UPPER=$(echo "$FILE" | tr -s "a-z" "A-Z")

script 4: UPPER=$(echo "$FILE" | dd conv=lcase 2>/dev/null)

script 5: UPPER=$(echo "$FILE" | awk '{printf ("%s", tolower ($0))}')

Note that script 1 uses Korn shell internals to do the conversion while the other four methods spawn another process (call an external
program) to perform the conversions.

I ran these five scripts against a directory containing 84,600 files and recorded the execution time. They were dramatically
different:

script 1: 8.0 seconds

script 2: 2:03:14.5 = 7394.5 seconds

script 3: 1:52:44.2 = 6764.2 seconds

script 4: 1:52:41.9 = 6761.9 seconds

script 5: 2:01:38.1 = 7298.1 seconds

No, that's not a mistake. Script 1 took 8 seconds to run while the others took 2 hours. The scripts that spawned another process (or
actually spawned 84,600 processes - one for each filename) took close to 1,000 times longer to perform the same function!

So, when you're writing your scripts out there, remember - spawning another process is the most costly thing you can do on a Unix
system.

Bill Thompson
Sr UNIX Systems Administrator
The Goodyear Tire & Rubber Co.

Contains Confidential and/or Proprietary Information
May Not Be Copied or Disseminated Without Express Consent of The Goodyear Tire & Rubber Company.

AIX-L Archives: http://marc.theaimsgroup.com/?l=aix-l&r=1&w=2

----- Original Message -----
From: "Sousa, Jaci" <jaci.sousa@EDS.COM>
Newsgroups: bit.listserv.aix-l
To: <aix-l@Princeton.EDU>
Sent: Monday, May 03, 2004 7:17 AM
Subject: FW: Filename conversion to uppercase

> Here are another ways (there are many):
>
> ----------------------------------------------------------------------------
> --------------
> # Filename conversion to uppercase"
> LIXO
> #
> # echo "LIXO" | tr "[:upper:]" "[:lower:]"
> lixo
> #
> ----------------------------------------------------------------------------
> --------------
> #
> # Program name:
> # uppercase.sh
> # Program description:
> # Rename the lowercase filenames to uppercase format
> # Program history:
> # Created by Emilia Nakahata (EDS)
> # Date 25.Apr.2000
> #
> DIR=/autogiro/dealers
> LISTDIR="$DIR `find $DIR -type d`"
> for dir in $LISTDIR
> do
> file=`ls -l $dir | grep "^-" | awk '{ print $9 }'`
> N=`echo $file | wc -w`
> n=1
> if [ "$file" != "" ];then
> FILE=`echo $file | awk '{ upper=toupper($0);print upper }'`
> while [ $n -le $N ]
> do
> name="$dir/`echo $file | cut -d' ' -f$n`"
> NAME="$dir/`echo $FILE | cut -d' ' -f$n`"
> mv $name $NAME
> n=`expr $n + 1`
> done
> fi
> done
> ----------------------------------------------------------------------------
> --------------
> #cat teste.log
> lkjafgjkhefkjshflgkjsndfkgjshdfgkjnsfdhgsdlf
> af.gkjadsnfgjkhsdlkgbnszdlghsldfghlsfhglsdf
> aflgfahdgoihsdgoisudhvoiuwehgiusdhfoiguwjrg
> afogiuhafoguyhseroigjhsdfoiughseiughsdopifugh
> erlgiuahefoiughseouighsoeifughsfdiuhgsuerhtgweruhgiwsrhg
> oiruhgwurhiwurhgweurt
> werghiewrguyhewritjhhwefoyghapoiwrntpqiaerhgiujaernglksrg
> qeroiuqrhe098fghpriuthqeowthwe4r
> #
>
> #cat teste.log | tr -s "a-z" "A-Z"
> LKJAFGJKHEFKJSHFLGKJSNDFKGJSHDFGKJNSFDHGSDLF
> AF.GKJADSNFGJKHSDLKGBNSZDLGHSLDFGHLSFHGLSDF
> AFLGFAHDGOIHSDGOISUDHVOIUWEHGIUSDHFOIGUWJRG
> AFOGIUHAFOGUYHSEROIGJHSDFOIUGHSEIUGHSDOPIFUGH
> ERLGIUAHEFOIUGHSEOUIGHSOEIFUGHSFDIUHGSUERHTGWERUHGIWSRHG
> OIRUHGWURHIWURHGWEURT
> WERGHIEWRGUYHEWRITJHWEFOYGHAPOIWRNTPQIAERHGIUJAERNGLKSRG
> QEROIUQRHE098FGHPRIUTHQEOWTHWE4R
> #
> ----------------------------------------------------------------------------
> --------------
> echo A | dd conv=lcase
> or
> echo b | dd conv=ucase
> ----------------------------------------------------------------------------
> --------------
> [MAKE THE .123 FILES LOWERCASE.]
>
> for i in `ls *123`
> do
> x=`echo $i | awk '{printf ("%s", tolower ($0))}'`
> mv $i $x
> done
> ----------------------------------------------------------------------------
> --------------
>
> Regards,
>
>
> Qualquer dúvida/problema, estou a disposição.
>
> Abraços,
>
> Jaci de Sousa
> EDS Brasil
> ITO - Midrange Hosting
> Sun Certified System Administrator for Solaris 8
> Sun Certified System Administrator for Solaris 7
> Compaq Certified Tru64 Unix V5.0 System Administration, Support &
> Integration
> ( 55-11-34714631
> - Av. Goiás, 3353 - SCS
> * <mailto:Jaci.Sousa@eds.com>
>
> O homem procura milagres para obedecer. Mas Deus nos manda obedecer para
> alcançar os milagres.
>
> Para se conquistar a vitória, não bastam somente estratégia e vontade de
> vencer. Ganhará uma batalha quem tiver a maior disposição para lutar.
>
>
>
> -----Original Message-----
> From: IBM AIX Discussion List [mailto:aix-l@Princeton.EDU] On Behalf Of Bill
> Thompson
> Sent: Saturday, May 01, 2004 12:19 PM
> To: aix-l@Princeton.EDU
> Subject: Re: Filename conversion to uppercase
>
>
> Here's one way (there are many):
>
> - - - - - snip - - - - -
> #!/bin/sh
>
> typeset -u UPPER
>
> for FILE in $@; do
> UPPER=$FILE
> mv $FILE $UPPER
> done
> - - - - - snip - - - - -
>
> Of course you may want to do some checking along the way, but that's the
> basic idea.
>
> Bill Thompson
> Sr UNIX Systems Administrator
> The Goodyear Tire & Rubber Co.
>
> Contains Confidential and/or Proprietary Information
> May Not Be Copied or Disseminated Without Express Consent of The Goodyear
> Tire & Rubber Company.
>
> AIX-L Archives: http://marc.theaimsgroup.com/?l=aix-l&r=1&w=2
>
>
> ----- Original Message -----
> From: <faisalq@CYBER.NET.PK>
> Newsgroups: bit.listserv.aix-l
> To: <aix-l@Princeton.EDU>
> Sent: Friday, April 30, 2004 9:01 AM
> Subject: Filename conversion to uppercase
>
> > Dear all,
> >
> > We need to write a script to convert filenames to uppercase. How can
> > that be done?
> >
> > For e.g. abc.txt needs to be renamed to ABC.TXT
> >
> > Regards
> >
> >
> >
> > --1083390016@njilife.com--



This archive was generated by hypermail 2.1.7 : Wed Apr 09 2008 - 22:17:53 EDT