User12345
User12345

Reputation: 5480

Multiple characters replace in Linux using sed

I have a variable in Linux like below. This is not a valid variable I just created to test how to replace characters with some new characters.

table=123~!@#$%^&*()+|}{:"?><-=[]\;',./

Want to replace all the special characters in this table variable like below

table1=123_____________________________

How can I do that in Linux?

Upvotes: 0

Views: 54

Answers (2)

karakfa
karakfa

Reputation: 67467

same with tr

tr -c '[:alnum:]' _  <file

Upvotes: 1

JNevill
JNevill

Reputation: 50019

Pipe it to sed and replace anything that is not ^ alphanumeric [:alnum:] with an underscore _.

sed 's/[^[:alnum:]]/_/g'

In your code, this would look something like:

table1=$(echo table | sed 's/[^[:alnum:]]/_/g')

Upvotes: 1

Related Questions