Perl/unix script to convert a fixed width file to a tab delimited file

Perl/unix script to convert a fixed width file to a tab delimited file

am 29.09.2007 15:35:50 von srikant

Hi all,
I have a situtation here. We have a script that reads two
delimited (comma or tab or pipe or semicolon or any other) files and
compares them returnig the list of records/rows unique to file1,
unique to file2 and the mismatches records.
Now, we also have to get this going for fixed width files. I
know that the inbuilt excel tool, Text to columns, does this. However
we need to get this process automated on the unix box.
Any suggestions? If someone can help me with the scipt itself,
that'll be awesome.


Thanks a lot.

Regards
Srikant

Re: Perl/unix script to convert a fixed width file to a tab delimited file

am 29.09.2007 16:32:41 von gbacon

In article <1191072950.845896.75120@y42g2000hsy.googlegroups.com>,
Srikant wrote:

: I have a situtation here. We have a script that reads two
: delimited (comma or tab or pipe or semicolon or any other) files and
: compares them returnig the list of records/rows unique to file1,
: unique to file2 and the mismatches records.
: Now, we also have to get this going for fixed width files. I
: know that the inbuilt excel tool, Text to columns, does this. However
: we need to get this process automated on the unix box.

Something like the following?

$ cat try
#! /usr/bin/perl

use warnings;
use strict;

while () {
chomp;

my @f = unpack "A5A5A5", $_;

print join("," => @f), "\n";
}

__DATA__
R1 Five5Hello
Row2 ThreeBye

$ ./try
R1,Five5,Hello
Row2,Three,Bye

See also `perldoc -q fixed`.

Hope this helps,
Greg
--
Human ingenuity, not government, solves the problem of scarcity. The
nations in which poverty is greatest are those that restrain human
ingenuity -- that is, freedom -- and punish initiative.
-- Wendy McElroy

Re: Perl/unix script to convert a fixed width file to a tab delimitedfile

am 29.09.2007 16:59:58 von Bob Walton

Srikant wrote:
....
> I have a situtation here. We have a script that reads two
> delimited (comma or tab or pipe or semicolon or any other) files and
> compares them returnig the list of records/rows unique to file1,
> unique to file2 and the mismatches records.
> Now, we also have to get this going for fixed width files. I
> know that the inbuilt excel tool, Text to columns, does this. However
> we need to get this process automated on the unix box.
> Any suggestions?

Get the book "Learning Perl" and read it.

If someone can help me with the scipt itself,
> that'll be awesome.
....
> Srikant
--
Bob Walton
Email: http://bwalton.com/cgi-bin/emailbob.pl

Re: Perl/unix script to convert a fixed width file to a tab delimited file

am 29.09.2007 17:26:18 von Dan Mercer

"Srikant" wrote in message news:1191072950.845896.75120@y42g2000hsy.googlegroups.com...
: Hi all,
: I have a situtation here. We have a script that reads two
: delimited (comma or tab or pipe or semicolon or any other) files and
: compares them returnig the list of records/rows unique to file1,
: unique to file2 and the mismatches records.
: Now, we also have to get this going for fixed width files. I
: know that the inbuilt excel tool, Text to columns, does this. However
: we need to get this process automated on the unix box.

If the files are sorted you can just use the comm utility. Else,
perldoc -f pack and perldoc -f unpack for the unpack utility.
It's a trivial one liner to convert a file from fixed width to
csv.

Dan Mercer

: Any suggestions? If someone can help me with the scipt itself,
: that'll be awesome.
:
:
: Thanks a lot.
:
: Regards
: Srikant
: