Filter away non-printable ASCII characters on Linux?

Sometimes the commands’ output have some non-printable ASCII characters. How to filter away these non-printable ASCII characters on Linux?

You can consider the problem as “how to leave only printable ascii characters?”.

The printable characters in ASCII table are:

Octal 011: Tab
Octal 012: Line Feed
Octal 015: Carriage Return
Octal 040 to 176: printable codes

Then, you can use tr to get only these characters:

cat input.txt | tr -cd '11121540-176' > output.txt

Eric Ma

Eric is a systems guy. Eric is interested in building high-performance and scalable distributed systems and related technologies. The views or opinions expressed here are solely Eric's own and do not necessarily represent those of any third parties.

Leave a Reply

Your email address will not be published. Required fields are marked *