开发者

Bash String Remove Specific Characters

开发者 https://www.devze.com 2023-03-04 08:53 出处:网络
I\'m almost done开发者_高级运维 with using bash, but the last thing I need to do is to do a regex replace on a string in bash. I have the PHP equivalent here

I'm almost done开发者_高级运维 with using bash, but the last thing I need to do is to do a regex replace on a string in bash. I have the PHP equivalent here

preg_replace("/[^a-z0-9\.]/", '',$theme_name);

I need to do this exact same thing in bash, which is replacing every non alphanumeric character or dots (.) with nothing. Thanks in advance!


Try the following:

echo "some string" | tr -d -c ".[:alnum:]"
  • tr "translates" characters in the string
  • -d deletes instead of translates
  • -c means complement
  • [:alnum:] means "alpha numerics".


Try this

echo "hello world" | sed  -e 's/[reg_ex]//g' 
0

精彩评论

暂无评论...
验证码 换一张
取 消