LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop
User Name
Password
Linux - Desktop This forum is for the discussion of all Linux Software used in a desktop context.

Notices


Reply
  Search this Thread
Old 01-27-2016, 07:44 AM   #1
santhosh.ananthula
LQ Newbie
 
Registered: Jan 2016
Posts: 1

Rep: Reputation: Disabled
how replace exact word using sed


I am having a requirement to replace a word xyz with zxy in a file containing a.xyz and xyz

when i am using the below command its replacing in both the places which is not the exact requirement

sed 's/xyz/zxy/g' names.txt

also tried
sed 's/ xyz/ zxy/g' names.txt it works but it seems to be no exact solution.

Please advice

Thanks
 
Old 01-27-2016, 08:16 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,039

Rep: Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347
use sed -r and check for word boundary
 
Old 01-27-2016, 12:44 PM   #3
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
it's one of those typical stackoverflow questions.
i don't understand why people don't search before asking... if this goes on, the internet will become totally unusable soon.
 
1 members found this post helpful.
Old 01-27-2016, 02:39 PM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,753

Rep: Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983
Quote:
Originally Posted by ondoho View Post
it's one of those typical stackoverflow questions.
i don't understand why people don't search before asking... if this goes on, the internet will become totally unusable soon.
Yes...I believe it's been brought up here several times. For this very reason.

I have yet to understand why folks can't apply some effort of their own, rather than looking for someone to give them the exact command...it would never occur to me *NOT* to try to solve the problem on my own. Even more entertaining, because this site is typically found through a Google search...which could also have provided an answer.
 
Old 01-27-2016, 05:10 PM   #5
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,784

Rep: Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214
This particular case is a bit different since "." is a non-word character, so the string "a.xyz" would still satisfy a full-word match for "xyz". Writing a regex with a different notion of word boundary that still covers the case where the "word" is at the beginning or end of the line is not trivial, nor is concocting a Google search for an answer to that problem.
 
  


Reply



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
sed: search for string, then match and replace the next occurance of a word nbritton Programming 4 01-21-2015 12:40 PM
[SOLVED] search and replace particular word with sed using variable roopakl Linux - Newbie 2 10-01-2012 11:08 AM
Trying to use sed to replace a number that is in a string with a word simpsonc Linux - Newbie 3 09-12-2010 07:55 PM
[SOLVED] Remove exact matching word using sed kushalkoolwal Programming 2 08-20-2009 05:15 PM
sed 1st word to replace all instances of ( donnied Programming 5 08-21-2008 06:43 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop

All times are GMT -5. The time now is 11:04 PM.

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