Parsing and Matching <NEWB>

C

Chris Petersen

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 :)
 
M

Matt Garrish

Chris Petersen said:
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
 
T

Tad McClellan

Chris Petersen said:
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
 
T

Tad McClellan

Chris Petersen said:
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 ...
 
C

Chris Charley

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
 
C

Chris Petersen

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:

and

combo2.txt which contains:

CCC.dat
DDD.datID,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
load,AAA.dat,1111
load,BBB.dat,1111
move,AAA.dat,1111
move,BBB.dat,1111
dump,CCC.dat,1111
dump,DDD.dat,1111
 
C

Chris Petersen

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:

and

combo2.txt which contains:

CCC.dat
DDD.datID,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
load,AAA.dat,1111
load,BBB.dat,1111
move,AAA.dat,1111
move,BBB.dat,1111
dump,CCC.dat,1111
dump,DDD.dat,1111
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top