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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl search csv fileA where two strings exist on another csv fileB
# 1  
Old 04-12-2012
Perl search csv fileA where two strings exist on another csv fileB

Hi

I have two csv files, with the following formats:

FileA.log:
Code:
Application, This occured blah
Application, That occured blah
Application, Also this
AnotherLog, Bob did this
AnotherLog, Dave did that

FileB.log:

Code:
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

Last edited by radoulov; 04-12-2012 at 06:52 PM.. Reason: Code tags!
# 2  
Old 04-12-2012
Hi PerlNewbRP,

What do you want to print out?
# 3  
Old 04-12-2012
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
# 4  
Old 04-12-2012
One way:
Code:
$ 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

This User Gave Thanks to birei For This Post:
# 5  
Old 04-13-2012
Thanks that works but..

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 Smilie.

I need to hard code the paths for both files and print the difference to another file.
# 6  
Old 04-13-2012
What do you mean with hardcode the paths?

How do you want to run the script and where does it fail?
# 7  
Old 04-13-2012
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:
Code:
$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;
        }
}

Moderator's Comments:
Mod Comment How to use code tags

Last edited by Franklin52; 04-13-2012 at 06:36 AM.. Reason: Please use code tags for code and data samples, thank you
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep from FileA, search in FileB, edit FileC > Output

Hello, Similar question to my previous posts. I am sorry for the trouble... Just checked my old threads but I can not implement any solution into this case.. My aim is to grab each line in fileA, check it in fileB and merge with fileC (tab separated) in corresponding line as given below: FileA:... (2 Replies)
Discussion started by: baris35
2 Replies

2. Shell Programming and Scripting

Read in search strings from text file, search for string in second text file and output to CSV

Hi guys, I have a text file named file1.txt that is formatted like this: 001 , ID , 20000 002 , Name , Brandon 003 , Phone_Number , 616-234-1999 004 , SSNumber , 234-23-234 005 , Model , Toyota 007 , Engine ,V8 008 , GPS , OFF and I have file2.txt formatted like this: ... (2 Replies)
Discussion started by: An0mander
2 Replies

3. Shell Programming and Scripting

awk read column csv and search in other csv

hi, someone to know how can i read a specific column of csv file and search the value in other csv columns if exist the value in the second csv copy entire row with all field in a new csv file. i suppose that its possible using awk but i m not expertise thanks in advance (8 Replies)
Discussion started by: giankan
8 Replies

4. Shell Programming and Scripting

CSV to SQL insert: Awk for strings with multiple lines in csv

Hi Fellows, I have been struggling to fix an issue in csv records to compose sql statements and have been really losing sleep over it. Here is the problem: I have csv files in the following pipe-delimited format: Column1|Column2|Column3|Column4|NEWLINE Address Type|some descriptive... (4 Replies)
Discussion started by: khayal
4 Replies

5. Shell Programming and Scripting

Extract strings from multiple lines into one csv file

Hi all, Please go through my requirement. I have a log file in the location /opt/WebSphere61/AppServer/profiles/EMQbatchprofile/logs/EMQbatch This file contains the follwing pattern data <af type="tenured" id="42" timestamp="May 14 13:44:13 2011" intervalms="955.624"> <minimum... (8 Replies)
Discussion started by: satish.vampire
8 Replies

6. Shell Programming and Scripting

Comparing Strings in 2 .csv/txt files?

EDIT: My problems have been solved thanks to the help of bartus11 and pravin27 This code is just to help me learn. It serves no purpose other than that. Here's a sample csv that I'm working with - #listofpeeps.csv Jackie Chan,1954,M Chuck Norris,1930,M Bruce Lee,1940,M This code is... (13 Replies)
Discussion started by: chickeneaterguy
13 Replies

7. Shell Programming and Scripting

extract strings from file and display in csv format

Hello All, I have a file whose data looks something like this I want to extract just the id, name and city fields in a csv format and sort them by id. Output should look like this. 1,psi,zzz 2,beta,pqr 3,theta,xyz 4,alpha,abc 5,gamma,jkl (12 Replies)
Discussion started by: grajp002
12 Replies

8. Shell Programming and Scripting

Copying lines from fileA if they start by a word from fileB

Hi I'm in the need of a script that basically takes two files and generates a 3rd. It reads from fileA and fileB and copies lines from fileA if they start by a word of fileB. for example fileA The dog is beautful Where is your cat Why are you sad? Help me! fileB The Where tree dog... (4 Replies)
Discussion started by: FrancoisCN
4 Replies

9. Shell Programming and Scripting

Replacing strings in csv file.

Hi, I have a problem.. 1) I have a file that contains the lines as below : VRF-TM_DummyLab/mse02.lab,mse02.lab,ge-2/0/7.222 VRF-EMS_HUAWEI_MSAN_208/mse01.lab,mse01.lab,xe-1/0/0.208 2) I need a method to read this file, line by line from :... (5 Replies)
Discussion started by: msafwan82
5 Replies

10. Shell Programming and Scripting

Replacing strings in a log file and saves as a new csv

Hello Im new here.I need to replace strings and change it into csv format, or at least saves the file as csv if that would work :p. Heres an example of my scenario 1) I have a log file, named abc.log, and its like a txt based file anyway, and the content looks like this ... (2 Replies)
Discussion started by: tententen
2 Replies
Login or Register to Ask a Question