Parsing records from one text file to another 
Author Message
 Parsing records from one text file to another

I have a text file that looks like this:

REC ITEMS~2~4~4~EACH~250.00~~1000.00~PRODUCT DESCRIPTION~ADDITIONAL
COMMENTS
REC HEAD~016969~19980630~709~020449~099~19980630~MERCER FREIGHT
REC ITEMS~1~1~1~EACH~2000.00~~2000.00~PRODUCT DESCRIPTION~ADDITIONAL
COMMENTS

I want to perform some sort of loop that says: if field one of my text file
is equal to "REC ITEMS" then print field one and the associatied fields
with it to another file.  The file would hold all "REC ITEMS" records in
it.

For example,
REC ITEMS~2~4~4~EACH~250.00~~1000.00~PRODUCT DESCRIPTION~ADDITIONAL
COMMENTS
REC ITEMS~1~1~1~EACH~2000.00~~2000.00~PRODUCT DESCRIPTION~ADDITIONAL
COMMENTS

Anyone know a good way to do this?

-Marcus



Sun, 25 Mar 2001 03:00:00 GMT  
 Parsing records from one text file to another

Quote:

>I have a text file that looks like this:

>REC ITEMS~2~4~4~EACH~250.00~~1000.00~PRODUCT DESCRIPTION~ADDITIONAL
>COMMENTS
>REC HEAD~016969~19980630~709~020449~099~19980630~MERCER FREIGHT
>REC ITEMS~1~1~1~EACH~2000.00~~2000.00~PRODUCT DESCRIPTION~ADDITIONAL
>COMMENTS

>I want to perform some sort of loop that says: if field one of my text
>file is equal to "REC ITEMS" then print field one and the associatied
>fields with it to another file.  The file would hold all "REC ITEMS"
>records in it.

>For example,
>REC ITEMS~2~4~4~EACH~250.00~~1000.00~PRODUCT DESCRIPTION~ADDITIONAL
>COMMENTS
>REC ITEMS~1~1~1~EACH~2000.00~~2000.00~PRODUCT DESCRIPTION~ADDITIONAL
>COMMENTS
>Anyone know a good way to do this?

I guess your fields are separated with "~"?

  while (<>) {
        if ((split/~/)[0] eq "REC ITEMS") {
                print;
        }
  }

That's one. It's more extensible than just printing the line if there's
a match, in case you decide to reformat the line as well. Another:

  while (<>) {
        /^REC ITEMS~/ or next;
        print;
  }

That's two. It skips the rest of the loop if the line doesn't start with
"REC ITEMS~". Again, it's easy to extend.

And here's a really compact one, which reads all lines in the file at
once, filters them, and prints just the selection:

        print grep { /^REC ITEMS~/ } <>;

Not quite as extensible.

        Bart.



Mon, 26 Mar 2001 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. One file for each Record or all records in one File?

2. database file where specific users can edit ONLY one record each

3. Parsing /(terminated|non-terminated)/ records from a file

4. NEED HELP GRABBING RECORDS OUT OF TEXT FILE

5. How to split text file into records?

6. newbie trying to parse one file into three

7. script to parse one file, search another

8. How to read one line from a text file

9. confining a search to a part one part of a text file

10. Parsing text file not working properly

11. Parsing large text file (TIA)

12. strategy for parsing text file

 

 
Powered by phpBB® Forum Software