The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
.
google unix.com



UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Array help (variable substitution). dsimpg1 Shell Programming and Scripting 3 04-11-2007 04:38 PM
KSH variable substitution tipsy Shell Programming and Scripting 5 08-14-2006 05:07 PM
Variable Substitution garak Shell Programming and Scripting 2 03-16-2006 08:55 PM
awk variable substitution apalex UNIX for Dummies Questions & Answers 1 09-10-2004 06:02 PM
variable substitution ck-18 UNIX for Dummies Questions & Answers 1 05-16-2003 08:44 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-17-2001
spragueg spragueg is offline
Registered User
  
 

Join Date: Oct 2001
Location: Framingham, MA USA
Posts: 4
Substitution in a variable

Hey All,

I'm trying to clean up a variable using sed but It dosn't seem to work. I'm trying to find all the spaces and replace them with "\ " (a slash and a space). For Example "Hello World" should become "Hello\ World". But it does nothing. If I put it directly into the command line it works fine. Here's my code...


#!/bin/sh


var_name="Hello World"
echo "var_name is $var_name"
var_name=`echo $var_name |sed 's/ /\\ /g'`
echo "Cat_name has changed to $Cat_name"

Thanks,
G
  #2 (permalink)  
Old 10-18-2001
LivinFree's Avatar
LivinFree LivinFree is offline Forum Advisor  
Goober Extraordinaire
  
 

Join Date: Jul 2001
Location: Portland, OR, USA
Posts: 1,584
First thing I noticed, was that you would echo out $Cat_name. You didn't define Cat_name. The other thing I did was to add another \ to the sed statement, along with the -e option... Hope that helps...

Try this:
Code:
#!/bin/sh

var_name="Hello World"
echo "var_name is $var_name"
var_name=`echo $var_name | sed -e 's/ /\\\ /g'`
echo "var_name has changed to $var_name"
  #3 (permalink)  
Old 10-18-2001
krishna krishna is offline
Registered User
  
 

Join Date: Aug 2001
Posts: 60
Re: Substitution in a variable


Hi,

just change as below :
var_name=`echo $var_name|sed 's/ / \//g'`

or for backslash :
var_name=`echo $var_name|sed 's/ / \\\/g'`

krishna

Last edited by krishna; 10-18-2001 at 12:30 AM..
  #4 (permalink)  
Old 10-18-2001
spragueg spragueg is offline
Registered User
  
 

Join Date: Oct 2001
Location: Framingham, MA USA
Posts: 4
Sweet it works! Thanks!
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:35 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0