Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Renaming files 2 1

Status
Not open for further replies.

jckrell2

Programmer
Nov 4, 2003
13
US
I have one more problem with the renaming of
files. Would appreciate your help.

I have many files named in the format of

xx-xx-xxx-xxx-xx.cgm exp. 21-10-150-275-02.cgm

I simply need to get rid of the last two digits
(the two digits just before the file extension
.cgm)

file 21-10-150-275-02.cgm would need to become
21-10-150-275.cgm

Could somebody please give me the script that would
make this change?

Thanks
 
Try this...
[tt]
#!/bin/ksh

for FNAME in *-*-*-*-*.cgm
do
mv ${FNAME} $(echo ${FNAME}|sed 's/-..\.cgm/.cgm/g')
done
[/tt]
Hope this helps.

 
Hi SamBones,

I don't think your sed subsitute is work. Pls. check.

tikual
 
It works on my system...
[tt]
$ echo 11-22-333-444-55.cgm | sed 's/-..\.cgm/.cgm/g'
11-22-333-444.cgm
$
[/tt]
The search is for a dash ([tt]-[/tt]), any two characters ([tt]..[/tt]), then a "[tt].cgm[/tt]". It's replaced with just the "[tt].cgm[/tt]". This makes it eliminate the last dash and the two characters that follow it.

Hope this helps.

 
Another way ...

for FNAME in *-*-*-*-*.cgm
do
mv $FNAME ${FNAME%??.cgm}.cgm
done
 
Hello,

Thanks for the help. I did use SamBones code (since he
saved me yesterday) and it worked perfectly. Thanks so much.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top