![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
sed 's/class /room /g' filename
|
|
#3
|
|||
|
|||
|
#4
|
||||
|
||||
|
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
|
|||
|
|||
|
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. |
|
#6
|
||||
|
||||
|
Quote:
sed 's/^class$/room/g' filename EDIT: This is WRONG, see post below!! Last edited by Ikon; 07-16-2008 at 01:53 PM. |
|
#7
|
|||
|
|||
|
Then use this:
sed 's/\<class\>/room/g' filename Should only replace words that start and end with class. |
|||
| Google The UNIX and Linux Forums |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|