compare 2 coloum of 2 diff files using perl script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting compare 2 coloum of 2 diff files using perl script
# 1  
Old 07-29-2008
compare 2 coloum of 2 diff files using perl script

Hi,

i am new to perl scripting.. i am still learing it.. i am asked to write a perl script which should compare 2 coloums of 2 different files. if those 2 coloumn are same the script should store the both the lines in 2 diff files.

these are files,

file 1:
21767016 226112 char[]
19136520 797355 java.lang.String
17769368 307049 java.lang.Object[]
13981656 582569 java.util.HashMap$Entry
10867240 16650 int[]
9065616 559799 java.lang.String[]
9060192 79626 java.util.HashMap$Entry[]
6969384 23146 byte[]
6857664 285736 java.util.Vector


file 2:
21702192 904258 java.lang.String
20985320 360561 java.lang.Object[]
20524112 209810 char[]
12623280 525970 java.util.HashMap$Entry
10945080 678896 java.lang.String[]
9781432 10871 int[]
8302464 345936 java.util.Vector
8107104 337796 netscape.ldap.util.RDN
7620024 68357 java.util.HashMap$Entry[]
6515152 52272 * ConstMethodKlass

so i have to compare 3rd coloumn of these to 2 files, eg i have java.lang.string present in both so i want the script store these complete line of both the files in 2 diff files. These 2 files are big files.

please suggest me how can this be done. some one had suggested you can use the hash table implementation.

thanks,
Vasuki
# 2  
Old 07-30-2008
Try this perhaps (untested):

Code:
awk '
    # load the contents of file1 into a hash indexed by $3
    NR==FNR { file1[$3]=$0; next }
    # check whether $3 in file2 is in the hash, if so, print bothlines to files
    $3 in file1 { print file1[$3] >> "file1.both"; print >> "file2.both" }
' file1 file2

# 3  
Old 07-30-2008
And here is (roughly) the same in Perl:

Code:
perl -ane 'BEGIN { open FILE1, ">file1.both"; open FILE2, ">file2.both"; }
  if ($. == ++$n) { $h{$F[2]} = $_; close ARGV if eof; next; }
  if ($h{$F[2]}) { print FILE1 $h{$F[2]}; print FILE2; }' file1 file2

This isn't very idiomatic Perl, but should hopefully be enough to get you started. The trickery with $n and ARGV is to simulate the awk NR==FNR idiom. The eof thing is to reset line numbers in $. when the file changes; see the eof documentation for a brief discussion.

By the way, $3 in 6515152 52272 * ConstMethodKlass is just "*" -- maybe you want to normalize that, rather than change the script.
# 4  
Old 07-30-2008
not sure whether understand your question correctly.

Anyway, hope below one can make some sense.

Code:
open (FH,"<a");
while(<FH>){
	@arr=split(" ",$_);
	$hash{$arr[2]}=$_;
}
close(FH);
open (FH,"<b");
while(<FH>){
	@arr=split(" ",$_);
	if(exists($hash{$arr[2]})){
		print $hash{$arr[2]};
		print $_;
	}
}
close(FH);

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Diff of 2 files using perl

Assitance needed in checking file difference using a perl script Below is the example I wanted a report saying file2 has an update of new value. File1 old date Alpha 156 Beta 255 Gama 6987 segma 4578 File2 new date Alpha 156 Beta 255 Gama 1000 (3 Replies)
Discussion started by: ajayram_arya
3 Replies

2. Shell Programming and Scripting

Compare two CSV files and put the difference in third file with line no,field no and diff value.

I am having two csv files i need to compare these files and the output file should have the information of the differences at the field level. For Example, File 1: A,B,C,D,E,F 1,2,3,4,5,6 File 2: A,C,B,D,E,F 1,2,4,5,5,6 out put file: (12 Replies)
Discussion started by: karingulanagara
12 Replies

3. Shell Programming and Scripting

Shell script to compare ,diff and remove betwen 2 files

Hi Friends Need your expertise. Command to check the difference and compare 2 files and remove lines . example File1 is master copy and File2 is a slave copy . whenever i change, add or delete a record in File1 it should update the same in slave copy . Can you guide me how can i accomplish... (3 Replies)
Discussion started by: ajayram_arya
3 Replies

4. Shell Programming and Scripting

Need Perl script to compare two CSV files

Need perl script to compare the two CSV files and and give out put in CSV format File MsPMTP.csv File ProfileNames.csv MsPMTP.csv is having lines like below JBL_VIJ_A_A962/r01sr4sl12/port#01-#13-Au4P-4c-TMi-PMNETR15 JBL_VIJ_A_A962/r01sr4sl12/port#01-#13-Au4P-4c-TMi-PMFETR15... (9 Replies)
Discussion started by: sreedhargouda
9 Replies

5. Shell Programming and Scripting

Perl script to compare two files

hi, As such I am new to perl on google search I found a code for Perl script to compare two files and print differences between them and instead of prinintg I want to store the diff. in a outputfile so can sombody provide assistance upon this from where can I edit in script to store the diff in... (1 Reply)
Discussion started by: dinesh.4126
1 Replies

6. Shell Programming and Scripting

Using Diff to Compare 2 files

Hi I've been trying various methods that I have found online with regards to comparing 2 files using the diff command. Nothing seems to work. The problem is that I'm not too familiar with the proper syntax. Can you please assist me. Here is my script: #!/bin/bash awk -F',' -v file1="$1"... (9 Replies)
Discussion started by: ladyAnne
9 Replies

7. Shell Programming and Scripting

compare two files, diff the result

Hi Everyone, 1.txt 00:01:01 asdf 00:33:33 1234 00:33:33 0987 00:33:33 12 00:33:33 444 2.txt vvvv|ee 444|dd33|ee dddd|ee 12|ee 3ciur|fdd the output should be: (6 Replies)
Discussion started by: jimmy_y
6 Replies

8. Shell Programming and Scripting

add coloum value based on first coloum

Hi i have a file and i want to add the coloum values like 9067 is coming twice here so o/p willbe like 9067,sum of 3rd coloum,sum of 4th coloum 9015 is coming 0nce then o/p 9015,15,6 sum is happening based on first coloum it will add all the third coloum and 4th coloum value where first... (1 Reply)
Discussion started by: aaysa123
1 Replies

9. Shell Programming and Scripting

Compare two files and output diff to third file

I have made several attempts to read two files of ip addresses and eliminate records from file1 that are in file2. My latest attempt follows. Everything works except my file3 is exactly the same as file1 and it should not be. # !/usr/bin/bash # # NoInterfaces # Utility will create a file... (8 Replies)
Discussion started by: altamaha
8 Replies
Login or Register to Ask a Question