[oak perl] Oakland Digest, Vol 56, Issue 3
Quinn Weaver
quinn at fairpath.com
Mon Feb 18 13:14:47 PST 2008
On Mon, Feb 18, 2008 at 11:19:25AM -0800, Tabatchnick, Justin wrote:
> Hi;
>
> Need some help , I have to replace the tab delineator with a different
> character in a tab delineated file and was hoping someone might be able
> to suggest a method and give an example in Perl.
Hi, Justin,
This is a classic Perl problem--so much so that Perl provides several shortcuts
to make solving it easier.
The basic solution is this:
perl -wpi.bak -e 's/\t/q/g' file1 file2 file3 # et cetera...
In this code, replace the q with the character you want. If it's an unprintable
character, you can represent it in hex: \xFF, for example. If you decide
you want to replace each tab with two characters, you can do that too;
for instance, you could change the code say qu instead of q.
This assumes you are typing the command from a Unix shell. If you use
Windows, I _think_ you need double quotes instead of single quotes. (I'm
not sure about that, though; I don't have a Windows box on which to test.)
For each file A, this code will create a file A.bak with the original
file contents (before replacing tabs). This is a safeguard. If you
don't want this behavior, just remove the ".bak" from that code.
That should do the trick. If you wanted sophisticated error recovery, you'd
have to write a real program, but, if all your files are readable and all
your directories are writeable, this should be fine.
Hope that helps,
--
Quinn Weaver, independent contractor | President, San Francisco Perl Mongers
http://fairpath.com/quinn/resume/ | http://sf.pm.org/
510-520-5217
More information about the Oakland
mailing list