Perl – Detect non-ASCII characters in file

Reading Time: 1 minute

Solution

$ perl -ne 'if (/[^[:ascii:]]/) { print $. . ": " . $_ }' filepath

Explanation

  • perl – the Perl language interpreter.
  • -n – assume while (<>) { ... } loop around program i.e. apply command to every line in the file.
  • -e – one line of program (several -e‘s allowed, omit programfile).

Running sample

Let’s take words.txt as input file. See the content below:

salut
România
Cluj
Mureș
măgăruș
mașină
oameni

After running:

$ perl -ne 'if (/[^[:ascii:]]/) { print $. . ": " . $_ }' words.txt

The output will be:

2: România
4: Mureș
5: măgăruș
6: mașină

So we can see all the lines (with the corresponding line number as prefix) in words.txt that contain non-ASCII character(s).

We transform challenges into digital experiences

Get in touch to let us know what you’re looking for. Our policy includes 14 days risk-free!

Free project consultation