Parsing and Matching <NEWB>

Discussion in 'Perl Misc' started by Chris Petersen, Jul 13, 2004.

  1. Hello all, I have 2 files:

    combo.txt which contains:

    AAA.dat
    BBB.dat

    and

    stats.txt which contains:

    ID,STEP,NAME,DATE,SIZE
    11,load,C:\Bin\combo.txt,20040404,1111 #note: only 2 "\"
    11,move,C:\Bin\Log\combo.txt,20040404,1111 #note: 3 "\"

    I'd like to create the following output.txt file:

    load,AAA.dat,1111
    load,BBB.dat,1111
    move,AAA.dat,1111
    move,BBB.dat,1111

    I've created a shell solution for this, but believe perl could do this
    much quicker, unfortunately I am still learning the language and would
    love any advice to get the ball rolling.

    Side Question:

    Is there a way to store the output of system calls in scalars?

    $asdf = system (echo hello);
    print $asdf; #should print "hello";

    Thanks in advance :)
     
    Chris Petersen, Jul 13, 2004
    #1
    1. Advertising

  2. Chris Petersen

    Matt Garrish Guest

    "Chris Petersen" <> wrote in message
    news:...
    > Hello all, I have 2 files:
    >
    > combo.txt which contains:
    >
    > AAA.dat
    > BBB.dat
    >
    > and
    >
    > stats.txt which contains:
    >
    > ID,STEP,NAME,DATE,SIZE
    > 11,load,C:\Bin\combo.txt,20040404,1111 #note: only 2 "\"
    > 11,move,C:\Bin\Log\combo.txt,20040404,1111 #note: 3 "\"
    >


    The number of backslashes in the path isn't the most effective way to figure
    out which file you're dealing with. You'd be better off associating the file
    with the path in a hash and then substituting one for the other. In other
    words, change combo.txt to a tab-delimited file including the path (or
    however you want to do it):

    AAA.dat c:\Bin\combo.txt
    BBB.dat c:\Bin\Log\combo.txt

    Read that file in to build your hash:

    while (my $line = <DATA>) {

    chomp $line;

    my ($datfile, $path) = split(/\t/, $line)

    $file{$path} = $datfile;

    }

    And then run through your second file and make the appropriate changes. For
    that I would recommend taking a look at the csv modules available and use
    one of them to break the lines in the file down into components.
    Reassembling the data per your requirements should then be a snap.

    >
    > Is there a way to store the output of system calls in scalars?
    >
    > $asdf = system (echo hello);
    > print $asdf; #should print "hello";
    >


    Yes, but you should be using backticks not system if you want to capture the
    output. See the documentation.

    Matt
     
    Matt Garrish, Jul 14, 2004
    #2
    1. Advertising

  3. Chris Petersen <> wrote:
    > Hello all, I have 2 files:
    >
    > combo.txt which contains:
    >
    > AAA.dat
    > BBB.dat
    >
    > and
    >
    > stats.txt which contains:
    >
    > ID,STEP,NAME,DATE,SIZE
    > 11,load,C:\Bin\combo.txt,20040404,1111 #note: only 2 "\"
    > 11,move,C:\Bin\Log\combo.txt,20040404,1111 #note: 3 "\"
    >
    > I'd like to create the following output.txt file:
    >
    > load,AAA.dat,1111
    > load,BBB.dat,1111
    > move,AAA.dat,1111
    > move,BBB.dat,1111



    -----------------------------------
    #!/usr/bin/perl
    use warnings;
    use strict;

    my @files = qw/ AAA.dat BBB.dat /;

    while ( <DATA> ) {
    next if /^ID/;
    chomp;
    my($op, $address) = (split /,/)[1,4]; # "Slices" section in perldata.pod
    print "$op,$_,$address\n" for @files;
    }

    __DATA__
    ID,STEP,NAME,DATE,SIZE
    11,load,C:\Bin\combo.txt,20040404,1111
    11,move,C:\Bin\Log\combo.txt,20040404,1111
    -----------------------------------


    But you should probably be using one of the CSV modules for
    processing CSV data.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jul 14, 2004
    #3
  4. Chris Petersen <> wrote:

    > I am still learning the language and would
    > love any advice to get the ball rolling.



    Read the documentation for the functions that you use.


    > Is there a way to store the output of system calls in scalars?



    Read the documentation for the functions that you use.


    perldoc -f system

    ...
    This is NOT what you want to use to capture the output
    from a command, for that you should use merely ...


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jul 14, 2004
    #4
  5. (Chris Petersen) wrote in message news:<>...
    > Hello all, I have 2 files:
    >
    > combo.txt which contains:
    >
    > AAA.dat
    > BBB.dat
    >
    > and
    >
    > stats.txt which contains:
    >
    > ID,STEP,NAME,DATE,SIZE
    > 11,load,C:\Bin\combo.txt,20040404,1111 #note: only 2 "\"
    > 11,move,C:\Bin\Log\combo.txt,20040404,1111 #note: 3 "\"


    Do the number of slashes signal different processing decisions?


    > I'd like to create the following output.txt file:
    >
    > load,AAA.dat,1111
    > load,BBB.dat,1111
    > move,AAA.dat,1111
    > move,BBB.dat,1111


    If I understand your requirements correctly, this code will produce
    your sample output.

    #!/usr/bin/perl
    use strict;
    use warnings;

    open COMBO, "<", "combo.txt" or die $!;
    chomp(my @files = <COMBO>);
    close COMBO or die $!;

    open STATS, "<", "stats.txt" or die $!;
    open OUT, ">", "output.txt" or die $!;

    while (<STATS>) {
    chomp;
    my ($step, $size) = (split ",")[1, 4];
    for (@files) {
    print OUT join(",", $step, $_, $size), "\n";
    }
    }
    close STATS or die $!;
    close OUT or die $!;


    > Side Question:
    >
    > Is there a way to store the output of system calls in scalars?


    Matt answered your question correctly.

    > Thanks in advance :)


    Chris
     
    Chris Charley, Jul 14, 2004
    #5
  6. Thanks a lot guys, I REALLY appreciate your help (sorry about that
    dumb system question, it came to me at the last second so I just
    through it in there with no real investigation). I don't think I made
    my explanation clear enough though, I'll revise it:

    > > combo1.txt which contains:
    > >
    > > AAA.dat
    > > BBB.dat


    and

    combo2.txt which contains:

    CCC.dat
    DDD.dat
    > >
    > > and
    > >
    > > stats.txt which contains:
    > >

    ID,STEP,NAME,DATE,SIZE
    11,load,C:\Bin\combo.txt,20040404,1111 #note: only 2 "\"
    11,move,C:\Bin\Log\combo.txt,20040404,1111 #note: 3 "\"
    11,dump,C:\combo2.txt,20040404,1111

    > > I'd like to create the following output.txt file:
    > >

    load,AAA.dat,1111
    load,BBB.dat,1111
    move,AAA.dat,1111
    move,BBB.dat,1111
    dump,CCC.dat,1111
    dump,DDD.dat,1111
     
    Chris Petersen, Jul 15, 2004
    #6
  7. Thanks a lot guys, I REALLY appreciate your help (sorry about that
    dumb system question, it came to me at the last second so I just
    through it in there with no real investigation). I don't think I made
    my explanation clear enough though, I'll revise it:

    > > combo1.txt which contains:
    > >
    > > AAA.dat
    > > BBB.dat


    and

    combo2.txt which contains:

    CCC.dat
    DDD.dat
    > >
    > > and
    > >
    > > stats.txt which contains:
    > >

    ID,STEP,NAME,DATE,SIZE
    11,load,C:\Bin\combo.txt,20040404,1111 #note: only 2 "\"
    11,move,C:\Bin\Log\combo.txt,20040404,1111 #note: 3 "\"
    11,dump,C:\combo2.txt,20040404,1111

    > > I'd like to create the following output.txt file:
    > >

    load,AAA.dat,1111
    load,BBB.dat,1111
    move,AAA.dat,1111
    move,BBB.dat,1111
    dump,CCC.dat,1111
    dump,DDD.dat,1111
     
    Chris Petersen, Jul 15, 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. Fernando Armenta

    Help with parsing and matching a character

    Fernando Armenta, Sep 19, 2003, in forum: Python
    Replies:
    1
    Views:
    268
    wes weston
    Sep 20, 2003
  2. Christopher Diggins
    Replies:
    0
    Views:
    632
    Christopher Diggins
    Jul 9, 2007
  3. Marc Bissonnette

    Pattern matching : not matching problem

    Marc Bissonnette, Jan 8, 2004, in forum: Perl Misc
    Replies:
    9
    Views:
    266
    Marc Bissonnette
    Jan 13, 2004
  4. Replies:
    1
    Views:
    241
    tuser
    Jun 6, 2006
  5. Bobby Chamness
    Replies:
    2
    Views:
    268
    Xicheng Jia
    May 3, 2007
Loading...

Share This Page