Bash: How to use sed to remove all characters except letters and numbers?

bash, grep, regex, sed, shell

Solution

You can use:

sed 's/[^[:alnum:]]\+//g' file
MytextOnlytext32423texttextt23432ext32342

`[^[:alnum:]]` property will find all non-alphanumerical characters.

EDIT: Based on comments below:

sed 's~[^[:alnum:]/]\+~~g' file
MytextOnlytext32423texttextt23432ext32342/

Problem

First off, I'm still learning about regular expression, I have googled about this but still doesn't work. How do I remove all characters except letters and numbers in a variable with `sed`? For example I have this text file: ``` MytextOnly !@#!text@@32423#@$text#%$#text%#t23432ext$32342%^-_+-=-_++_;:"'][}}{|\/ ``` How do I show only letters and numbers?

Original source

Related problems