简体   繁体   English

在Linux中使用sed替换多个字符

[英]Multiple characters replace in Linux using sed

I have a variable in Linux like below. 我在Linux有一个变量,如下所示。 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? 如何在Linux中做到这一点?

Pipe it to sed and replace anything that is not ^ alphanumeric [:alnum:] with an underscore _ . 用管道传输到sed并用下划线 _替换不是 ^ 字母数字 [:alnum:]的任何内容。

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

In your code, this would look something like: 在您的代码中,这看起来像:

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

tr相同

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM