newbie trying to parse one file into three 
Author Message
 newbie trying to parse one file into three

    I've got a text file with various records in it.  Each record is on an
individual line, and each record is comma delimited.

    I need to take this file, and put (let's say) fields one, four and seven
of each record into a file.  Likewise, I need to put fields two and five
into another file, while fields three and six go into a third file.

    I've got a rough idea on what to do, but what Perl statements/code will
best allow me to do so?  I'd like to have a solid footing to go on before I
start.  Thanks for any and all help.



Fri, 23 Mar 2001 03:00:00 GMT  
 newbie trying to parse one file into three
# Let's say you have a text file called data and it's filled with
# stuff like:
# one, two, three, four, five, six, seven

Here is a sample script, your mileage may vary
open FH1, ">file.one" or die "Cannot open file.one\n";
open FH2, ">file.two" or die "Cannot open file.two\n";
open FH3, ">file.three" or die "Cannot open file.three\n";

while(<>) {
    chomp;
    ($one, $two, $three, $four, $five, $six, $seven) = split /,/;
    print FH1 "$one, $four, $seven\n";
    print FH2 "$two, $five\n";
    print FH3 "$three, $six\n";

Quote:
}

close FH1;
close FH2;
close FH3;

Quote:

>     I've got a text file with various records in it.  Each record is on an
> individual line, and each record is comma delimited.

>     I need to take this file, and put (let's say) fields one, four and seven
> of each record into a file.  Likewise, I need to put fields two and five
> into another file, while fields three and six go into a third file.

>     I've got a rough idea on what to do, but what Perl statements/code will
> best allow me to do so?  I'd like to have a solid footing to go on before I
> start.  Thanks for any and all help.

--
Justin B. Harvey                        http://www.auspex.net/jbharvey

                   use Perl || die "trying";



Sat, 24 Mar 2001 03:00:00 GMT  
 newbie trying to parse one file into three

:     I've got a text file with various records in it.  Each record is on an
: individual line, and each record is comma delimited.

:     I need to take this file, and put (let's say) fields one, four and seven
: of each record into a file.  Likewise, I need to put fields two and five
: into another file, while fields three and six go into a third file.

:     I've got a rough idea on what to do, but what Perl statements/code will
: best allow me to do so?  I'd like to have a solid footing to go on before I
: start.  Thanks for any and all help.

-------------------------------
#!/usr/bin/perl -w

open(FH147, ">147.db") || die "could not open '147.db'  $!";
open(FH25, ">25.db") || die "could not open '25.db'  $!";
open(FH36, ">36.db") || die "could not open '36.db'  $!";

while (<DATA>) {
   chomp;




Quote:
}

close(FH147);
close(FH25);
close(FH36);

__DATA__
one1,two1,three1,four1,five1,six1,seven1
one2,two2,three2,four2,five2,six2,seven2
one3,two3,three3,four3,five3,six3,seven3
one4,two4,three4,four4,five4,six4,seven4
-------------------------------

--
    Tad McClellan                          SGML Consulting

    Fort Worth, Texas



Sat, 24 Mar 2001 03:00:00 GMT  
 newbie trying to parse one file into three

My first reaction whenever I see code like this (the essential 3 lines),
I convert it to:

($\,$,) = ("\n",",");
while (<DATA>) {




Quote:
}

$, replaces the join, $\ takes care of the newline.

        Bart.



Sat, 24 Mar 2001 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Perl Newbie trying to parse file

2. Newbie trying to parse CGI POST data

3. regex for one "two three" four

4. Trying to parse a file...need help

5. Parse a word into three strings

6. Parsing records from one text file to another

7. script to parse one file, search another

8. Newbie trying to do a file move

9. newbie trying to solve file copying problem

10. Newbie trying to compare two .txt files

11. Tutorial for writing modules (newbie ones/ portable ones)

12. Newbie ? - Delete one and only one line

 

 
Powered by phpBB® Forum Software