Worst case, manually hack the likely occurrences -
(edit - Thanks, anubhava)
$: echo "man man man man man - manual man is really manners man" |
awk -v replace="man" '{
while ($0~"[[:space:]]"replace"[[:space:]]") {
gsub("[[:space:]]+"replace"[[:space:]]+"," ");
}
gsub("^"replace"[[:space:]]+","");
gsub("[[:space:]]+"replace"$","");
} 1'
- manual is really manners
As anubhava pointed out in the comments, the space-on-either-side check can be tricked by multiple consecutive occurrences of the pattern, so the scan needs to make multiple passes to be sure.
If you care to try sed
it can do boundary markers and doesn't have that problem -
$: echo "man man man man man - manual man is really manners man" | sed -E 's/\<man\>/ /g; s/ +/ /g;'
- manual is really manners
Note that multiples at the front still leaves a leading space -you can always add one more check and clean to the pattern list.
If you have GNU awk
, and maybe don't care about the double spaces -
$: echo "manual man has manners" | awk '{gsub("\\<man\\>","")}1'
manual has manners
Be sure to double-slash the metachars. If you really want to supply the pattern as a variable on the command line, especially with GNU awk
using border markers, watch out for double-parsing. You will still need double-backslash quoting if you use single-ticks, or four if you double-quote...
$: echo "manual man has manners" | awk -v replace='\\<man\\>' '{gsub(replace,"")}1'
manual has manners
$: echo "manual man has manners" | awk -v replace="\\\\<man\\\\>" '{gsub(replace,"")}1'
manual has manners