The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Perl and Net::LDAP, objects and arrays query mikie Shell Programming and Scripting 3 08-22-2008 03:05 PM
Perl Arrays and Substituion popeye Shell Programming and Scripting 1 05-06-2008 05:29 AM
perl functions and arrays mercuryshipzz Shell Programming and Scripting 1 01-17-2008 03:18 PM
Compare two arrays in sh or compare two fields rijeshpp Shell Programming and Scripting 0 10-31-2007 02:47 AM
perl arrays crashnburn High Level Programming 2 08-05-2002 05:53 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-28-2008
Donkey25 Donkey25 is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 36
Perl - Compare 2 Arrays

Hi all,

I have the following script where the contents of file1 and file2 would be something like this:

file1:

56790,0,0,100998765
89756,0,0,100567876
867645,1,3,678777654

file2:

56790,0,0,100998765
65776,0,0,4766457890
+5896,0,0,675489876

What I then want to do is check if each of the 4 comma separated values on each line exists somewhere in the other file. So in the above case clearly the line that says 56790,0,0,100998765 is in both files so I'd like it to return true. I'd like to return false for any line that doesn't exist in the other file. The order of the values in each csv line is also important.

So far I have:

$input_file1="home/file1";
open(DAT1, $input_file1) || print "Could not open file1!";
@raw_data1=<DAT1>;
close(DAT1);

$input_file2="home/file2";
open(DAT2, $input_file2) || print "Could not open file2!";
@raw_data2=<DAT2>;
close(DAT2);

foreach $line (@raw_data1)
{
chop($line);
($v1,$v2,$v3,$v4)=split(/,/,$line);
if ($v1 ……

It's at this point that I get stuck. I've seen plenty of examples that compare just one value from each array but I want to make sure that all 4 of my comma seperated values exist in the other array in exactly the same order.

Any ideas?

Many thanks in advance.
  #2 (permalink)  
Old 08-28-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
The customary solution is to read the first file into a hash, and then if the hash key exists for a line you read from the second file, then it obviously existed in the first file.
  #3 (permalink)  
Old 08-28-2008
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,079
Hi,

Is it ok for you to use some other command.

try below one:

Code:
paste -d"," file1 file2 > temp.txt
awk '{
if ($1=$5 && $2==$6 && $3==$7 && $4==$8)
print true
}' temp.txt
rm temp.txt
  #4 (permalink)  
Old 08-29-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
That requires two passes over the input data, and uses a temporary file. You can do better with just awk.

Code:
awk 'NR == FNR { a[$0]++; next } { if (a[$0]) next; print }' file1 file2
... will print all the lines which exist in file2 but not in file1.

The same in Perl:

Code:
perl -nle 'if ($. == ++$i) { $a{$_}++; close ARGV if eof; next; }
print unless $a{$_}' file1 file2
  #5 (permalink)  
Old 09-01-2008
Donkey25 Donkey25 is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 36
Perfect, thanks.

I used the last Perl soultion which works brilliantly.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:25 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0