Replace special characters in a text file
by Zubair on May.01, 2010, under Technology, Unix programming
I recently ran into a problem which is rather simple in a small dataset but get magnified exponentially when the magnitude becomes large. It was nothing but replacing a particular character with another in a text file.
So what should i do – simple, open the text file in a text editor, select the find and replace option and go ahead and do the needful. Well, the simple matter was that the text file in itself was about 2 Gigs in size, with millions of IP addresses in it (each IP address has at-least three periods).
With no text editor capable of opening such a file, i looked to the Unix command line and found ‘tr’. The man page describes it as: Translate, squeeze, and/or delete characters
Replacing the characters was made possible by:
The above command with the squeeze-repeats (-s) option takes all strings with the period [.] and replaces them with the ‘:’. The ‘<’ indirection operator directs every character from the input file to tr and then the replaced characters are redirected to the output file.
More information on this powerful command is at the man page.