To replace whole words with sed
, one does:
$ echo "bar embarassment" | sed "s/\bbar\b/no bar/g"
no bar embarassment
This is taken from another stackoverflow question. Follow on question, how do I change the definition of a word?
From linuxtopia:
GNU sed, ssed, sed16, sed15 and sedmod use certain symbols to define the boundary between a "word character" and a nonword character. A word character fits the regex "[A-Za-z0-9_]".
How does one include e.g. "-"? Or in my particular case, I want to rename variables in a R
codebase, where they are littered with "." (it is often used instead of "_" in variable names, see for example google's R styleguide), so I would like to include "." in the definition of a word.
EDIT:
To be extra clear, say I want to change current.my.date <- my.date + today
into current.my.date <- any.date + date
, what is the sed
command?
e.g. fix this command
echo "current.my.date <- my.date + today" | sed "s/\bmy.date\b/any.date/g"
current.any.date <- any.date + today
Because in its current form it also changes current.my.date