Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Search Forums:



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

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-05-2012
Registered User
 

Join Date: Dec 2010
Location: Mumbai
Posts: 29
Thanks: 15
Thanked 0 Times in 0 Posts
Compare fields in files

Hi,

I need the most efficient way of comparing the following and arriving at the result

I have a file which has entries like,

File1:

1|2|5|7|8|2|3|6|3|1

File2:

1|2|3|1|2|7|9|2

I need to compare the entries in these two file with those of a general file,

1|2|3|5|2|5|6|9|3|1 ---> constant file

My need is to calculate the similar entries between file1 and constant file and the same for file2 and so on.
Only matching.

In my case,
the compare with file1 and constant file will account to: "2"
similarly for file2 it will be: "4"

Please help me to implement this in an easy and effective way.

thanks.
Sponsored Links
    #2  
Old 02-05-2012
Registered User
 

Join Date: Oct 2010
Location: Bilbao, Spain
Posts: 574
Thanks: 8
Thanked 157 Times in 155 Posts
Quote:
the compare with file1 and constant file will account to: "2"
Why this result?

Regards,
Birei
Sponsored Links
    #3  
Old 02-05-2012
agama agama is offline Forum Advisor  
Always Learning
 

Join Date: Jul 2010
Location: earth>US>UTC-5
Posts: 1,210
Thanks: 86
Thanked 405 Times in 389 Posts
The result seems to be the number of fields (pipe separated) that match the constant file. The question I have is: is there just one record in the constant file, or multiple records?
    #4  
Old 02-05-2012
Registered User
 

Join Date: Jun 2008
Location: Singapore
Posts: 186
Thanks: 3
Thanked 40 Times in 39 Posts
Assuming agama's assumption is correct.
The constant file is like this

Code:
1|2|4|6|5|7|2|2|9|8

Here is the code:

Code:
paste constant file1 file2 |
awk '
BEGIN {
        FS="\t"
        sep="|"
}
{
        nc=split($1, c, sep)
        nf1=split($2, f1, sep)
        nf2=split($3, f2, sep)

        k=0
        for (i=1; i<=nf1; ++i ) {
                if ( f1[i] == c[i] ) { k+=1 }
        }
        print "File1:", k

        k=0
        for (i=1; i<=nf2; ++i ) {
                if ( f2[i] == c[i] ) { k+=1 }
        }
        print "File2:", k

}'

Sponsored Links
    #5  
Old 02-06-2012
Registered User
 

Join Date: Feb 2012
Posts: 4
Thanks: 3
Thanked 0 Times in 0 Posts
Is there multiple files like file1,2,3 and so on?
Is there a way to do this in python.

Thanks in advance,
Uma
Sponsored Links
    #6  
Old 02-06-2012
ctsgnb ctsgnb is offline Forum Advisor  
Registered User
 

Join Date: Oct 2010
Location: France
Posts: 2,529
Thanks: 64
Thanked 521 Times in 500 Posts
Assuming - as per your example - that all your files contain 1 line :


Code:
$ cat f1
1|2|5|7|8|2|3|6|3|1
$ cat f2
1|2|3|1|2|7|9|6
$ cat f3
1|0|5|0|8|2|1|1|1
$ nawk -F\| 'NR==FNR{n=NF;split($0,A,"\|");next}FNR==1{d=0}{for(i=0;++i<=n;)if($i==A[i]) ++d;print d}' f*
3
4
$ nawk -F\| 'NR==FNR{n=NF;split($0,A,"\|");next}FNR==1{d=0}{for(i=0;++i<=n;)if($i==A[i]) ++d;print d}' f1 f2 f3
3
4
$

The Following User Says Thank You to ctsgnb For This Useful Post:
pradebban (02-06-2012)
Sponsored Links
    #7  
Old 02-06-2012
Registered User
 

Join Date: Feb 2012
Posts: 4
Thanks: 3
Thanked 0 Times in 0 Posts
Is f1 - constant here?

I think the need is to compare f1 with f2,f3,f4 and so on.. and get the matched count in R2,R3 and so on.

f1 -> 1|2|3|4
f2 -> 1|2|3|1
f3 -> 1|3|3|2

so the result must be,

R2 -> 3 (as 1,2, and 3 matches)
R4 -> 2 (as 1 and 3 matches)

Am i correct here!
Sponsored Links
Reply

Tags
compare

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
compare fields in different files yassinegoth Shell Programming and Scripting 6 12-07-2011 08:09 PM
AWK Compare files, different fields, output stacky69 Shell Programming and Scripting 4 11-08-2010 07:54 AM
Compare fields in 2 files using AWK rashmisb Shell Programming and Scripting 5 10-21-2010 12:20 PM
Compare two files based on values of fields. Hangman2 Shell Programming and Scripting 4 10-21-2010 10:43 AM
Compare 2 files through multiple fields newinawk Shell Programming and Scripting 4 06-12-2008 04:34 PM



All times are GMT -4. The time now is 04:29 AM.