简体   繁体   中英

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.

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?

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/

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM