Finding Hidden Characters in a File

By: John Dixon

As well as the characters you can see in a web page (or any other text file), there are a whole host of hidden characters that you can't see. Most of the time you don't need to know where these characters are, which is why they are hidden in the first place. Occasionally though, perhaps when you need to perform some specific processing on one or more files, you need to find and display these characters. In this article I'll show how you can use a simple Perl script to find and display hidden characters in text files.

Here is the script:

Find Hidden Characters

The script initially finds all the files in the 'current' directory (folder) that end with '.txt'. If you wanted to find all the files that ended in '.htm' or '.html', for example, you would just need to change the '.txt' in the line containing the grep command.

The second part of the script makes a backup of each file found, which is always a good idea, and then searches through each file identifying and displaying (inside each file) the hidden characters found.

The characters that this script finds are the 'end of line' character, the 'end of file' character, the 'tab' character, and any control characters. You could of course edit the script to find different hidden characters if you wanted to.

The way the script finds and displays the characters is by using regular expressions. Regular expressions offer a very powerful way of searching for and (often) replacing strings within files. This script is written in Perl, which offers more support for regular expressions than any other language.

The regular expressions are contained in the lines beginning '$line ='. There are four of them in this script.

Running the Script

The script is designed to run on a local machine. Therefore, in order for it to work you will need to have a perl interpreter installed on your computer.

If you named the script 'hidden.pl', for example, you should be able to run it by double-clicking it from within Windows Explorer. Alternatively, you could open a command window, navigate to where the script (and files to be processed) are located, and type 'perl hidden.pl' at the command prompt.

Top Searches on
Programming
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 

» More on Programming
 



Share this article :
Click to see more related articles