jlp jlp - 1 year ago 105
Bash Question

How to remove special characters from strings but keep underscores in shell script

I have a string that is something like "info_A!__B????????C_*". I wan to remove the special characters from it but keep underscores and letters. I tried with [:word:] (ASCII letters and _) character set, but it says "invalid character set". any idea how to handle this ? Thanks.

if [ -z `echo $text | tr -dc "[:word:]"` ]

Answer Source

Using bash parameter expansion:

$ var='info_A!__B????????C_*'

$ echo "${var//[^[:alnum:]_]/}"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download