Skip to main content

Sed Remove All But Letters and Digits

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

Given

'file' with this content:

MytextOnly !@#!text@@32423#@$text#%$#text%#t23432ext$32342%^-_+-=-_++_;:"'][}}{|\/

Solved Initial

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

Update

Check my updated answer for allowing forward slash. @Tiago: Quantifier + is used for efficiency so that less # of replacements happen.

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

Source