I want to replace special characters (regex \W) with _ (underscore) But I don't want to replace whitespace with underscore Also replace multiple consecutive special characters with single underscore
Example
String: The/Sun is red@
Output: The_Sun is red_
String: .//hack Moon
Output: _hack Moon
I have tried echo 'string' | sed 's/\W/_/g'
But it's not accurate
sed
approach:
s="The/Sun is red@ .//hack Moon"
sed -E 's/[^[:alnum:][:space:]]+/_/g' <<<"$s"
The_Sun is red_ _hack Moon
[^[:alnum:][:space:]]+
- match any character sequence except alphanumeric and whitespaceUse tr
for that:
echo "The/Sun is red@" | tr -s -c [:alnum:][:blank:] _
[:alnum:][:blank:]
represents alphanumeric characters and whitespace, -c
means the opposite of that.
Added: -s
to squeeze duplicate underscores into one.
Just with bash parameter expansion, similar pattern to other answers:
shopt -s extglob
for str in "The/Sun is red@" ".//hack Moon"; do
echo "${str//+([^[:alnum:][:blank:]])/_}"
# .........^^........................^ replace all
# ...........^^.....................^ one or more
# .............^^^^^^^^^^^^^^^^^^^^^ non-alnum, non-space character
done
The_Sun is red_
_hack Moon
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With