1

Is it possible to use sed to replace words, but avoid replacing words that begin with the letters?

For example if I replace man with woman, but women becomes wowoman is it possible to skip woman with sed? Or would you pipe in sed again to remove wo from the beginning of the letters ?

sed -i 's/man/woman/g' /usr/share/dict/words | sed 's^wo//'g /usr/share/dict/words 

which doesn't seem to be working just add another wo on the start.

After reading more on piping grep into sed i tried the following command

sudo sed -i 's/man/woman | grep -v 'woman'/g' words1 

what i am looking to try out is replace all letter that have man with woman

2

1 Answer 1

1

Only replace "man" if it's a single word:

sed -i 's/\bman\b/woman/g' /usr/share/dict/words 
2
  • after reading on passing grep into sed i tried this command 'sudo sed -i 's/man/woman | grep -v 'woman'/g' words1' the output of the file is wowoman | grep -v woman
    – David
    CommentedSep 22, 2016 at 20:32
  • Please watch your command carefully. sed 's/xxx/yyy/g' is replacing "xxx" by "yyy". So your command will replace "man" by "wowoman | grep -v woman"
    – rudimeier
    CommentedSep 22, 2016 at 20:53

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.