Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
06-14-2004, 05:28 PM
|
#1
|
Member
Registered: Aug 2003
Distribution: Arch Linux
Posts: 140
Rep:
|
File name manipulation with tr, concatination troubles
I apologize, but this is a pretty weak programming question. I just didn't think it fit very well into the the Linux-General forum.
In many of my files I had the pattern " - ", that is whitespace hyphen whitespace. I didn't like having whitespaces in my file names so I used tr to change whitespaces to underscores and hyphens also to underscores.
I'd then hoped to use tr to change "___" to simply "_" in the same manner that I'd done my earlier manipulations. This however does not work. Rather than searching for the entire ___ pattern tr searches for just _ . So nothing changes.
Does anyone know how to force tr to search for exact strings, or even if it will?
|
|
|
06-14-2004, 06:07 PM
|
#2
|
Member
Registered: Mar 2004
Location: bursa
Posts: 110
Rep:
|
Have you tried the -d option to 'tr'?
Also, try 'sed'... it's fun.
|
|
|
06-14-2004, 06:11 PM
|
#3
|
Member
Registered: Aug 2003
Distribution: Arch Linux
Posts: 140
Original Poster
Rep:
|
Yes.
No, it doesn't work.
|
|
|
06-14-2004, 06:25 PM
|
#4
|
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536
Rep: 
|
Code:
shell$ echo "abc - def ghi---jkl" | sed 's/[ -][ -]*/_/g'
abc_def_ghi_jkl
Last edited by Hko; 06-14-2004 at 06:28 PM.
|
|
|
06-14-2004, 08:47 PM
|
#5
|
Member
Registered: Oct 2002
Location: Bangalore
Distribution: Gentoo Linux
Posts: 96
Rep:
|
If you want to really use tr itself, you're looking for the -s option:
Code:
$ echo "abc___def__g" | tr -s _
abc_def_g
$ echo "abc - def _ g" | tr -s ' -' '__'
abc_def_g
Arvind
|
|
|
06-14-2004, 08:54 PM
|
#6
|
Member
Registered: Oct 2002
Location: Bangalore
Distribution: Gentoo Linux
Posts: 96
Rep:
|
Something I forgot to mention. If you want to use "-" as the first character, escape it.
Code:
$ echo "abc - def _ g" | tr -s '\- ' '__'
$ echo "abc - def ghi---jkl" | tr -s '\- ' '__'
$ echo "abc - def ghi---jkl" | tr -s ' -' '__'
Arvind
|
|
|
06-14-2004, 09:17 PM
|
#7
|
Member
Registered: Aug 2003
Distribution: Arch Linux
Posts: 140
Original Poster
Rep:
|
The -s option is exactly what I needed.
Geez, sometimes I think I go blind. I read the man page over and over and kept missing it.
Thanks for all the responses.
|
|
|
07-05-2004, 12:06 PM
|
#8
|
Member
Registered: Aug 2003
Distribution: Arch Linux
Posts: 140
Original Poster
Rep:
|
I've been busy for quite a while, so that's why this follow-up question was a half month in the making.
As I've said, I'm renaming a bunch of files. I use find to locate those files with the specific attributes I want to change and then kick each found file into
Code:
#!/bin/bash
file_name=$1
real_name=`basename "${file_name}"`
path=`dirname "${file_name}"`
echo ${real_name} | tr -s "-" " " | tr -s " " "_"=cut_name
echo ${path}/${cut_name}
Right now I'm only testing, which is why it just echos out. Also, I haven't tried to test accross directories, so if you notice a glaring flaw with regards to that, please point it out.
My main problem is that I need ${path} directly stuck to ${cut_name}, witht the / in between, but that doesn't happen. I get cut_name echoed out first, and then path.
I can't figure out why, which makes it glaringly obvious that I'm an awful programmer and should just stick to math.
I need help.
Last edited by goofyheadedpunk; 07-05-2004 at 12:21 PM.
|
|
|
07-05-2004, 08:40 PM
|
#9
|
Member
Registered: Oct 2002
Location: Bangalore
Distribution: Gentoo Linux
Posts: 96
Rep:
|
Hi,
You were not very clear. Answer some questions for me and let's see what to do.
Quote:
As I've said, I'm renaming a bunch of files. I use find to locate those files with the specific attributes I want to change and then kick each found file into
|
What attributes, specifically?
Quote:
Code:
echo ${real_name} | tr -s "-" " " | tr -s " " "_"=cut_name
|
This won't work. It should be:
Code:
cutname=`echo ${real_name} | tr -s "- " " _"`
Quote:
My main problem is that I need ${path} directly stuck to ${cut_name}, witht the / in between, but that doesn't happen. I get cut_name echoed out first, and then path.
|
What do you mean, "stuck"? Do you want "$path/$cut_name"? What's the problem?
Give me an example of what you're trying to do? That is, given a filename, with path, what output do you expect from the script?
Arvind
|
|
|
07-06-2004, 02:39 AM
|
#10
|
Member
Registered: May 2004
Posts: 133
Rep:
|
tr " " "_" < translate
translate is the name of the file containing contents
|
|
|
All times are GMT -5. The time now is 02:35 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|