Check if a file contains special (non-ascii) characters

15 Apr 2010

To check if a file contains any special (non “standard” ascii) characters, use Perl and POSIX character classes. Depending on how permissive you want to be use one of:

  • allow only whitespace and standard ascii print characters (i.e. [\x20-\x7E])

    $ perl -nwe 'print if /[^[:space:][:print:]]/' file.txt
  • also allow control characters (i.e. [\x00-\x7F])

    $ perl -nwe 'print if /[^[:ascii:]]/' file.txt

This recipe has been packed as a script on github: gremlins.