LinuxQuestions.org
Visit Jeremy's Blog.
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, 10: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, 10: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 10:22 AM.
 
Old 10-06-2006, 10: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, 10:59 AM   #4
haertig
Senior Member
 
Registered: Nov 2004
Distribution: Debian, Ubuntu, LinuxMint, Slackware, SysrescueCD
Posts: 2,114

Rep: Reputation: 330Reputation: 330Reputation: 330Reputation: 330
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, 11:11 AM   #5
titopoquito
Senior Member
 
Registered: Jul 2004
Location: Lower Rhine region, Germany
Distribution: Slackware 14.1 (32 and 64 bit)
Posts: 1,587

Rep: Reputation: 121Reputation: 121
Another way is to use rename. Your mp3 renaming could be done with
Code:
rename ".mp3" "" *.mp3
 
Old 10-06-2006, 11: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, 11:53 AM   #7
titopoquito
Senior Member
 
Registered: Jul 2004
Location: Lower Rhine region, Germany
Distribution: Slackware 14.1 (32 and 64 bit)
Posts: 1,587

Rep: Reputation: 121Reputation: 121
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, 12:08 PM   #8
haertig
Senior Member
 
Registered: Nov 2004
Distribution: Debian, Ubuntu, LinuxMint, Slackware, SysrescueCD
Posts: 2,114

Rep: Reputation: 330Reputation: 330Reputation: 330Reputation: 330
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, 12:40 PM   #9
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Arch
Posts: 3,013

Rep: Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225
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, 02:13 PM   #10
Sepero
Member
 
Registered: Jul 2004
Location: Tampa, Florida, USA
Distribution: Ubuntu
Posts: 734
Blog Entries: 1

Rep: Reputation: 32
$ 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 02:14 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 06:46 PM
kded driving me mad !!! gazza Mandriva 3 03-18-2005 01:19 PM
apt-get, driving me mad? luqman Red Hat 1 03-03-2005 01:03 AM
kernel is driving me mad luqman Red Hat 2 02-17-2005 08:00 AM
Alsa driving me mad siphi Linux - Newbie 1 02-19-2004 09:21 PM


All times are GMT -5. The time now is 10:08 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration