Technology
Replacing characters in multiple file names
by Zubair 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/bashfor file in tcpTrace*.shdomv $file pingTrace${file#tcpTrace}done
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.