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

Technology

Replacing characters in multiple file names

by on May.05, 2010, under Technology, Unix programming

I had a large set of shell scripts each of which i needed to run on separate planetLab nodes for which I included the name of the vantage point in every file. I had a pre-string to every file name which i then needed to replace. Unix/Linux makes this things so much more easier in this case.

Original name of file(s): tcpTrace-[vantageName-1].sh

I needed to change the inital string name from tcpTrace to pingTrace: Final name of file(s): pingTrace-[vantageName-1].sh

Since there was over 250 such files in my directory – a manual rename would be really tedious. I wrote the following rather simple shell script to do the job:

#!/bin/bash
for file in tcpTrace*.sh
do
mv $file pingTrace${file#tcpTrace}
done

I use a simple but rather obscure string operator here, the ‘#’. Generally, we know that anything following the # in a shell script is ignored as a comment (except the first line of course). However, the ‘#’ sign can be used within a shell variable to carry out a delete operation. ‘#’ here means that to ‘delete from the left to the first case of what follows’. Further info is here.
In my case, i first add the string ‘pingTrace’ to the start of the file, then take the filename (stored in $file) and delete from that variable, starting from the left till the first case of when ‘tcpTrace’ is encountered. Simple and really effective fix.
Comments Off more...

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.

Comments Off more...

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