1846742 Members
5656 Online
110256 Solutions
New Discussion

Re: Unix script

 
SOLVED
Go to solution
David B. Bordwick
Occasional Contributor

Unix script

Does anyone have a script to convert lower case file names to upper case?
i.e., (abcdef.cbl to ABCDEF.cbl)
14 REPLIES 14
James R. Ferguson
Acclaimed Contributor

Re: Unix script

Hi David:

'tr' will do this. For exampe:

# echo "abc"|tr -s '[:lower:]' '[:upper:]'

...JRF...
Bill McNAMARA_1
Honored Contributor

Re: Unix script

you want to be looking at tr in the following way

with a few move commands too...

tr [a-z] [A-Z]

I try to root something out...
Later,
Bill
It works for me (tm)
Sachin Patel
Honored Contributor

Re: Unix script

Hi
I don't have script, but you can use perl to do it. Perl has function called "uc"

For example
#!/usr/local/bin/perl
open(LOWER,"filename");
open(UPPER,"foreach (LOWER)
$line=uc;
print UPPER "$line2";
end

Sachin
Is photography a hobby or another way to spend $
John Poff
Honored Contributor

Re: Unix script

I also like to use the typeset command in a script to handle jobs like this:

for f in *
do
typeset -u UCASE=$f
print $f $UCASE
done


The -u option on the typeset command sets the UCASE variable to be upper case. I like this method because I'm too lazy to remember all the options for the 'tr' command.



James R. Ferguson
Acclaimed Contributor

Re: Unix script

Hi (again):

Looking further at what you wrote, if you wanted to keep the file extension part (after the dot) in lower case, you might use 'awk' like this, for example:

# echo "abc.txt"|awk -F. '{X=toupper($1);print X FS $2}'

This sets the field delimiter as a dot character, translates the first field to uppercase, and outputs the translated field, the field separator (the dot) and the second, unaltered field.

Regards!

...JRF...
Vincenzo Restuccia
Honored Contributor

Re: Unix script

cd /PATH
ll *.cbl|awk '{print $9}'>file
tr [a-z] [A-Z] file2
paste file file2 >file3
while read i,j
do
mv $i $j
done



Rob Mallard
Valued Contributor

Re: Unix script

This is what I use:

#!/usr/bin/sh
#
# Script to convert mixed case names
# to lower case
#
for upper_name in *
do
lower_name=$(echo $upper_name | tr "[:upper:]" "[:lower:]")
mv $upper_name $lower_name 2> /dev/null
done
Victor_5
Trusted Contributor

Re: Unix script

Try this one:

for file
do
my $file `echo$file | tr "[a-z]" "[A-Z]"`
done
federico_3
Honored Contributor

Re: Unix script


look at man tr:

you'll find -> tr -s '[:upper:]''[:lower:]'
Laurent Paumier
Trusted Contributor

Re: Unix script

Be careful with the tr command, it uses the LC_COLLATE variable... try the following commands :

# export LC_COLLATE=fr_FR.iso88591
# echo ABCDEF | tr '[a-z]' '[A-Z]'

Not really the result I would expect... But who cares about non US english people... ;)

Re: Unix script

#BEGINN
#!/bin/sh
echo "directory who file reside \c"; read DIR
echo "Waiting...."
cd $DIR
for j in .name of any directory?s if there are.
do
cd $DIR/$j
for i in `ls -1`
do
mv $i `echo $i | tr "[a-z]" "[A-Z]"`
done
done

I think i could help you
Mohamed
the world of unix is beautifull
Bruce Regittko_1
Esteemed Contributor

Re: Unix script

Hi,

I agree that if you don't want to convert the extention then perl or awk are your best bets. Also, if you use tr, you probably do not want the -s option, which squeezes two or more repeating characters to a single character. Thus,

echo "aabbcc" | tr -s "[:lower:]" "[:upper:]"

yields the string "ABC", not "AABBCC".

--Bruce
www.stratech.com/training
James R. Ferguson
Acclaimed Contributor

Re: Unix script

Hi Bruce:

Ah, yes, good catch. I didn't mean to (s)queeze the multiple occurances to one in this case! I did (do) it frequently for strings involving blank characters and ...well...

Thanks!

...JRF...

Deepak_5
Advisor
Solution

Re: Unix script

Hi,
This will list pick all .cbl from a directory
and turn to upper case.
Currently cp is commented, just incase you
need it, uncomment it.
Hope it helps.
#**Start Script****
for i in *.cbl
do
echo $i
name_no_ext=`echo $i| cut -f1 -d"."|tr [a-z] [A-Z] `
echo "Original : " $i
echo "Upper Case : " $name_no_ext.cbl
#cp $i $name_no_ext.cbl
done
#**End Script****

Thanks and Regards
Deepak