[oak perl] Oakland Digest, Vol 56, Issue 6

Tabatchnick, Justin justin.tabatchnick at intel.com
Tue Feb 19 12:29:26 PST 2008


It works - thanks

Justin

-----Original Message-----
From: oakland-bounces+justin.tabatchnick=intel.com at pm.org
[mailto:oakland-bounces+justin.tabatchnick=intel.com at pm.org] On Behalf
Of oakland-request at pm.org
Sent: Tuesday, February 19, 2008 12:00 PM
To: oakland at pm.org
Subject: Oakland Digest, Vol 56, Issue 6

Send Oakland mailing list submissions to
	oakland at pm.org

To subscribe or unsubscribe via the World Wide Web, visit
	http://mail.pm.org/mailman/listinfo/oakland
or, via email, send a message with subject or body 'help' to
	oakland-request at pm.org

You can reach the person managing the list at
	oakland-owner at pm.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Oakland digest..."


Today's Topics:

   1. Re: Oakland Digest, Vol 56, Issue 3 (Quinn Weaver)
   2. Re: Replace the tab delineator (Steve Kolupaev)


----------------------------------------------------------------------

Message: 1
Date: Mon, 18 Feb 2008 13:14:47 -0800
From: Quinn Weaver <quinn at fairpath.com>
Subject: Re: [oak perl] Oakland Digest, Vol 56, Issue 3
To: oakland at pm.org
Message-ID: <20080218211447.GB43231 at fu.funkspiel.org>
Content-Type: text/plain; charset=us-ascii

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


------------------------------

Message: 2
Date: Mon, 18 Feb 2008 15:27:20 -0800
From: Steve Kolupaev <skolupae at sonic.net>
Subject: Re: [oak perl] Replace the tab delineator
To: Oakland Perl Mongers <oakland at pm.org>
Message-ID: <47BA1458.20302 at sonic.net>
Content-Type: text/plain; charset=us-ascii; format=flowed

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.



------------------------------

_______________________________________________
Oakland mailing list
Oakland at pm.org
http://mail.pm.org/mailman/listinfo/oakland

End of Oakland Digest, Vol 56, Issue 6
**************************************


More information about the Oakland mailing list