Rss Feed
Tweeter button
Facebook button
Digg button
Flickr button
Youtube button
Cryptic Writings …

Replace special characters in a text file

by 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.

I had a file of traceroute outputs and my requirement was to convert all the periods (.) to colons (:) [i know the requirement is weird as well, but i'll explain later in another post].

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:

tr -s ‘[.]‘ ‘:’ < inputFilename > outputfilename

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.

Strings with control characters can also be replaced by using POSIX character classes such as [:cntrl:], [:blank:] and so on.

More information on this powerful command is at the man page.

No comments for this entry yet...

Comments are closed.

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...

Archives

All entries, chronologically...