Operating System - HP-UX
1819915 Members
2352 Online
109607 Solutions
New Discussion юеВ

script rename files in dir. by stripping last 3 characters

 
SOLVED
Go to solution
Sammy_2
Super Advisor

script rename files in dir. by stripping last 3 characters

How do I strip word .SAM from the list of files below ?
=================
# ls
KEYA.SAM
Slyton.SAM
web_startfeb27.bup.SAM
WWW.SAM web.start.wand.sh.SAM
...
...
=====================

Need to be like:
KEYA
Slyton
web_startfeb27.bup
web.start.wand.sh
...
..
good judgement comes from experience and experience comes from bad judgement.
15 REPLIES 15
Hazem Mahmoud_3
Respected Contributor
Solution

Re: script rename files in dir. by stripping last 3 characters

Run the below in a script:

for file in //*.SAM
do
file2=`echo $file | sed 's/.SAM//g'`
mv $file $file2
done


-Hazem
Jeff_Traigle
Honored Contributor

Re: script rename files in dir. by stripping last 3 characters

Should be able to reduce that to a one-liner in the for loop like this:

for FILE in //*.SAM
do
mv ${FILE} $((basename ${FILE} .SAM))
done
--
Jeff Traigle
Sammy_2
Super Advisor

Re: script rename files in dir. by stripping last 3 characters

Thanks Hazem. Works beautimously.
Jeff, I tried your script as well but
I get:
# for FILE in /home/paradigm/scripts/WED/*SAM
> do
> mv ${FILE} $((basename ${FILE} .SAM))
> done
ksh[3]: basename /home/paradigm/scripts/WED/KEYA.SAM .SAM: bad number
# x
jeff.It should work because I have seen other programmes use your login in their scripts. i will look at my syntax in ksh.
good judgement comes from experience and experience comes from bad judgement.
Jeff_Traigle
Honored Contributor

Re: script rename files in dir. by stripping last 3 characters

Ah... ksh doesn't recognize the $(()) construct. Have to use `basename ${FILE} .SAM` instead.

Of course, I also realized that this assumes you're in the same directory as the files you're renaming since basename will strip the leading directory path if it exists. You could still keep the one-liner in there by putting a `dirname ${FILE}` in front of the `basename ${FILE} .SAM` probably. That just gets kind of verbose compared to the sed option though. :)
--
Jeff Traigle
Sammy_2
Super Advisor

Re: script rename files in dir. by stripping last 3 characters

Thanks Jeff. Works like a charm now. I should have specified I am doing this in ksh.
good judgement comes from experience and experience comes from bad judgement.
Jeff_Traigle
Honored Contributor

Re: script rename files in dir. by stripping last 3 characters

I know we answered your question already, but I realized my initial goof and wanted to clear up the problems with things I stated... $((expr)) evaluates arithmetically which is why you got the bad magic number message... $(expr) performs the command substitution I meant to have initially and is equivalent to `expr`. Works in both POSIX and Korn shells.
--
Jeff Traigle
Robert Dill
Advisor

Re: script rename files in dir. by stripping last 3 characters

There is some cute parameter stuff in "man sh-posix" and "man ksh".
Try:
for FILE in $DIR/*.SAM
do
mv $DIR/$FILE $DIR/${FILE%.SAM}
done
You can do a lot with that type of trick.
Bob
Sammy_2
Super Advisor

Re: script rename files in dir. by stripping last 3 characters

Dill,
Nice suggestion but
I get the usage error message below when I run your script in ksh.
============================
-rw------- 1 root sys 440 Apr 21 16:00 web_startfeb27.SAM
-rw------- 1 root sys 442 Apr 21 16:00 web_startfeb27.bup.SAM
-rw-r--r-- 1 root sys 9 Apr 21 16:00 wed.SAM
-rw-r--r-- 1 root sys 0 Apr 21 16:00 wed.bup.SAM
potion:for FILE in $DIR/*.SAM
> do
> mv $DIR/$FILE $DIR/${FILE%.SAM}
> done
Usage: mv [-f] [-i] [-e warn|force|ignore] f1 f2
mv [-f] [-i] [-e warn|force|ignore] f1 ... fn d1
mv [-f] [-i] [-e warn|force|ignore] d1 d2
good judgement comes from experience and experience comes from bad judgement.
H.Merijn Brand (procura
Honored Contributor

Re: script rename files in dir. by stripping last 3 characters

# perl -MFile::Copy -e'for(@ARGV){($n=$_)=~s/\.sam$/i;move($_,$n)' *.SAM *.sam

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
H.Merijn Brand (procura
Honored Contributor

Re: script rename files in dir. by stripping last 3 characters

# perl -MFile::Copy -e'for(@ARGV){($n=$_)=~s/\.sam$/i;move($_,$n)}' *.SAM *.sam

Sorry, forgot the traiting }

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Sammy_2
Super Advisor

Re: script rename files in dir. by stripping last 3 characters

procura,
I get the substitution message below. I have perl 5.6.
Thanks

rw------- 1 root sys 442 Apr 21 16:00 web_startfeb27.bup.SAM
-rw-r--r-- 1 root sys 9 Apr 21 16:00 wed.SAM
-rw-r--r-- 1 root sys 0 Apr 21 16:00 wed.bup.SAM
potion:perl -MFile::Copy -e'for(@ARGV){($n=$_)=~s/\.sam$/i;move($_,$n)}' *.SAM *
.sam
Substitution replacement not terminated at -e line 1.
good judgement comes from experience and experience comes from bad judgement.
H.Merijn Brand (procura
Honored Contributor

Re: script rename files in dir. by stripping last 3 characters

# perl -MFile::Copy -e'for(@ARGV){($n=$_)=~s/\.sam$//i;move($_,$n)}' *.SAM *.sam

missing '/' before i

And of course perl was right :/
perl-5.6.x should do fine

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Sammy_2
Super Advisor

Re: script rename files in dir. by stripping last 3 characters

Thanks Procura. I know , especially, you can do pretty much everything in perl. That worked fine although your perl is too advance for my taste to understand what is that one liner doing. I will crack open my Perl (reilly book)to figure it out. But Happy I am.
good judgement comes from experience and experience comes from bad judgement.
H.Merijn Brand (procura
Honored Contributor

Re: script rename files in dir. by stripping last 3 characters

Easy

> # perl -MFile::Copy

use module File::Copy. Present in all perl ports.
# man File::Copy for the manual page

> -e'
define the script

> for (@ARGV) {
for all command line arguments

> ($n = $_) =~ s/\.sam$//i;
set $n to the argument, and on the fly remove the .sam extension case insensitive (/i)

> move ($_, $n)
move () is from File::Copy, and does the same as unix' mv command, ut is safe accross OS's, so it'll also work on Win32, OS/2, and VMS

> }' *.SAM *.sam
and these are the command line arguments.

Enjoy, Have FUN! H.Merijn [ no magic involved this time. Only typo's :) ]
Enjoy, Have FUN! H.Merijn
Sammy_2
Super Advisor

Re: script rename files in dir. by stripping last 3 characters

Thank You very much Procura for breaking it down for me and my teammates. Probably I learn more from you then from this thick "Perl in 21 days" book I had for 3 years.
good judgement comes from experience and experience comes from bad judgement.