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
Last word substitution capri_drm Linux 4 06-10-2008 03:19 AM
word substitution in unix capri_drm Linux 6 05-14-2008 11:36 AM
How to use sed substitution using a $variable for a line containing a word Sangal-Arun Shell Programming and Scripting 4 08-07-2007 04:09 PM
Can a shell script pull the first word (or nth word) off each line of a text file? tricky Shell Programming and Scripting 5 08-17-2006 03:29 AM
word substitution in csh oprestol Shell Programming and Scripting 1 09-15-2005 08:15 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 02-14-2008
Registered User
 

Join Date: Feb 2008
Posts: 2
whole word substitution in SED

I am trying to substitute something with sed and what I want is to substitute a whole
word and not part of a word. ie

sed 's/class/room/g' filename

will substitute both class and classes into room and roomes which is not what i want

Grep for instance can use the -w option or <>
grep -w class
and in that case it will not get classes.

Don't know about sed but wanted to implement something like that
Reply With Quote
Forum Sponsor
  #2  
Old 02-14-2008
joeyg's Avatar
Moderator
 

Join Date: Dec 2007
Location: Home of world champion Boston Celtics
Posts: 965
Question Would a space help?

sed 's/class /room /g' filename
Reply With Quote
  #3  
Old 02-15-2008
Registered User
 

Join Date: Feb 2008
Posts: 2
Quote:
Originally Posted by joeyg View Post
sed 's/class /room /g' filename
Thank for your suggestion
Reply With Quote
  #4  
Old 02-17-2008
Smiling Dragon's Avatar
Disorganised User
 
Join Date: Nov 2007
Location: New Zealand
Posts: 734
Quote:
Originally Posted by joeyg View Post
sed 's/class /room /g' filename
That won't handle lines that end in 'class' or words that end in class (say 'p-class' for example).
I'd suggest looking for words that are proceeded by either a space or start of line, AND end in a space or end of line.
Reply With Quote
  #5  
Old 07-16-2008
Registered User
 

Join Date: Jul 2008
Location: Silicon Forest
Posts: 15
Cool

use:
sed 's/class\>/room/g' filename

the above will only replace lines that end with class and will ignore classes.

Last edited by FunibonE; 07-16-2008 at 01:44 PM.
Reply With Quote
  #6  
Old 07-16-2008
Ikon's Avatar
Registered User
 

Join Date: Jul 2008
Location: Phoenix, Arizona
Posts: 382
Quote:
Originally Posted by FunibonE View Post
use:
sed 's/class$/room/g' filename

the above will only replace lines that end with class and will ignore classes.
But that will replace class and whateverclass.

sed 's/^class$/room/g' filename



EDIT: This is WRONG, see post below!!

Last edited by Ikon; 07-16-2008 at 01:53 PM.
Reply With Quote
  #7  
Old 07-16-2008
Registered User
 

Join Date: Jul 2008
Location: Silicon Forest
Posts: 15
Cool

Then use this:

sed 's/\<class\>/room/g' filename

Should only replace words that start and end with class.
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 03:17 PM.


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