[英]Bash: How to use sed to remove all characters except letters and numbers?
First off, I'm still learning about regular expression, I have googled about this but still doesn't work. 首先,我仍在学习正则表达式,已经在Google上进行了搜索,但仍然无法正常工作。
How do I remove all characters except letters and numbers in a variable with sed
? 如何使用
sed
删除变量中除字母和数字以外的所有字符? For example I have this text file: 例如,我有这个文本文件:
MytextOnly !@#!text@@32423#@$text#%$#text%#t23432ext$32342%^-_+-=-_++_;:"'][}}{|\/
How do I show only letters and numbers? 如何仅显示字母和数字?
You can use: 您可以使用:
sed 's/[^[:alnum:]]\+//g' file
MytextOnlytext32423texttextt23432ext32342
[^[:alnum:]]
property will find all non-alphanumerical characters. [^[:alnum:]]
属性将查找所有非字母数字字符。
EDIT: Based on comments below: 编辑:基于以下评论:
sed 's~[^[:alnum:]/]\+~~g' file
MytextOnlytext32423texttextt23432ext32342/
Using grep
使用
grep
grep -o '[[:alnum:]]' file
agree, no the perfect output, but everything is there 同意,没有完美的输出,但是一切都在那里
Using tr
使用
tr
$ tr -d -c '[:alnum:]' < file
MytextOnlytext32423texttextt23432ext32342
If you also want to keep forward slashes: 如果您还想保持正斜杠:
$ tr -d -c '[:alnum:]/' < file
MytextOnlytext32423texttextt23432ext32342/
For a python solution, see https://stackoverflow.com/a/5843560/297323 有关python解决方案,请参见https://stackoverflow.com/a/5843560/297323
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.