Perl search csv fileA where two strings exist on another csv fileB

Hi

I have two csv files, with the following formats:

FileA.log:

Application, This occured blah
Application, That occured blah
Application, Also this
AnotherLog, Bob did this
AnotherLog, Dave did that

FileB.log:

Uk, London, Application, datetime, LaterDateTime, Today it had'nt occured blah but I dont care
Uk, London, Application, datetime, LaterDateTime, Today This occured blah and I care
USA, Texas, Application, datetime, LaterDateTime, Today This may have occured blah but I dont care

From Perl, how can I print out to another file, where colum[0] and colum[1] from FileA have occured in colum[3] and colum[6] from FileB - notice that comun[6] may have part of the string.

Without a 1liner plz.

Thanks for your help

Hi PerlNewbRP,

What do you want to print out?

Hi

I'd like to print each line where the columns match, so for example the result will show something like this:

to OUTPUTfile.log:

Uk, London, Application, datetime, LaterDateTime, Today This occured blah and I care

Because it had the work 'application' which matched the string from the other file in colum[0] (in fileB), and it contained the string ' This occured blah ' which occured in column[5] (in fileB).

There will be many rows in both files.

Thanks

One way:

$ head File[AB].log
==> FileA.log <==
Application, This occured blah
Application, That occured blah
Application, Also this
AnotherLog, Bob did this
AnotherLog, Dave did that

==> FileB.log <==
Uk, London, Application, datetime, LaterDateTime, Today it had'nt occured blah but I dont care
Uk, London, Application, datetime, LaterDateTime, Today This occured blah and I care
USA, Texas, Application, datetime, LaterDateTime, Today This may have occured blah but I dont care
$ cat script.pl
use warnings;
use strict;

die qq[Usage: perl $0 <fileA> <fileB>\n] unless @ARGV == 2;

my $file_in_process = 1;
my %fileA;

while ( <> ) {
        next if m/\A\s*\Z/;
        chomp;
        my @f = split /\s*,\s*/;

        if ( $file_in_process == 1 ) {
                next unless @f == 2;
                push @{ $fileA{ $f[0] } }, $f[1];
                next;
        }

        if ( exists $fileA{ $f[2] } and grep { $f[5] =~ m/$_/ } @{ $fileA{ $f[2] } } ) {
                printf qq[%s\n], $_;
        }
}
continue {
        if ( eof ) {
                ++$file_in_process;
        }
}
$ perl script.pl File[AB].log
Uk, London, Application, datetime, LaterDateTime, Today This occured blah and I care
1 Like

Thanks that works but I can't get it to work if I want to hard code the path in...sorry to be a pain :D.

I need to hard code the paths for both files and print the difference to another file.

What do you mean with hardcode the paths?

How do you want to run the script and where does it fail?

Well, it does work perfectly - thanks to you...but I dont want the user to input the paths, so I try this and it hangs:

$f[0] = "C:/Scripts/OUTPUT/Application.log";
$f[1] = "C:/Scripts/OUTPUT/Application2.log";

#die qq[Usage: perl $0 <fileA> <fileB>\n] unless @ARGV == 2;

open(FOUTLOG, ">FOUTAppLogFile.log") || die " Cannot open FOUTAppLogFile file for output\n"; 	#Goto to this file

my $file_in_process = 1;
my %fileA;

while ( <> ) {
        next if m/\A\s*\Z/;
        chomp;
        my @f = split /\s*,\s*/;

        if ( $file_in_process == 1 ) {
                next unless @f == 2;
                push @{ $fileA{ $f[0] } }, $f[1];
                next;
        }

        if ( exists $fileA{ $f[2] } and grep { $f[5] =~ m/$_/ } @{ $fileA{ $f[2] } } ) {
                printf FOUTAppLogFile qq[%s\n], $_;
        }
}
continue 
{
        if ( eof ) {
                ++$file_in_process;
        }
}

Ok. Now I understand.

Try this version (change paths to your particular case):

$ cat script.pl
use warnings;
use strict;

push @ARGV, qw(
        ./FileA.log
        ./FileB.log
);

#die qq[Usage: perl $0 <fileA> <fileB>\n] unless @ARGV == 2;

my $file_in_process = 1;
my %fileA;

while ( <> ) {
        next if m/\A\s*\Z/;
        chomp;
        my @f = split /\s*,\s*/;

        if ( $file_in_process == 1 ) {
                next unless @f == 2;
                push @{ $fileA{ $f[0] } }, $f[1];
                next;
        }

        if ( exists $fileA{ $f[2] } and grep { $f[5] =~ m/$_/ } @{ $fileA{ $f[2] } } ) {
                printf qq[%s\n], $_;
        }
}
continue {
        if ( eof ) {
                ++$file_in_process;
        }
}
$ perl script.pl
Uk, London, Application, datetime, LaterDateTime, Today This occured blah and I care

1 Like

Thanks, that worked a treat - You are a Legend!

---------- Post updated at 12:05 PM ---------- Previous update was at 10:47 AM ----------

I should contact the queen for your knighthood!:smiley: