Converting Delimited Files with Perl (Help!)

Darren Reely darren.reely@latticesemi.com
Thu, 08 Nov 2001 14:44:20 -0800


Karen,

Your first problem seems to be the 'use Text...' part.  Perl seems to be
having a problem finding it.  I suggest this because here at Lattice your
second attempt does not show that problem on a Solaris box.  We're running
Perl version 5.004_04.  Once you solve that issue then...

I've played with this a bit I think the following is what you want:

perl -e 'use Text::Tabs; $tabstop=50;while (<>) {print expand($_); } ' <
input.txt > output.txt

Notice that I removed the tr/\t/""/; for two reasons.  First is that the
tabs are replaced with a single quote (").  Second, it is not needed anyway
as the expand() function seems to work with the $tabstop variable and
tabs.  I have no documention at hand so I'm somewhat guessing about the
second issue, but my solution works for me.

Good luck,

Darren Reely


Karen Barrett wrote:
> 
> Recently Tom Patton posted a solution to expanding a delimited file to fixed
> width using Perl.  This looked pretty good to me as I have a tab delimited
> file that is giving us big headaches. Yours truly who can barely stagger
> around Unix  is trying to revise the examples.
> 
> # Replace with tabs:
> perl -e 'while (<>) {tr/|/\t/; print; } < oldfile > newfile
> 
> #Blank-pad with arbirtrary field width (10 in this example)
> perl -e 'use Text::Tabs; $tabstop=10; while (<>) { tr/|/\t;
>    print expand($_); }' < oldfile > newfile
> 
> My first effort :
> perl -e 'while (<>) {tr/\t/""/;  print; } ' < segments.txt > seg2.txt
> removed some but not all of the tabs.
> 
> My second effort:
> perl -e 'use Text::Tabs; $tabstop=50;while (<>) {tr/\t/""/; print expand
> ($_); } ' < segments.txt > segnew.txt
> Generated the following -
> syntax error in file /tmp/perl-ea24890 at line 1, next 2 tokens "use Text"
> Execution of /tmp/perl-ea24890 aborted due to compilation errors.
> 
> This looks to be my only foray into Unix/Perl on the horizon for some time.
> If I am making a simple error (or errors), can someone steer me? I was
> unsuccessful in my attempts to email Tom offline.
> 
> Karen Barrett
> Senior Program Analyst
> Denkor Dental Management Corp.
> Portland, OR.
> 
> = = = = = = = = = = = = = = = = = = = = = = = = = = = =
> Mailing list: powerh-l@lists.swau.edu
> Subscribe: "subscribe" in message body to powerh-l-request@lists.swau.edu
> Unsubscribe: "unsubscribe" in message body to powerh-l-request@lists.swau.edu
> http://lists.swau.edu/mailman/listinfo/powerh-l
> This list is closed, thus to post to the list you must be a subscriber.