1834805 Members
2217 Online
110070 Solutions
New Discussion

Re: script-help

 
SOLVED
Go to solution
Ionut Grigorescu_2
Super Advisor

script-help

Hi gurus,

I need to write a script which convert the lowercase letter in a filename to uppercase letters. For example I have a file called bts.a303 and I want to rename it to BTS.A303. I have to do that for 20 files at once in a directory. The problem is I know there is a function called toupper but I don't know how to use it in a shell script.
Regards,
ionut .
If it weren't for STRESS I'd have no energy at all
16 REPLIES 16
Pete Randall
Outstanding Contributor

Re: script-help

From man tr:

tr "[:lower:]" "[:upper:]" file2


Pete

Pete
James R. Ferguson
Acclaimed Contributor

Re: script-help

Hi:

See the man pages for 'tr'. You can do this:

# tr "[:lower:]" "[:upper:]" < infile > outfile

Regards!

...JRF...
Steve Steel
Honored Contributor
Solution

Re: script-help

Hi

You need the shell typeset option

set -x
file=bts.a303
typeset -u fileu=$file
echo mv $file $fileu


output

+ file=bts.a303
+ typeset -u fileu=bts.a303
+ echo mv bts.a303 BTS.A303
mv bts.a303 BTS.A303

Steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
James R. Ferguson
Acclaimed Contributor

Re: script-help

Hi:

BTW, the 'toupper' function you are thinking of belongs to 'awk'. You could use it thusly to translate your lowercase to uppercase:

# echo "bts.a303"|awk '{print toupper($0)'}

Using 'tr':

# echo "bts.a303"|tr "[:lower:]" "[:upper:]"

Regards!

...JRF...
Robin Wakefield
Honored Contributor

Re: script-help

Hi,

Or you could use perl:

perl -e '{foreach $f (<*>){rename($f,uc $f)}}'

rgds, Robin
john korterman
Honored Contributor

Re: script-help

Hi ionut,
a simple script solution:


#!/usr/bin/sh

# Dir. in which the files are
FILEDIR=
cd $FILEDIR

# list of the filenames to change
FILENAMES="bts.a303 file2"
for i in $FILENAMES
do
UPPER=$(echo $i|tr "[:lower:]" "[:upper:]")
echo "mv $i $UPPER"
#mv $i $UPPER
done

where you need to set FILEDIR to the dir., in which the files are.
FILENAMES should be a list of the file names you want to rename. If it looks sensible, then uncomment the #mv line.

regards,
John K.
it would be nice if you always got a second chance
harry d brown jr
Honored Contributor

Re: script-help


How's this:

# ll
total 0
-rw-rw-rw- 1 root sys 0 Mar 10 08:26 Hkwla.888
-rw-rw-rw- 1 root sys 0 Mar 10 08:26 JEKSI.888
-rw-rw-rw- 1 root sys 0 Mar 10 08:26 hywue.888
-rw-rw-rw- 1 root sys 0 Mar 10 08:26 jsjs.888
# ls -1 | awk '{system("mv " $1 " " toupper($1));}'
mv: JEKSI.888 and JEKSI.888 are identical
# ll
total 0
-rw-rw-rw- 1 root sys 0 Mar 10 08:26 HKWLA.888
-rw-rw-rw- 1 root sys 0 Mar 10 08:26 HYWUE.888
-rw-rw-rw- 1 root sys 0 Mar 10 08:26 JEKSI.888
-rw-rw-rw- 1 root sys 0 Mar 10 08:26 JSJS.888
#


If you are willing to ignore the fact that one file was already in upper case, then the above will work.

live free or die
harry
Live Free or Die
Ionut Grigorescu_2
Super Advisor

Re: script-help

Thank you all!
case closed.

Harry: you took my breath away...
btw: it's working with $9, not $1, $1 are the permissions in ls -l output.
If it weren't for STRESS I'd have no energy at all
H.Merijn Brand (procura
Honored Contributor

Re: script-help

Same, but different :)

a5:/tmp/tmp 112 > ll
total 8
581 drwxrwxrwx 2 merijn softwr 96 Mar 10 14:44 .
2 drwxrwxrwx 7 root root 4096 Mar 10 14:44 ..
575 -rw-rw-rw- 1 merijn softwr 5 Mar 10 14:44 Gff7.org
574 -rw-rw-rw- 1 merijn softwr 5 Mar 10 14:44 aGy.7
573 -rw-rw-rw- 1 merijn softwr 5 Mar 10 14:44 aaB
103 -rw-rw-rw- 1 merijn softwr 5 Mar 10 14:44 aaa
a5:/tmp/tmp 113 > perl -MFile::Copy -e'move$_,uc for<*>'
a5:/tmp/tmp 114 > ll
total 8
581 drwxrwxrwx 2 merijn softwr 96 Mar 10 14:44 .
2 drwxrwxrwx 7 root root 4096 Mar 10 14:44 ..
103 -rw-rw-rw- 1 merijn softwr 5 Mar 10 14:44 AAA
573 -rw-rw-rw- 1 merijn softwr 5 Mar 10 14:44 AAB
574 -rw-rw-rw- 1 merijn softwr 5 Mar 10 14:44 AGY.7
575 -rw-rw-rw- 1 merijn softwr 5 Mar 10 14:44 GFF7.ORG
a5:/tmp/tmp 115 >

Enjoy, have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Pete Randall
Outstanding Contributor

Re: script-help

That's because that's a numeral one (ls -1) not the letter l (ls -l).

Pete

Pete
Robin Wakefield
Honored Contributor

Re: script-help

Hi,

The ls command doesn't actually need the "-1" switch if stdout is a pipe:

ls | awk '{print $1}'

is identical to:

ls -1 | awk '{print $1}'

rgds, Robin
James R. Ferguson
Acclaimed Contributor

Re: script-help

Hi (again):

If you are going to leverage 'ls' and move your files, I'd consider filtering and processing *only* files:

# cd
# ls -l |awk '/^-/ {system("mv " $9 " "toupper($9))}'

Regards!

...JRF...
Ionut Grigorescu_2
Super Advisor

Re: script-help

procura: I have to learn Perl first. Using your perl line is something like sorcery for me - it works maybe, but I'm not understanding how it does what it does...btw: I think I need a perl module or something like this, cause I have an error: unrecognized switch: MFile...
If it weren't for STRESS I'd have no energy at all
H.Merijn Brand (procura
Honored Contributor

Re: script-help

Ahh :) I guessed that much anyway. I'm regularly using `new' perl functionality in this group to tease the readers, and show how effective perl scripting can be. Anyway, explaining all a bit for you

-M means 'use a mudule'
if that fails, you're probably using perl4.x, which is quite old. I'm using 5.8.0 plus some extra patches.
You can check your version with 'perl -v' (short) or 'perl -V' (long), which should IMHO show at least 5.6.1 to be useful for /my/ examples
Check the location of your perl binary with 'which perl'. perl4 is located in /usr/contrib/bin. If perl5 is installed on your system, it can be found in either /opt/perl5 or /usr/local (the executable for both in bin/perl). If it is not, it is freely available from several sources (https://www.beepz.com/personal/merijn/ or http://www.cmve.net/~merijn/ or http://hpux.connect.org.uk/hppd/hpux/Languages/perl-5.8.0/ to name a few)


Now for the script:

perl -MFile::Copy -e'move$_,uc for<*>'

-MFile::Copy uses the File::Copy module that is included in the CORE distribution of Perl, and offers OS-independant copy/move commands

-e passes an expression to perl. The expression here is 'move$_,uc for<*>', which only parses OK in newer perls where the 'for' loop is allowed as a statement modifier, allowing to leave out the parenteses. Rewritten in C-like structure, that would read as

for (glob ("*")) {
move ($_, uc $_);
}

<*> is a shorthand for a list of all files, which is effectively fetched with perl's 'glob' function. Most base functions in perl have optional arguments that default to the global variable $_. 'uc' is the "uppercase" function. Parenteses are aften optional too, and spaces also, as long as leaving them out does not confuses the parser.

Enjoy, have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Ionut Grigorescu_2
Super Advisor

Re: script-help

OK, procura, I have perl 4.0 on the 712 workstation I use for test purposes. I shall install the latest perl you've recommended. Thank you for the explanations! :-))
If it weren't for STRESS I'd have no energy at all
Ionut Grigorescu_2
Super Advisor

Re: script-help

Procura: it works wonderful! It remains only to learn perl ;-) indeed, there is more than one way...
If it weren't for STRESS I'd have no energy at all