The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Perl: Search for string on line then search and replace text Crypto Shell Programming and Scripting 4 01-04-2008 07:24 AM
How to replace variable inside the variable mani_um Shell Programming and Scripting 31 08-09-2007 07:56 PM
Replace variable with a user defined variable ce124 Shell Programming and Scripting 1 04-15-2007 11:56 AM
sed search and replace d__browne UNIX for Dummies Questions & Answers 7 04-26-2006 06:46 AM
how to search for a str and replace it.. sekar sundaram Shell Programming and Scripting 6 12-29-2005 03:11 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 09-27-2005
Registered User
 

Join Date: May 2005
Posts: 54
search & replace in variable

Can I use search & replace in any variable?

Suppose I have one variable named var1 which holds value "abcabc" I need to search 'a' in var1 and want to replace with 'x' like 'xbcxbc'. Is it possible? Can you provide me an example?

Malay
Reply With Quote
Forum Sponsor
  #2  
Old 09-27-2005
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,699
If you are using sh read the man pages of sh

Under the Parameter Expansion section

Code:
       ${parameter/pattern/string}
       ${parameter//pattern/string}
              The pattern is expanded to produce a pattern just as in pathname
              expansion.  Parameter is expanded and the longest match of  pat-
              tern  against  its  value is replaced with string.  In the first
              form, only the first match is replaced.  The second form  causes
              all  matches  of pattern to be replaced with string.  If pattern
              begins with #, it must match at the beginning  of  the  expanded
              value  of parameter.  If pattern begins with %, it must match at
              the end of the expanded value of parameter.  If string is  null,
              matches  of  pattern are deleted and the / following pattern may
              be omitted.  If parameter is @ or *, the substitution  operation
              is  applied to each positional parameter in turn, and the expan-
              sion is the resultant list.  If parameter is an  array  variable
              subscripted  with  @ or *, the substitution operation is applied
              to each member of the array in turn, and the  expansion  is  the
              resultant list.
That would translate to a construct like this
Code:
${var1//a/x}
I couldnt find any similiar shell builtin for ksh.

There are a bunch of solutions for something similiar in this thread Find and replace the value in a variable

vino
Reply With Quote
  #3  
Old 10-04-2005
Registered User
 

Join Date: Sep 2005
Location: Chennai,India
Posts: 12
Why can't you echo the variable to sed and search and replace

echo "abcabc" | sed 's/a/x/g'
Reply With Quote
  #4  
Old 10-04-2005
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,699
Quote:
Originally Posted by monsanbu
Why can't you echo the variable to sed and search and replace

echo "abcabc" | sed 's/a/x/g'
There is nothing wrong in doing that.

But why invoke an external program (sed, in this case) when the shell allows you to do the same thing.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 06:03 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0