![](https://seccdn.libravatar.org/avatar/93c680d385f945fd267db4b1609a81df.jpg?s=120&d=mm&r=g)
Christopher, I didn't know that I could use sed-like substitutions with bash variables! Neat little tool! I checked and it doesn't work for ksh or sh however ;-< I would suggest, just to be safe, adding a -d option to the ls command like this: ls -d *SNG* | while read name; do new=${name/.SNG.txt/.txt} mv $name $new done Without this option, if there is a *SNG* that is a directory you may get very unsatisfactory results. Grant Q -----Original Message----- From: Christopher Mahmood [mailto:ckm@suse.com] Sent: Tuesday, June 17, 2003 2:59 PM To: SuSE List Subject: Re: [SLE] Shell script query * James Ogley (james@usr-local-bin.org) [030617 14:48]:
filename.SNG.txt
(don't ask - long story...)
Now, filename is a random sequence of between 5 and eight numeric characters.
My question is this:
Does anyone know of a way in bash script to rename all the files of the above format to filename.txt?
Something like ls *SNG* | while read name; do new=${name/.SNG.txt/.txt} mv $name $new done -- -ckm -- Check the headers for your unsubscription address For additional commands send e-mail to suse-linux-e-help@suse.com Also check the archives at http://lists.suse.com Please read the FAQs: suse-linux-e-faq@suse.com
![](https://seccdn.libravatar.org/avatar/d70f8a8240c69007852b9d1bf27e170b.jpg?s=120&d=mm&r=g)
* Quinlan, Grant (Grant_Quinlan@bmc.com) [030617 15:29]:
I didn't know that I could use sed-like substitutions with bash variables!
There are more of them, see the 'Parameter Expansion' section of bash(1). '${!prefix*}' is a pretty evil one.
Neat little tool! I checked and it doesn't work for ksh or sh however ;-<
Yeah, ksh can do '%%' like someone else suggested. -- -ckm
![](https://seccdn.libravatar.org/avatar/8cf53a3d0edae578a2cb0d7c5e9cba69.jpg?s=120&d=mm&r=g)
On Tue, Jun 17, 2003 at 03:58:08PM -0700, Christopher Mahmood wrote: : * Quinlan, Grant (Grant_Quinlan@bmc.com) [030617 15:29]: : > I didn't know that I could use sed-like substitutions with bash variables! : : There are more of them, see the 'Parameter Expansion' section of : bash(1). '${!prefix*}' is a pretty evil one. : : > Neat little tool! I checked and it doesn't work for ksh or sh however ;-< For sh/ksh88 do the following: for i in `ls -d *SNG*`; do mv $i `echo $i | sed 's/\.SNG//g' done : Yeah, ksh can do '%%' like someone else suggested. ksh93 also supports sed-like substitions. --Jerry -- Open-Source software isn't a matter of life or death... ...It's much more important than that!
![](https://seccdn.libravatar.org/avatar/b9171464f4629a0067d23c63bbdeaca4.jpg?s=120&d=mm&r=g)
On Wed, 2003-06-18 at 04:37, Jerry A! wrote:
For sh/ksh88 do the following:
for i in `ls -d *SNG*`; do mv $i `echo $i | sed 's/\.SNG//g' done
While this example might be easy to follow, it on two occasions invoke external binaries, 'ls' and 'sed', which in turn invoke another copy of the shell. It is ineffective. The snippet I suggested last night does not. If you are doing something like this, but for say 100,000 files, you want your script to invoke as little as possible outside itself. The use of %, %%, # and ## can cut down drastically the amount of sed usage and therefor do a significant amount for the speed and resource usage of your script.
: Yeah, ksh can do '%%' like someone else suggested.
ksh93 also supports sed-like substitions.
This was a feature I was unaware of. Now that I know it is there, I'll
be making good use of it. :)
Regards,
--
Anders Karlsson
![](https://seccdn.libravatar.org/avatar/8cf53a3d0edae578a2cb0d7c5e9cba69.jpg?s=120&d=mm&r=g)
On Wed, Jun 18, 2003 at 06:08:28AM +0100, Anders Karlsson wrote: : On Wed, 2003-06-18 at 04:37, Jerry A! wrote: : > For sh/ksh88 do the following: : > : > for i in `ls -d *SNG*`; do : > mv $i `echo $i | sed 's/\.SNG//g' : > done : : While this example might be easy to follow, it on two occasions invoke : external binaries, 'ls' and 'sed', which in turn invoke another copy of : the shell. It is ineffective. The snippet I suggested last night does : not. You're absolutely right. Sorry I was being lazy in answering the question in regards to making this work with sh and ksh. While the 'ls' in unnecessary, the 'sed' is still needed for Bourne shell since it doesn't support %/%%/#/## parameter expansion. Regardless, I'll make sure I'm not so sloppy in the future. : > : Yeah, ksh can do '%%' like someone else suggested. : > : > ksh93 also supports sed-like substitions. : : This was a feature I was unaware of. Now that I know it is there, I'll : be making good use of it. :) It's had it since ksh93 was introduced in well 1993. I believe that this predates bash's support of this feature (but I'm not certain). --Jerry -- Open-Source software isn't a matter of life or death... ...It's much more important than that!
![](https://seccdn.libravatar.org/avatar/7bb3ab4572bd491ad0e437ca0da69c6c.jpg?s=120&d=mm&r=g)
If you are doing something like this, but for say 100,000 files, you want your script to invoke as little as possible outside itself. The use of %, %%, # and ## can cut down drastically the amount of sed usage and therefor do a significant amount for the speed and resource usage of your script.
Wow, quite a discussion, I've had several suggestions on how to do it, I'll be at the box in question to do the job at the weekend, many thanks to all who helped. And the discussion is quite cool (makes a change from all the SCO stuff recently - anyone else as bored as me with it?), seems with BASH, as with Perl, TMTOWTDI ;) -- James Ogley, Webmaster, Rubber Turnip james@rubberturnip.org.uk http://www.rubberturnip.org.uk Jabber: riggwelter@myjabber.net Using Free Software since 1994, running GNU/Linux (SuSE 8.2). GNOME updates for SuSE: http://www.usr-local-bin.org
participants (5)
-
Anders Karlsson
-
Christopher Mahmood
-
James Ogley
-
Jerry A!
-
Quinlan, Grant