LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   'tr' command driving me mad! (https://www.linuxquestions.org/questions/linux-newbie-8/tr-command-driving-me-mad-489991/)

Freestone 10-06-2006 09:03 AM

'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!

spirit receiver 10-06-2006 09:11 AM

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.

Freestone 10-06-2006 09:35 AM

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!

haertig 10-06-2006 09:59 AM

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`"

titopoquito 10-06-2006 10:11 AM

Another way is to use rename. Your mp3 renaming could be done with
Code:

rename ".mp3" "" *.mp3

Freestone 10-06-2006 10:27 AM

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.

titopoquito 10-06-2006 10:53 AM

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.

haertig 10-06-2006 11:08 AM

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.

ntubski 10-06-2006 11:40 AM

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

Sepero 10-06-2006 01:13 PM

$ echo "I'm-a-retard" | tr "-" " "
I'm a retard



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


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