LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 05-31-2007, 03:23 AM   #1
baks
Member
 
Registered: Feb 2007
Posts: 41

Rep: Reputation: 15
A tr command problem


Hi, I have a problem with the folowing code, the tr command doesnt seem to want to replace the chosen characters with a character that i have stored in a varable. Can anyone help ?

#!/bin/bash

STRING=/a/s/d/f/
CHAR=|
OUTPUT=`echo $STRING | tr '/' '$CHAR'`

echo $OUTPUT


Thank You
 
Old 05-31-2007, 03:32 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

You need to use double quotes, otherwise the variable isn't expanded.

OUTPUT=`echo $STRING | tr '/' "$CHAR"`

In short: All between single quotes is not expanded, all between double quotes is.

Hope this clears things up.
 
Old 05-31-2007, 04:04 AM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by baks
Hi, I have a problem with the folowing code, the tr command doesnt seem to want to replace the chosen characters with a character that i have stored in a varable. Can anyone help ?

#!/bin/bash

STRING=/a/s/d/f/
CHAR=|
OUTPUT=`echo $STRING | tr '/' '$CHAR'`

echo $OUTPUT


Thank You
"|" is interpreted as a pipe in shell, if you did not enclose in quotes "".
correct way should be
Code:
STRING=/a/s/d/f/
CHAR="|"
OUTPUT=`echo $STRING | tr '/' "$CHAR"`
echo $OUTPUT

Last edited by ghostdog74; 05-31-2007 at 04:06 AM.
 
Old 05-31-2007, 04:07 AM   #4
baks
Member
 
Registered: Feb 2007
Posts: 41

Original Poster
Rep: Reputation: 15
Thank you, I have the following code now but I dont think its replacing each instance of "/" with the CHAR variable:

#!/bin/bash

STRING=/a/s/d/f/
CHA="......"
OUTPUT=`echo $STRING | tr '/' "$CHA"`

echo $OUTPUT

It should replace each forward slash with 6 dots but I dont think it is.
 
Old 05-31-2007, 04:08 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

@ghostdog74: I overlooked that For me it's common practise to use (double) quotes around all my variable declarations.

Thanks for pointing this out.
 
Old 05-31-2007, 04:13 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

@baks: To my knowledge tr doesn't substitute one character to multiple characters, you need something like sed to do that.

Your second example should echo this: .a.s.d.f.
 
Old 05-31-2007, 04:22 AM   #7
baks
Member
 
Registered: Feb 2007
Posts: 41

Original Poster
Rep: Reputation: 15
Thank you for your replies.
 
Old 05-31-2007, 05:08 AM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
@drunna:no problem
@baks:
Code:
echo $STRING|sed 's/\//\.\.\.\./g'
 
Old 05-31-2007, 05:26 AM   #9
baks
Member
 
Registered: Feb 2007
Posts: 41

Original Poster
Rep: Reputation: 15
Thank you but I cannot used sed or awk yet because I havent reached that part of the course. but thank you.
 
Old 05-31-2007, 06:00 AM   #10
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by baks
Thank you but I cannot used sed or awk yet because I havent reached that part of the course. but thank you.
okay, If you are learning bash, you can use bash's own string substitution
Code:
# echo ${STRING//\//....}
....a....b....c....d
for info, read the bash man page under PARAMETER EXPANSION
 
Old 05-31-2007, 07:03 AM   #11
baks
Member
 
Registered: Feb 2007
Posts: 41

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by ghostdog74
okay, If you are learning bash, you can use bash's own string substitution
Code:
# echo ${STRING//\//....}
....a....b....c....d
for info, read the bash man page under PARAMETER EXPANSION

Thank you I actually forgot that I could use substrings.
 
  


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
at Command Problem BBPS Linux - Software 2 09-06-2006 05:23 AM
problem with dd command ? MaaSTaaR Linux - General 5 06-25-2006 07:50 AM
problem in cp command ksriram03 Linux - Newbie 2 03-15-2006 11:12 AM
command problem vickr1z Linux - Newbie 3 09-06-2004 12:43 PM
problem with tar command tiang_ono Linux - Newbie 3 06-17-2004 02:36 PM

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

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