Extract same text from file.

Discussion in 'Perl Misc' started by Ryan Keeling, Aug 16, 2004.

  1. Ryan Keeling

    Ryan Keeling Guest

    Hi
    I have the following text file which I want to use to populate text
    files containing data only relevant to a particular project. Is it
    possible for me to group similar lines together and put them in their
    own text file? Such as a file containing all entries for
    MS102_PCP.Implementation and another file for MS102_PCP.Support.

    Any help would be greatly appreciated!


    2004:01:07:3:02:MS102_PCP:Belfast:Charge:pCP:Implementation:+:5.5
    2004:01:07:3:02:MS102_PCP:Belfast:Charge:pCP:Support:+:2.0
    2004:01:05:1:02:MS102_PCP:Belfast:Charge:pCP:Implementation:+:7.5
    2004:01:09:5:02:MS102_PCP:Belfast:Charge:pCP:Meetings:+:1.0
    2004:01:20:2:04:MS102_PCP:Belfast:Charge:pCP:Standards:6.5
    2004:01:19:1:04:MS102_PCP:Belfast:Charge:pCP:Standard:7.5
    2004:02:03:2:06:MS102_PCP:Belfast:Charge:pCP:Support:+:2.0
    2004:03:02:2:10:MS102_PCP:Belfast:Charge:pCP:Implementation:+:5.5
    2004:03:03:3:10:MS102_PCP:Belfast:Charge:pCP:Implementation:+:7.5
    2004:03:02:2:10:MS102_PCP:Belfast:Charge:pCP:Support:+:2.5
    2004:03:26:5:13:MS102_PCP:Belfast:Charge:pCP:Support:+:7.5
    2004:03:29:1:14:MS102_PCP:Belfast:Charge:pCP:Support:+:8.0
    2004:03:30:2:14:MS102_PCP:Belfast:Charge:pCP:Implementation:+:8.0
    2004:01:05:1:02:MS102_PCP:Belfast:Charge:pCP:project-Control:+:7.5
    2004:01:06:2:02:MS102_PCP:Belfast:Charge:pCP:Meetings:+:2.0
    2004:01:06:2:02:MS102_PCP:Belfast:Charge:pCP:Meetings:+:0.7


    Ryan
    Ryan Keeling, Aug 16, 2004
    #1
    1. Advertising

  2. Ryan Keeling wrote:
    > Hi
    > I have the following text file which I want to use to populate text
    > files containing data only relevant to a particular project. Is it
    > possible for me to group similar lines together and put them in their
    > own text file? Such as a file containing all entries for
    > MS102_PCP.Implementation and another file for MS102_PCP.Support.
    >
    > Any help would be greatly appreciated!
    >
    >
    > 2004:01:07:3:02:MS102_PCP:Belfast:Charge:pCP:Implementation:+:5.5
    > 2004:01:07:3:02:MS102_PCP:Belfast:Charge:pCP:Support:+:2.0
    > 2004:01:05:1:02:MS102_PCP:Belfast:Charge:pCP:Implementation:+:7.5
    > 2004:01:09:5:02:MS102_PCP:Belfast:Charge:pCP:Meetings:+:1.0
    > 2004:01:20:2:04:MS102_PCP:Belfast:Charge:pCP:Standards:6.5
    > 2004:01:19:1:04:MS102_PCP:Belfast:Charge:pCP:Standard:7.5
    > 2004:02:03:2:06:MS102_PCP:Belfast:Charge:pCP:Support:+:2.0
    > 2004:03:02:2:10:MS102_PCP:Belfast:Charge:pCP:Implementation:+:5.5
    > 2004:03:03:3:10:MS102_PCP:Belfast:Charge:pCP:Implementation:+:7.5
    > 2004:03:02:2:10:MS102_PCP:Belfast:Charge:pCP:Support:+:2.5
    > 2004:03:26:5:13:MS102_PCP:Belfast:Charge:pCP:Support:+:7.5
    > 2004:03:29:1:14:MS102_PCP:Belfast:Charge:pCP:Support:+:8.0
    > 2004:03:30:2:14:MS102_PCP:Belfast:Charge:pCP:Implementation:+:8.0
    > 2004:01:05:1:02:MS102_PCP:Belfast:Charge:pCP:project-Control:+:7.5
    > 2004:01:06:2:02:MS102_PCP:Belfast:Charge:pCP:Meetings:+:2.0
    > 2004:01:06:2:02:MS102_PCP:Belfast:Charge:pCP:Meetings:+:0.7


    Answer to your question: yes.

    Obvious counter-question: what have you tried so far and where have you
    had problems?

    Since your data is reasonably structured, I'd use split and then create
    a filename from fields 8 and 9 to append the current line to. If the
    data isn't too much, you can use a hash of arrays to collect the data
    and write it out in one go.

    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett
    Josef Moellers, Aug 16, 2004
    #2
    1. Advertising

  3. Ryan Keeling

    Tore Aursand Guest

    On Mon, 16 Aug 2004 02:44:31 -0700, Ryan Keeling wrote:
    > I have the following text file which I want to use to populate text
    > files containing data only relevant to a particular project. Is it
    > possible for me to group similar lines together and put them in their
    > own text file? Such as a file containing all entries for
    > MS102_PCP.Implementation and another file for MS102_PCP.Support.
    >
    > 2004:01:07:3:02:MS102_PCP:Belfast:Charge:pCP:Implementation:+:5.5
    > 2004:01:07:3:02:MS102_PCP:Belfast:Charge:pCP:Support:+:2.0
    > 2004:01:05:1:02:MS102_PCP:Belfast:Charge:pCP:Implementation:+:7.5
    > [...]


    Yes, it is _very_ possible. What have you tried so far? You want to look
    into 'perldoc -f split', for example.


    --
    Tore Aursand <>
    "A teacher is never a giver of truth - he is a guide, a pointer to the
    truth that each student must find for himself. A good teacher is
    merely a catalyst." (Bruce Lee)
    Tore Aursand, Aug 16, 2004
    #3
  4. Ryan Keeling

    Abhinav Guest

    Ryan Keeling wrote:

    > Hi
    > I have the following text file which I want to use to populate text
    > files containing data only relevant to a particular project. Is it
    > possible for me to group similar lines together and put them in their
    > own text file? Such as a file containing all entries for
    > MS102_PCP.Implementation and another file for MS102_PCP.Support.
    >
    > Any help would be greatly appreciated!
    >
    >
    > 2004:01:07:3:02:MS102_PCP:Belfast:Charge:pCP:Implementation:+:5.5
    > 2004:01:07:3:02:MS102_PCP:Belfast:Charge:pCP:Support:+:2.0
    > 2004:01:05:1:02:MS102_PCP:Belfast:Charge:pCP:Implementation:+:7.5
    > 2004:01:09:5:02:MS102_PCP:Belfast:Charge:pCP:Meetings:+:1.0
    > 2004:01:20:2:04:MS102_PCP:Belfast:Charge:pCP:Standards:6.5
    > 2004:01:19:1:04:MS102_PCP:Belfast:Charge:pCP:Standard:7.5
    > 2004:02:03:2:06:MS102_PCP:Belfast:Charge:pCP:Support:+:2.0
    > 2004:03:02:2:10:MS102_PCP:Belfast:Charge:pCP:Implementation:+:5.5
    > 2004:03:03:3:10:MS102_PCP:Belfast:Charge:pCP:Implementation:+:7.5
    > 2004:03:02:2:10:MS102_PCP:Belfast:Charge:pCP:Support:+:2.5
    > 2004:03:26:5:13:MS102_PCP:Belfast:Charge:pCP:Support:+:7.5
    > 2004:03:29:1:14:MS102_PCP:Belfast:Charge:pCP:Support:+:8.0
    > 2004:03:30:2:14:MS102_PCP:Belfast:Charge:pCP:Implementation:+:8.0
    > 2004:01:05:1:02:MS102_PCP:Belfast:Charge:pCP:project-Control:+:7.5
    > 2004:01:06:2:02:MS102_PCP:Belfast:Charge:pCP:Meetings:+:2.0
    > 2004:01:06:2:02:MS102_PCP:Belfast:Charge:pCP:Meetings:+:0.7
    >


    Something like this (untested) ?

    use strict;
    use warnings;

    while (<STDIN>)
    {
    my @outStr = split ':';
    system qq(echo '$_' >> '$outStr[9].txt');

    }
    Regards

    --
    Abhinav
    Abhinav, Aug 16, 2004
    #4
  5. Ryan Keeling

    Abhinav Guest

    Abhinav wrote:

    > Ryan Keeling wrote:
    >
    >
    >>Hi
    >>I have the following text file which I want to use to populate text
    >>files containing data only relevant to a particular project. Is it
    >>possible for me to group similar lines together and put them in their
    >>own text file? Such as a file containing all entries for
    >>MS102_PCP.Implementation and another file for MS102_PCP.Support.
    >>
    >>Any help would be greatly appreciated!
    >>
    >>
    >>2004:01:07:3:02:MS102_PCP:Belfast:Charge:pCP:Implementation:+:5.5
    >>2004:01:07:3:02:MS102_PCP:Belfast:Charge:pCP:Support:+:2.0
    >>2004:01:05:1:02:MS102_PCP:Belfast:Charge:pCP:Implementation:+:7.5
    >>2004:01:09:5:02:MS102_PCP:Belfast:Charge:pCP:Meetings:+:1.0
    >>2004:01:20:2:04:MS102_PCP:Belfast:Charge:pCP:Standards:6.5
    >>2004:01:19:1:04:MS102_PCP:Belfast:Charge:pCP:Standard:7.5
    >>2004:02:03:2:06:MS102_PCP:Belfast:Charge:pCP:Support:+:2.0
    >>2004:03:02:2:10:MS102_PCP:Belfast:Charge:pCP:Implementation:+:5.5
    >>2004:03:03:3:10:MS102_PCP:Belfast:Charge:pCP:Implementation:+:7.5
    >>2004:03:02:2:10:MS102_PCP:Belfast:Charge:pCP:Support:+:2.5
    >>2004:03:26:5:13:MS102_PCP:Belfast:Charge:pCP:Support:+:7.5
    >>2004:03:29:1:14:MS102_PCP:Belfast:Charge:pCP:Support:+:8.0
    >>2004:03:30:2:14:MS102_PCP:Belfast:Charge:pCP:Implementation:+:8.0
    >>2004:01:05:1:02:MS102_PCP:Belfast:Charge:pCP:project-Control:+:7.5
    >>2004:01:06:2:02:MS102_PCP:Belfast:Charge:pCP:Meetings:+:2.0
    >>2004:01:06:2:02:MS102_PCP:Belfast:Charge:pCP:Meetings:+:0.7
    >>

    >
    >
    > Something like this (untested) ?
    >
    > use strict;
    > use warnings;
    >
    > while (<STDIN>)
    > {
    > my @outStr = split ':';
    > system qq(echo '$_' >> '$outStr[9].txt');


    Oops ..this should be
    system qq(echo '$_' >> '$outStr[5]$outStr[9].txt');
    >
    > }
    > Regards
    >
    > --
    > Abhinav
    Abhinav, Aug 16, 2004
    #5
  6. Ryan Keeling

    Joe Smith Guest

    Ryan Keeling wrote:

    > Such as a file containing all entries for
    > MS102_PCP.Implementation and another file for MS102_PCP.Support.


    Here's some help regarding multiple output files:

    my %filehandles
    while(<>){
    ($dept,$proj) = (...); # Something to pick out the right columns
    unless (exists $filehandles{$proj}) {
    open $filehandles{$proj}, '>>', "$dept.$proj" or warn
    "Cannot append to $dept.$proj: $!\n";
    }
    print $filehandles{$proj} $_ or warn "Output error $proj: $!\n"
    }

    As for the part marked (...); that is an exercise for the reader.
    -Joe
    Joe Smith, Aug 16, 2004
    #6
  7. Ryan Keeling

    Ryan Keeling Guest

    Thanks guys this worked perfectly, it was a bit of a d'oh on my part
    that I didnt think of it earlier!!

    Thanks again

    Ryan

    Joe Smith <> wrote in message news:<mo8Uc.135779$8_6.6463@attbi_s04>...
    > Ryan Keeling wrote:
    >
    > > Such as a file containing all entries for
    > > MS102_PCP.Implementation and another file for MS102_PCP.Support.

    >
    > Here's some help regarding multiple output files:
    >
    > my %filehandles
    > while(<>){
    > ($dept,$proj) = (...); # Something to pick out the right columns
    > unless (exists $filehandles{$proj}) {
    > open $filehandles{$proj}, '>>', "$dept.$proj" or warn
    > "Cannot append to $dept.$proj: $!\n";
    > }
    > print $filehandles{$proj} $_ or warn "Output error $proj: $!\n"
    > }
    >
    > As for the part marked (...); that is an exercise for the reader.
    > -Joe
    Ryan Keeling, Aug 20, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Prabh
    Replies:
    1
    Views:
    448
    Marco Schmidt
    Sep 1, 2003
  2. Prabh
    Replies:
    2
    Views:
    941
    Weichao Wang
    Sep 9, 2003
  3. jason
    Replies:
    3
    Views:
    120
    jason
    Aug 14, 2003
  4. John Smith
    Replies:
    1
    Views:
    106
    William Herrera
    Nov 29, 2003
  5. Mladen
    Replies:
    5
    Views:
    156
    Peter Scott
    Feb 22, 2011
Loading...

Share This Page