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 and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
String replacement mnmonu Shell Programming and Scripting 6 03-24-2009 07:22 AM
String replacement reddybs Shell Programming and Scripting 7 01-22-2009 08:50 AM
Replacement of string c0384 Shell Programming and Scripting 1 09-17-2008 11:06 AM
AWK String replacement subin_bala Shell Programming and Scripting 5 07-16-2008 03:29 AM
sed problem - replacement string should be same length as matching string. amangeles Shell Programming and Scripting 4 01-11-2006 06:11 AM

Reply
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 07-07-2009
sngk sngk is offline
Registered User
  
 

Join Date: Jul 2009
Posts: 3
String replacement using sed

I need to search and replace a particular string in a file. Only the exact match of the string should be replaced.
eg: File contents : abc abcd abcdef
--> Replace only 'abc' with 'xyz', but it should not replace abcd with xyzd.

So the o/p should be: xyz abcd abcdef.

How can this be done? I tried sed 's/\<abc\>/xyz/g' , but it dint work.

Any help would be appreciated.

Thanks in advance!
  #2 (permalink)  
Old 07-07-2009
reddybs reddybs is offline
Registered User
  
 

Join Date: Jan 2009
Location: Sriharikota-AP-India
Posts: 53

Code:
echo "abc abcd abcdef" | sed -e 's/^abc/xyz/g'

  #3 (permalink)  
Old 07-07-2009
sngk sngk is offline
Registered User
  
 

Join Date: Jul 2009
Posts: 3
I tried it. It dint work. Let me be more specific. The string need not be at the beginning of the line:

i/p: abcd abc abcdef
o/p: abcd xyz abcdef

Thanks!
  #4 (permalink)  
Old 07-08-2009
reddybs reddybs is offline
Registered User
  
 

Join Date: Jan 2009
Location: Sriharikota-AP-India
Posts: 53
Quote:
Originally Posted by sngk View Post
I tried it. It dint work. Let me be more specific. The string need not be at the beginning of the line:

i/p: abcd abc abcdef
o/p: abcd xyz abcdef

Thanks!

Code:
echo "string" | sed -e 's/ abc / xyz /g' | sed -e 's/^abc /xyz /g' -e 's/ abc$/ xyz/g'

Hello i am not very sure how to do this using sed options , there could far better way though.
In the above code i just took care about
1. abc at starting of the string
2. abc at end of the string
3. abc in between the string
  #5 (permalink)  
Old 07-09-2009
zaxxon's Avatar
zaxxon zaxxon is online now Forum Staff  
Moderator
  
 

Join Date: Sep 2007
Location: Germany
Posts: 2,313
Your first example should work using the <> to define a "word" but only with GNU sed or GNU awk and similar enhanced SEDs and AWKs. There are SEDs and AWKs out there that have a not as broad understanding of regular expressions like the modern GNU versions.
On most Linux boxes your line should work flawless.

Afaik you will have to emulate the <word> RegExp behaviour like reddybs posted. Only thing I see is it could be written so sed is called only once, but I would go with that too, example:


Code:
$> echo "abc abcd abcde abc abcd xabc dabc"| sed '
> s/ abc / ooo /g
> s/^abc /ooo /g
> s/ abc$/ ooo/g
> '
ooo abcd abcde ooo abcd xabc dabc


Last edited by zaxxon; 07-09-2009 at 03:00 AM..
  #6 (permalink)  
Old 07-12-2009
sngk sngk is offline
Registered User
  
 

Join Date: Jul 2009
Posts: 3
Thanks zaxxon and reddybs. That worked!
Reply

Bookmarks

Tags
exact, replace, seach

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 05:25 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