LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-06-2006, 09:03 AM   #1
Freestone
Member
 
Registered: Jan 2003
Location: Brighton, Michigan
Distribution: FC5
Posts: 114

Rep: Reputation: 15
'tr' command driving me mad!


Greetings.

I have been reading the man and info pages and various online guides concerning the 'tr' command and I am getting absolutely nowhere. I've created a file with 'vim' and I want to alter the filename with 'tr' and
I'm stuck. Here is the filename:
Quote:
text-to-test-on.txt
What I am trying to accomplish is this:
Quote:
text to test on.txt
I am looking to replace the '-' with a whitespace. I know I can accomplish this with the rename command via
Quote:
rename text-to-test-on.txt text\ to\ test\ on.txt text-to-test-on.txt
but that isn't any fun! I am looking to learn some new things.

This is what I have tried:
Quote:
for i in *.txt;do mv "$i" `echo $i | tr [:space:] '-' `; done
That and many other combinations...I'm just not getting it! Is what I am trying to do possible with the 'tr' command?

Thanks a bunch!
 
Old 10-06-2006, 09:11 AM   #2
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
I guess the problem is the for loop. I loops over the content of *.txt separated by space characters, i.e. "text to test on.txt" will lead to four cycles. Try the following:
Code:
(IFS=$'\n'; for i in *.txt;do mv "$i" `echo $i | tr [:space:] '-' `; done)
Search "man bash" for IFS to see what's going on.

Edit: Seems like I was wrong. You could try using echo -n "$i" instead of echo $i, but I don't think it will make much of a difference. Try and use the "set -x" command to find out what's going on in detail.

Last edited by spirit receiver; 10-06-2006 at 09:22 AM.
 
Old 10-06-2006, 09:35 AM   #3
Freestone
Member
 
Registered: Jan 2003
Location: Brighton, Michigan
Distribution: FC5
Posts: 114

Original Poster
Rep: Reputation: 15
spiritreceiver,

I tried wht you suggested and I ended up with the following:
Quote:
text-to-test-on.txt

IFS=$'\n';for i in *.txt;do mv "$i" `echo $i | tr [:space:] '-'`;done

text-to-test-on.txt-
It added a '-' to the end. I'm actually laughing at this. (I guess I'm starting to mature. I used to get angry when something didn't work.)
The reason I'm trying so hard to learn this is because I was experimenting with a whole directory of mp3 files and I wanted to remove the '.mp3' from each file. What I ended up with was this:
Quote:
Name-of-song.mp3.mp3.mp3.mp3
Thats not what I was looking for!
Anyway, if someone who reads this thread knows what I'm trying to do, please post!

Thanks!
 
Old 10-06-2006, 09:59 AM   #4
haertig
Senior Member
 
Registered: Nov 2004
Distribution: Debian, Ubuntu, LinuxMint, Slackware, SysrescueCD, Raspbian, Arch
Posts: 2,331

Rep: Reputation: 357Reputation: 357Reputation: 357Reputation: 357
I didn't try this solution to prove it, but I'm about 99% sure that you just need to put you backticked echo command inside double quotes.

Like this:
Code:
mv $i "`echo $i | tr blah blah blah`"
 
Old 10-06-2006, 10:11 AM   #5
titopoquito
Senior Member
 
Registered: Jul 2004
Location: Lower Rhine region, Germany
Distribution: Slackware64 14.2 and current, SlackwareARM current
Posts: 1,644

Rep: Reputation: 145Reputation: 145
Another way is to use rename. Your mp3 renaming could be done with
Code:
rename ".mp3" "" *.mp3
 
Old 10-06-2006, 10:27 AM   #6
Freestone
Member
 
Registered: Jan 2003
Location: Brighton, Michigan
Distribution: FC5
Posts: 114

Original Poster
Rep: Reputation: 15
titopoquito,

In my original post I mentioned that I know I can use the 'rename' command.
I have a directory of mp3's that I accidentally renamed to 'song.mp3.mp3.mp3.mp3'.
'tr' is the command that I am trying to learn.

It bothers me when people don't read the complete thread and jump in and offer a suggestion without regard to the whole thread.

Thanks anyway for your input.
 
Old 10-06-2006, 10:53 AM   #7
titopoquito
Senior Member
 
Registered: Jul 2004
Location: Lower Rhine region, Germany
Distribution: Slackware64 14.2 and current, SlackwareARM current
Posts: 1,644

Rep: Reputation: 145Reputation: 145
Quote:
Originally Posted by Freestone
titopoquito,

In my original post I mentioned that I know I can use the 'rename' command.

It bothers me when people don't read the complete thread and jump in and offer a suggestion without regard to the whole thread.

Thanks anyway for your input.
I actually read your post, but you first said you wanted to substitute dashes with spaces and rename is probably not the best command to do this (since it replaces only the first occurance of the replace pattern). You wrote later about changing the file extensions for which rename IS a good solution. So no offense intended, but it was not clear to me that you didn't want to use "rename" for BOTH.
 
Old 10-06-2006, 11:08 AM   #8
haertig
Senior Member
 
Registered: Nov 2004
Distribution: Debian, Ubuntu, LinuxMint, Slackware, SysrescueCD, Raspbian, Arch
Posts: 2,331

Rep: Reputation: 357Reputation: 357Reputation: 357Reputation: 357
Quote:
Originally Posted by Freestone
It bothers me when people don't read the complete thread and jump in and offer a suggestion without regard to the whole thread.
I have a suggestion here. It's usually counterproductive to throw out veiled holier-than-thou digs at those trying to help you.

FWIW, there are lots of threads on these forums. Lots! I read and respond to quite a few, as do many other people offering their help for free. I can only speak for myself, but I do not read every post in every thread. I typically look at the original post, quickly scan it (not necessarily read it in depth), to see what the jist of the problem is. Then I quickly scan responses to see if my initial thoughts have already been covered by any other responder, or whether another responder has offered advice that I consider bad or incorrect.

This is because posts are often times overly verbose. My own are a prime example of this. What can be said in ten words, I'll say in 17,386. I know and understand this about myself, and certainly wouldn't fault someone for skimming my words and picking out the ten that are actually relavent.

The jist I picked up from your post was "Here's a guy who has filenames with dashes in them and he wants to rename those with spaces." I did scan a tad further, and picked up on your comment about "learning tr", so I dropped you a hint that related to spaces in filenames and double quotes. Didn't spell it out for you, but dropped you a hint.

You have to realize that your typical post-responder-helper-person may very well scan right over your "supporting details", home in on the "synopsis", and write off what you've already tried (and failed at) as "He's trying something pretty weird and inefficient, I'll suggest a better way."

I don't know about titopoquito, but I hit "submit reply" on my post above as I was being harried by my daughter, "Come on Dad, we have to go!" I did not preview it, nor reread your original post (things I usually do). Just a quick "I'll throw this guy something, and maybe it might help him". My suggestion may have been well off-base since it was done in haste without a complete question and thread review.

If it bothers you that people try to quickly help you without analyzing every word you have typed previously, it bothers me that you actually expect people to help you in the first place.
 
Old 10-06-2006, 11:40 AM   #9
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by Freestone
spiritreceiver,

I tried wht you suggested and I ended up with the following:

Quote:
text-to-test-on.txt

IFS=$'\n';for i in *.txt;do mv "$i" `echo $i | tr [:space:] '-'`;done

text-to-test-on.txt-
It added a '-' to the end.
You have to use [:blank:] instead of [:space:], [:space:] matches the newline
 
Old 10-06-2006, 01:13 PM   #10
Sepero
Member
 
Registered: Jul 2004
Location: Tampa, Florida, USA
Distribution: Ubuntu
Posts: 734
Blog Entries: 1

Rep: Reputation: 33
$ echo "I'm-a-retard" | tr "-" " "
I'm a retard



for i in *.txt;do mv "$i" `echo "$i" | tr "-" " "`; done

Last edited by Sepero; 10-06-2006 at 01:14 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
DNS driving me mad kkiedrowski Linux - Networking 1 11-27-2005 05:46 PM
kded driving me mad !!! gazza Mandriva 3 03-18-2005 12:19 PM
apt-get, driving me mad? luqman Red Hat 1 03-03-2005 12:03 AM
kernel is driving me mad luqman Red Hat 2 02-17-2005 07:00 AM
Alsa driving me mad siphi Linux - Newbie 1 02-19-2004 08:21 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:11 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration