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
Last word substitution capri_drm Linux 4 06-10-2008 07:19 AM
word substitution in unix capri_drm Linux 6 05-14-2008 03:36 PM
How to use sed substitution using a $variable for a line containing a word Sangal-Arun Shell Programming and Scripting 4 08-07-2007 08: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 07:29 AM
word substitution in csh oprestol Shell Programming and Scripting 1 09-16-2005 12:15 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 02-14-2008
gikay01 gikay01 is offline
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
  #2 (permalink)  
Old 02-14-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Question Would a space help?

sed 's/class /room /g' filename
  #3 (permalink)  
Old 02-15-2008
gikay01 gikay01 is offline
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
  #4 (permalink)  
Old 02-17-2008
Smiling Dragon's Avatar
Smiling Dragon Smiling Dragon is offline Forum Advisor  
Disorganised User
  
 

Join Date: Nov 2007
Location: New Zealand
Posts: 922
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.
  #5 (permalink)  
Old 07-16-2008
FunibonE FunibonE is offline
Registered User
  
 

Join Date: Jul 2008
Location: Silicon Forest
Posts: 16
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 05:44 PM..
  #6 (permalink)  
Old 07-16-2008
Ikon's Avatar
Ikon Ikon is offline Forum Advisor  
Registered User
  
 

Join Date: Jul 2008
Location: Phoenix, Arizona
Posts: 669
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 05:53 PM..
  #7 (permalink)  
Old 07-16-2008
FunibonE FunibonE is offline
Registered User
  
 

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

Then use this:

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

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