[oak perl] Replace the tab delineator

Steve Kolupaev skolupae at sonic.net
Mon Feb 18 15:27:20 PST 2008


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. 
>
> Thanks
>
> Justin Tabatchnick
> Intel Corporation
>   
Justin,

This is a classic beginning exercise in Perl. Someone on the list can 
probably point out an excellent example already worked out. Here's my 
quick take, providing all the details. This changes the delimiter from 
TAB to "!", with hard-coded filenames
and no error checking.

$input_file = "xxxxx.txt";
$output_file = "yyyyy.txt";
open( FH, $input_file );
open( OFH, ">$output_file");
while ( <FH> ) { s/\t/!/g; print OFH }


Perhaps you just want to get this done, and don't really want to write a 
program. In that case, the Gnumeric spreadsheet program should do a fine 
job. Gnumeric is part of Gnome Office, which you can get for Ubuntu and 
SuSE linux. There is Windows build of Gnumeric also. If memory serves, 
MS Excel can also save to a text file with your choice of column delimiter.

Assuming you are have Gnumeric on Linux:
First start Gnumeric. In Ubuntu or Suse: just type gnumeric ENTER
from a shell window. Once the GUI starts, you do to following:

Data-> Get External Data -> Import Text File
select the input file
hit Open
hit Forward
select the delimiter character if not already correct
hit Forward
hit Finish

and your input spreadsheet is internalized by Gnumeric.
Now save with a different separator character like this:

Hit File
Hit Save As
set the output file name, set File type to "text export (configurable)"
Hit Save
select the new separator character from dropdown list or input box
Click Save

and you're done.



More information about the Oakland mailing list