How to convert tab delimited file to space delimited file 
Author Message
 How to convert tab delimited file to space delimited file


Quote:
> I have several tab delimited ASCII files that I need to convert to fixed
> length space delimited fields.

what do you mean, `fixed length' `space delimited'?  the program here will
do the first one.  if you just want to add a space between the fixed-length
fields (to make it easier to read), this is easy to do.

Quote:
> Some of the fields consist of multiple words.

it can't be space-delimeted if it's got spaces inside fields, so I'll
assume you mostly care about the fixed-length aspect.

Quote:
> Is there an easy way to do this using Perl?

here's an awk solution I wrote not long ago.  conversion to perl should
be trivial (even automatic, with a2p), but I've redirected to c.l.awk
just in case.

some bits could be done with sprintf(), and I could use NR's persistence
instead of my own numrecs, but... some things I don't bother remembering.

# tab2fix.awk -- change tab-delimited to fixed-width

BEGIN \
{
        FS="\t";
        exitcode=0;
        STDERR="/dev/stderr";
        numrecs=0;

Quote:
}

function savedata()
{
        for (j=1;j<=NF;j++)
                data[numrecs,j]=$j

Quote:
}

(NR==1) \
{
        numrecs++;

        numf=NF;
        for (i=1;i<=NF;i++)
                widths[i]=length($i);

        savedata();

        next;

Quote:
}

{
        numrecs++;

        if (numf!=NF)
        {
                print("record " NR " has odd number of fields") > STDERR;
                exitcode=1;
                exit(exitcode);
        }

        for (i=1;i<=NF;i++)
                if (length($i)>widths[i])
                        widths[i]=length($i);

        savedata();

Quote:
}

END \
{
        if (exitcode)
                exit(exitcode);

        for (r=1;r<=numrecs;r++)
        {
                aline="";
                for (i=1;i<=numf;i++)
                {
                        afield=data[r,i];
                        while (length(afield)<widths[i])
                                afield=afield " ";
                        aline=aline afield;
                }
                print aline;
        }

Quote:
}

--



Wed, 11 Aug 1999 03:00:00 GMT  
 How to convert tab delimited file to space delimited file


Quote:


> > I have several tab delimited ASCII files that I need to convert to fixed
> > length space delimited fields.

> what do you mean, `fixed length' `space delimited'?  the program here will
> do the first one.  if you just want to add a space between the fixed-length
> fields (to make it easier to read), this is easy to do.

Methinks what this fellow really means is, "take a tab-delimited file,
and insert spaces so as to make the columns line up nicely in a fixed-
pitch font."

IF he knows the maximum possible length of each column, then what he
can do is (1) split the line into fields on tabs, then (2) pad
the end of each with spaces by saying something like

$tmpString = pack("A30",$tmpString); #30 is the total length desired

and then concatenate 'em all together.

Alternatively, if he's running some flavor of Unix, he should also
consider either the -t and -e flags on pr or the expand command
if his flavor of Unix has the expand command.  Check the manpages
for pr and expand.  On a BSD-derived system, you can say something
like:

expand -10,20,30,40,50,60 filename
        ^^ ^^ ^^ ^^ ^^ ^^ These values are the tabstops, in spaces

If, on the other hand, he does not know the maximum length of each
column, then it will require a two-pass solution. First pass to
determine the longest value in each column, second pass to do
the expansion.  Been there, done that, it's a royal pain.  It's
even more fun when you also have an upper limit on total line
length -- then you may have to do some wrapping as well.  This is
the point at which I generally give up and import the file into
a word-processing, typesetting, or report-generation program that
groks tables with multi-line cells.

Actually, come to think of it, maybe the solution might be to
generate an HTML table, then use your favorite WWW browser to
render it...

Hope this helps.

---------

http://paella.med.yale.edu/~healy
Go and share the Gospel.  Use words only if necessary --St Francis



Sat, 14 Aug 1999 03:00:00 GMT  
 How to convert tab delimited file to space delimited file

I can only agree on all points.  In addition, I'd like to point out that
awk is often handy to pipe-fit the data into tbl(1) | nroff(1).  It's
certainly handy to generate ascii mail reports.

                                                Martin Neitzel
--

Unix, Networking, Internet-Services / Xlink-POP           "Alles wird gut."



Sun, 15 Aug 1999 03:00:00 GMT  
 How to convert tab delimited file to space delimited file


Quote:
>> what do you mean, `fixed length' `space delimited'?  the program here
>> will do the first one.  if you just want to add a space between the
>> fixed-length fields (to make it easier to read), this is easy to do.

> Methinks what this fellow really means is, "take a tab-delimited file,
> and insert spaces so as to make the columns line up nicely in a fixed-
> pitch font."

that's what the code I posted does.

Quote:
> $tmpString = pack("A30",$tmpString); #30 is the total length desired

um, I _did_ set followups to the awk group.

Quote:
> If, on the other hand, he does not know the maximum length of each
> column, then it will require a two-pass solution. First pass to
> determine the longest value in each column, second pass to do
> the expansion.  Been there, done that, it's a royal pain.

I disagree.  the code I posted does this.  it's not much.
--



Sun, 15 Aug 1999 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Pluggin to open Tab delimited file

2. Tab Delimited Basic File

3. Exporting .tps to tab delimited text file

4. Help working with space delimited files

5. Importing a TAB delimited file.

6. append from a tab delimited file ?

7. problem importing TAB delimited file

8. Newbie: Append from tab delimited file?

9. appending tab delimited test files

10. Loading Tab delimited data from text file

11. Tab-delimited output file

12. Q: Performing INSPECT on Tab-delimited File in MFCOBOL V3.4.18

 

 
Powered by phpBB® Forum Software