Bash: How to use sed to remove all characters except letters and numbers?
11,992
Solution 1
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/
Solution 2
Using grep
grep -o '[[:alnum:]]' file
agree, no the perfect output, but everything is there
Using 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
Author by
Lin
Updated on June 04, 2022Comments
-
Lin almost 2 years
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?