The suggested solutions may fail with specific version of sed, e.g. GNU sed 4.2.1.
Using tr
:
tr -cd '[:print:]' < yourfile.txt
This will remove any characters not in [\x20-\x7e]
.
If you want to keep e.g. line feeds, just add \n
:
tr -cd '[:print:]\n' < yourfile.txt
If you really want to keep all ASCII characters (even the control codes):
tr -cd '[:print:][:cntrl:]' < yourfile.txt
This will remove any characters not in [\x00-\x7f]
.