|
Home > Archive > alt.os.linux > October 2002 > Using tr to replace a specific string of characters
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Using tr to replace a specific string of characters
|
|
| Erich J. Wahl 2002-10-14, 9:24 pm |
| I have some files named foobar1.avi.mpg, foobar2.avi.mpg and
foobar3.avi.mpg. How do I strip out the ".avi" so they now are
foobar1.mpg, foobar2.mpg and foobar3.mpg?
I've used a script like this:
for i in *mpg; do mv "$i" `echo $i | tr -d '.avi'`; done
but that leaves me with foobr1mpg, foobr2mpg and foobr3mpg.
I want it to delete the exact phrase '.avi' and not every occurrence of
'.', 'a', 'v' and 'i'.
Any suggestions?
TIA,
Erich
| |
| Daniel Jensen 2002-10-14, 9:24 pm |
| "Erich J. Wahl" <ejwahl@NOSPAMhotmail.com> writes:
> I have some files named foobar1.avi.mpg, foobar2.avi.mpg and
> foobar3.avi.mpg. How do I strip out the ".avi" so they now are
> foobar1.mpg, foobar2.mpg and foobar3.mpg?
>
> I've used a script like this:
>
> for i in *mpg; do mv "$i" `echo $i | tr -d '.avi'`; done
>
> but that leaves me with foobr1mpg, foobr2mpg and foobr3mpg.
>
> I want it to delete the exact phrase '.avi' and not every occurrence
> of '.', 'a', 'v' and 'i'.
You don't use tr for that. tr is for translating or deleting
*characters*, not strings.
You can use sed for that purpose. Like this:
for i in *.mpg; do mv "$i" `echo $i | sed 's/\.avi//'`; done
--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet?
| |
| +Chiron+ 2002-10-15, 1:24 am |
| -----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On Tue, 15 Oct 2002 01:45:20 GMT, Erich J. Wahl tempted the fates in
alt.os.linux by proclaiming the following:
> I have some files named foobar1.avi.mpg, foobar2.avi.mpg and
> foobar3.avi.mpg. How do I strip out the ".avi" so they now are
> foobar1.mpg, foobar2.mpg and foobar3.mpg?
[snip]
> Any suggestions?
Yup.
Use 'ren'.
http://freshmeat.net/projects/ren/
Then you'd be able to use wildcards like this:
bash~# ren 'foobar*.avi.mpg' 'foobar#1.mpg'
which would change these:
foobar1.avi.mpg
foobar2.avi.mpg
foobar3.avi.mpg
to these:
foobar1.mpg
foobar2.mpg
foobar3.mpg
Granted, it's a 'lazy' way of doing it, but I don't care. =)
Work smarter - not harder, and all that. 
- --
+Chiron+ | But it does move! -- Galileo Galilei
GnuPG Pub Key 848D1A2D -o) |
Linux Kernel 2.4.19 /\\ |
Slackware 8.1 *w00t* _\_v |
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.0 (GNU/Linux)
iD8DBQE9q7BUe8wcrYSNGi0RAi20AK
CKS5E2tSkAeqohaFJMZNjlAHAOfACf
T+Ws
4012RSReg3TW1/CPzIlmRDw=
=i81t
-----END PGP SIGNATURE-----
| |
| +Chiron+ 2002-10-15, 1:24 am |
| -----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On 15 Oct 2002 04:19:01 +0200, Daniel Jensen tempted the fates in
alt.os.linux by proclaiming the following:
> You don't use tr for that. tr is for translating or deleting
> *characters*, not strings.
>
> You can use sed for that purpose. Like this:
>
> for i in *.mpg; do mv "$i" `echo $i | sed 's/\.avi//'`; done
My way's shorter. 
- --
+Chiron+ | AUTOMOBILE: A four-wheeled vehicle that
GnuPG Pub Key 848D1A2D -o) | runs up hills and down pedestrians.
Linux Kernel 2.4.19 /\\ |
Slackware 8.1 *w00t* _\_v |
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.0 (GNU/Linux)
iD8DBQE9q7Che8wcrYSNGi0RAsCsAJ
40wUZqa1My23QKkcVQAZB2XhNmFgCg
lYlI
s+wOUlX3y7XNPaAjB1w5Xmo=
=sENB
-----END PGP SIGNATURE-----
| |
| Paul Lutus 2002-10-15, 3:24 am |
| +Chiron+ wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> On 15 Oct 2002 04:19:01 +0200, Daniel Jensen tempted the fates in
> alt.os.linux by proclaiming the following:
>
>> You don't use tr for that. tr is for translating or deleting
>> *characters*, not strings.
>>
>> You can use sed for that purpose. Like this:
>>
>> for i in *.mpg; do mv "$i" `echo $i | sed 's/\.avi//'`; done
>
> My way's shorter. 
Not if you include the download and install time for "ren". And "sed" is in
every Linux installation, so learning it produces a portable skill. 
--
Paul Lutus
www.arachnoid.com
| |
| Daniel Jensen 2002-10-15, 5:24 am |
| Paul Lutus <nospam@nosite.zzz> writes:
> +Chiron+ wrote:
>
> > -----BEGIN PGP SIGNED MESSAGE-----
> > Hash: SHA1
> >
> > On 15 Oct 2002 04:19:01 +0200, Daniel Jensen tempted the fates in
> > alt.os.linux by proclaiming the following:
> >
> >> You don't use tr for that. tr is for translating or deleting
> >> *characters*, not strings.
> >>
> >> You can use sed for that purpose. Like this:
> >>
> >> for i in *.mpg; do mv "$i" `echo $i | sed 's/\.avi//'`; done
> >
> > My way's shorter. 
>
> Not if you include the download and install time for "ren". And "sed" is in
> every Linux installation, so learning it produces a portable skill. 
That's very true. And besides, the OP just had to change his script
from tr to sed, which would be an easy job.
--
Obligatory uptime signature:
11:56am up 3 days, 18:50, 1 user, load average: 0.08, 0.08, 0.05
| |
| Lucius Chiaraviglio 2002-10-18, 11:24 pm |
| Sorry about the off-topic answer, but . . .
Daniel Jensen <daniel@bigwalter.net> wrote:
>[. . .]
>--
>A: Because it messes up the order in which people normally read text.
>Q: Why is top-posting such a bad thing?
>A: Top-posting.
>Q: What is the most annoying thing on usenet?
I like that! Mind if the rest of us ste-, er, borrow this?
| |
| Daniel Jensen 2002-10-19, 5:24 am |
| lucius1@telo_large_urban_area.com (Lucius Chiaraviglio) writes:
> Sorry about the off-topic answer, but . . .
>
> Daniel Jensen <daniel@bigwalter.net> wrote:
>>[. . .]
>>--
>>A: Because it messes up the order in which people normally read text.
>>Q: Why is top-posting such a bad thing?
>>A: Top-posting.
>>Q: What is the most annoying thing on usenet?
>
> I like that! Mind if the rest of us ste-, er, borrow this?
Go ahead, I 'borrowed' it myself.
--
10 Daniel Jensen
20 daniel@bigwalter.net
30 GOTO 10
|
|
|
|
|