Howto Parse data in spaced columns??? 
Author Message
 Howto Parse data in spaced columns???

--------------74B844D8CA773035A9C8AC7C
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

How do I parse data out of the following computed text file, you can
assume no tabs, just plain old spaces:

column 1         column 2               column 3                column 4

aaa                  bbb                       ccc
ddd
eee                                                     fff

I want to be able to know that fff is in column 4, and that ccc is in
column 3.

Thanks for your time,
CG

--------------74B844D8CA773035A9C8AC7C
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit

<HTML>
<FONT SIZE=-1></FONT>

<P><FONT SIZE=-1>How do I parse data out of the following computed text
file, you can assume no tabs, just plain old spaces:</FONT>
<BR><FONT SIZE=-1></FONT>&nbsp;<FONT SIZE=-1></FONT>

<P><FONT SIZE=-1>column 1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
column 2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
column 3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
column 4</FONT><FONT SIZE=-1></FONT>

<P><FONT SIZE=-1>aaa&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
bbb&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
ccc</FONT>
<BR><FONT SIZE=-1>ddd&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
eee&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
fff</FONT><FONT SIZE=-1></FONT>

<P><FONT SIZE=-1>I want to be able to know that fff is in column 4, and
that ccc is in column 3.</FONT><FONT SIZE=-1></FONT>

<P><FONT SIZE=-1>Thanks for your time,</FONT>
<BR><FONT SIZE=-1>CG</FONT></HTML>

--------------74B844D8CA773035A9C8AC7C--



Tue, 29 Feb 2000 03:00:00 GMT  
 Howto Parse data in spaced columns???

Quote:

> How do I parse data out of the following computed text file, you can
> assume no tabs, just plain old spaces:

> column 1         column 2               column 3                column
> 4

> aaa                  bbb                       ccc
> ddd
> eee                                                     fff

an off the top of my head inneficient way (in a pinch) might be (i'm
open to suggestion myself):

open(file,"cat filename|cut -c$range1|");
while(defined($=<file>){
#check for item

Quote:
}

open(file,"cat filename|cut -c$range2|");
while(defined($=<file>){
#check for item
Quote:
}

open(file,"cat filename|cut -c$range3|");
while(defined($=<file>){
#check for item
Quote:
}

open(file,"cat filename|cut -c$range4|");
while(defined($=<file>){
#check for item

Quote:
}
> I want to be able to know that fff is in column 4, and that ccc is in
> column 3.

> Thanks for your time,
> CG

--Ray
.~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.
      --- reply to rbush - at - up - dot - net ---
--- please include indication of past correspondence ---
      --- in order to receive a faster response ---

--Ray
.~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.
      --- reply to rbush - at - up - dot - net ---
--- please include indication of past correspondence ---
      --- in order to receive a faster response ---



Tue, 29 Feb 2000 03:00:00 GMT  
 Howto Parse data in spaced columns???

Quote:

> well, you kinda wanna count the spaces in the file, to make life easier.
> assume that it's gonna be about 10 spaces between each items (make sure
> that you leave 30 spaces between eee & fff), then things will be easy to
> take care of.

> assign the file handle to array or string, and then count the delimiters
> (whether 10 spaces or \n or \t, so long as there is a pattern to apply).
> then you just do a split on them.  it will give you two "" items in the
> second column.

> the following example use a single space for delimiter.

> reading it the file into an array...
> foreach (array) {
>         ($item1, $item2, $item3, $item4) = split (' ', $_);
> }

> that should do it.

> yin-so

sure if there is a single space delimiter, or if the number of spaces
stays regular;
i thought about this too but the example file dindt give this.

The problem is in the program that generates the data.
If he was going to change the problem i'd put a null (or somethingthat
wont appear as $item) in for blank entries then just do:

open (file,("cat somefile|));
while(<file>){
        ($_=$_)=s/  / /g;

        $count=1;

                if ($item ne "null") {
                        print "$item is in column $count\n";
                }
                count++;
        }

Quote:
}

close(file);

but it sure sounds like somone is going to have to fix the input file.

- Show quoted text -

Quote:

> > How do I parse data out of the following computed text file, you can
> > assume no tabs, just plain old spaces:

> > column 1         column 2               column 3                column 4

> > aaa                  bbb                       ccc
> > ddd
> > eee                                                     fff

> > I want to be able to know that fff is in column 4, and that ccc is in
> > column 3.

> > Thanks for your time,
> > CG

--
--Ray
.~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.
      --- reply to rbush - at - up - dot - net ---
--- please include indication of past correspondence ---
      --- in order to receive a faster response ---


Tue, 29 Feb 2000 03:00:00 GMT  
 Howto Parse data in spaced columns???

well, you kinda wanna count the spaces in the file, to make life easier.
assume that it's gonna be about 10 spaces between each items (make sure
that you leave 30 spaces between eee & fff), then things will be easy to
take care of.

assign the file handle to array or string, and then count the delimiters
(whether 10 spaces or \n or \t, so long as there is a pattern to apply).
then you just do a split on them.  it will give you two "" items in the
second column.

the following example use a single space for delimiter.

reading it the file into an array...
foreach (array) {
        ($item1, $item2, $item3, $item4) = split (' ', $_);

Quote:
}

that should do it.

yin-so

Quote:

> How do I parse data out of the following computed text file, you can
> assume no tabs, just plain old spaces:

> column 1         column 2               column 3                column 4

> aaa                  bbb                       ccc
> ddd
> eee                                                     fff

> I want to be able to know that fff is in column 4, and that ccc is in
> column 3.

> Thanks for your time,
> CG



Tue, 29 Feb 2000 03:00:00 GMT  
 Howto Parse data in spaced columns???

Quote:

>How do I parse data out of the following computed text file, you can
>assume no tabs, just plain old spaces:

          1         2         3         4         5         6
 1234567890123456789012345678901234567890123456789012345678901234567890

Quote:
>column 1         column 2               column 3                column 4

>aaa                  bbb                       ccc
>ddd
>eee                                                     fff

>I want to be able to know that fff is in column 4, and that ccc is in
>column 3.

From your sample, it's not clear that "fff" is in column 4. For our
purposes, let's say that the column headings are left-justified
(making "fff" lie in column 3, but you can adapt the numbers as you
need to).

column 1 is 17 characters wide (1-17)
column 2 is 23 characters wide (18-40)
column 3 is 24 characters wide (41-64)
column 4 is the rest.



that there may not be data in every column. Then you can strip off
any leading/trailing spaces if you want, and process the columns' data.

Steve Monson
--
The difference between chimps and Chicago drivers is that chimps
display their fangs whereas Chicago drivers display their middle
finger.
                     -- Allen R. Sampson



Tue, 29 Feb 2000 03:00:00 GMT  
 Howto Parse data in spaced columns???

Raymond> open (file,("cat somefile|));

I'm sorry, I don't normally hand out "Useless Use of Cat Awards" in
comp.lang.perl.misc as I do in comp.unix.{questions,shell}, but this
one was too good to pass up.

Why fire off a cat process, when:

        open file, "<somefile" or die "Bad open: $!";

would have done it?

I'm still puzzled why people use all these cats. :-)

print "Just another Perl hacker," # but not what the media calls "hacker!" :-)
## legal fund: $20,990.69 collected, $186,159.85 spent; just 353 more days

--
Name: Randal L. Schwartz / Stonehenge Consulting Services (503)777-0095
Keywords: Perl training, UNIX[tm] consulting, video production, skiing, flying

Web: <A HREF="http://www.stonehenge.com/merlyn/">My Home Page!</A>
Quote: "I'm telling you, if I could have five lines in my .sig, I would!" -- me



Wed, 01 Mar 2000 03:00:00 GMT  
 Howto Parse data in spaced columns???

Quote:

> How do I parse data out of the following computed text file, you can
> assume no tabs, just plain old spaces:

> column 1         column 2               column 3               column4

> aaa                  bbb                       ccc
> ddd
> eee                                                     fff

> I want to be able to know that fff is in column 4, and that ccc is in
> column 3.

Are the columns of equal width?  If so then try this script (set $length
as appropriate) and send the data as stdin (eg. script < data_file):

#!/usr/bin/perl -w

$length = 10;

while (<>) {
    chop;
    $line++;
    $col = 0;
    print map { $col++; "$line $col: $_\n"; } /.{1,$length}/g;

Quote:
}

It's up to you to strip surrounding whitespace.
Even if the widths aren't equal it isn't much harder.  Also you could
just assign the values to a list, a la:

Please excuse the (possibly) bad formatting.  I'm stuck posting
news with netscape until I can get Pnews working.
--



Thu, 02 Mar 2000 03:00:00 GMT  
 Howto Parse data in spaced columns???

Funny that no-one thought of unpack.

Quote:


>>How do I parse data out of the following computed text file, you can
>>assume no tabs, just plain old spaces:
>>>>column 1         column 2               column 3                column 4

>>aaa                  bbb                       ccc
>>ddd
>>eee                                                     fff

>>I want to be able to know that fff is in column 4, and that ccc is in
>>column 3.
>column 1 is 17 characters wide (1-17)
>column 2 is 23 characters wide (18-40)
>column 3 is 24 characters wide (41-64)
>column 4 is the rest.


This will strip trailing whitespaces in the colums too.

Or, if the ciumns aren't fixed width, the next will assume columns are
divided by at least two spaces:


HTH,
Bart.



Thu, 02 Mar 2000 03:00:00 GMT  
 Howto Parse data in spaced columns???

Quote:


> > How do I parse data out of the following computed text file, you can
> > assume no tabs, just plain old spaces:

> > column 1         column 2               column 3                column
> > 4

> > aaa                  bbb                       ccc
> > ddd
> > eee                                                     fff

> an off the top of my head inneficient way (in a pinch) might be (i'm
> open to suggestion myself):

> open(file,"cat filename|cut -c$range1|");
> while(defined($=<file>){
> #check for item
> }
> open(file,"cat filename|cut -c$range2|");
> while(defined($=<file>){
> #check for item
> }
> open(file,"cat filename|cut -c$range3|");
> while(defined($=<file>){
> #check for item
> }
> open(file,"cat filename|cut -c$range4|");
> while(defined($=<file>){
> #check for item
> }

> > I want to be able to know that fff is in column 4, and that ccc is in
> > column 3.

> > Thanks for your time,
> > CG

hmmm that is a useless use of cat ... oops sorry ...


Thu, 02 Mar 2000 03:00:00 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. howto specify maybe 1+ space regexp

2. howto split without regard to white spaces?

3. Extra spaces in output and concatenating columns

4. Tricky column formatting/space removing question

5. DBD::CSV with column names containing spaces

6. parsing with cgi-lib howto?

7. Parse string retaining column info

8. parsing a record at specific columns

9. howto read binary data from files

10. Win32::ODBC Question: Order of data columns returned from Select()

11. automatically pretty print columns of data

12. sqlserver to mysql data transfer of image column

 

 
Powered by phpBB® Forum Software