Remove all non-ascii characters with perl

Sometimes when creating one-liners, the source contains non-ASCII characters that make parsing harder. To strip them all out, use Perl:

cat dirty-source.txt | perl -pe 's/[^[:ascii:]]//g' > clean-output.txt

This replaces every character that is not in the ASCII set with nothing, leaving only clean ASCII text. The same approach works for Windows line endings or other invisible characters by adjusting the character class.

If you prefer a sed alternative for the same task:

sed 's/[^[:print:]]//g' dirty-source.txt > clean-output.txt

Hope it helps!

Leave a Reply

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