使用sed删除非字母数字字符 [英] Removing non-alphanumeric characters with sed

查看:87
本文介绍了使用sed删除非字母数字字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试验证一些输入以删除一组字符.仅允许使用字母数字字符加号,句点,下划线和连字符.我已经在 http://gskinner处测试过正则表达式 [^ \ w .-] .com/RegExr/,它与我要删除的内容匹配,所以我不确定为什么 sed 返回相反的内容.我想念什么?

I am trying to validate some inputs to remove a set of characters. Only alphanumeric characters plus, period, underscore, hyphen are allowed. I've tested the regex expression [^\w.-] here http://gskinner.com/RegExr/ and it matches what I want removed so I not sure why sed is returning the opposite. What am I missing?

我的最终目标是输入"10.41.89.50" 并获得"10.41.89.50 ".

My end goal is to input "Â10.41.89.50 " and get "10.41.89.50".

我尝试过:

echo"10.41.89.50" |sed s/[^ \ w .-]//g 返回 ......

echo"10.41.89.50" |sed s/[\ w .-]//g echoÂ10.41.89.50" |sed s/[\ w ^ .-]//g 返回 10418950

我尝试在此处找到答案用sed跳过/删除非ascii字符,但没有删除任何内容.

I attempted the answer found here Skip/remove non-ascii character with sed but nothing was removed.

推荐答案

-c (补码)标志可能是一个选项

tr's -c (complement) flag may be an option

echo "Â10.41.89.50-._ " | tr -cd '[:alnum:]._-'

这篇关于使用sed删除非字母数字字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆