LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 06-14-2004, 05:28 PM   #1
goofyheadedpunk
Member
 
Registered: Aug 2003
Distribution: Arch Linux
Posts: 140

Rep: Reputation: 15
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?
 
Old 06-14-2004, 06:07 PM   #2
rkef
Member
 
Registered: Mar 2004
Location: bursa
Posts: 110

Rep: Reputation: 15
Have you tried the -d option to 'tr'?

Also, try 'sed'... it's fun.
 
Old 06-14-2004, 06:11 PM   #3
goofyheadedpunk
Member
 
Registered: Aug 2003
Distribution: Arch Linux
Posts: 140

Original Poster
Rep: Reputation: 15
Yes.

No, it doesn't work.
 
Old 06-14-2004, 06:25 PM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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.
 
Old 06-14-2004, 08:47 PM   #5
arvind_sv
Member
 
Registered: Oct 2002
Location: Bangalore
Distribution: Gentoo Linux
Posts: 96

Rep: Reputation: 16
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
 
Old 06-14-2004, 08:54 PM   #6
arvind_sv
Member
 
Registered: Oct 2002
Location: Bangalore
Distribution: Gentoo Linux
Posts: 96

Rep: Reputation: 16
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
 
Old 06-14-2004, 09:17 PM   #7
goofyheadedpunk
Member
 
Registered: Aug 2003
Distribution: Arch Linux
Posts: 140

Original Poster
Rep: Reputation: 15
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.
 
Old 07-05-2004, 12:06 PM   #8
goofyheadedpunk
Member
 
Registered: Aug 2003
Distribution: Arch Linux
Posts: 140

Original Poster
Rep: Reputation: 15
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.
 
Old 07-05-2004, 08:40 PM   #9
arvind_sv
Member
 
Registered: Oct 2002
Location: Bangalore
Distribution: Gentoo Linux
Posts: 96

Rep: Reputation: 16
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
 
Old 07-06-2004, 02:39 AM   #10
suchi_s
Member
 
Registered: May 2004
Posts: 133

Rep: Reputation: 15
tr " " "_" < translate

translate is the name of the file containing contents
 
  


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
shell script: File Manipulation simon_qwl Programming 2 10-29-2005 03:40 PM
file manipulation with c C.Aymen Programming 2 09-01-2005 12:48 PM
C File Manipulation in Linux drigz Programming 5 10-01-2004 07:28 AM
File manipulation of NTFS from RHat 9 javadudd Linux - Newbie 3 04-01-2004 08:21 PM
Some basic File Manipulation commands sureshp1980 Linux - General 2 09-24-2003 08:36 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:35 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