Compare 2 files and print matches and non-matches in separate files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare 2 files and print matches and non-matches in separate files
# 1  
Old 03-09-2013
Compare 2 files and print matches and non-matches in separate files

Hi all,

I have two files, chap.txt and complex.txt.

chap.txt looks like this:
Code:
a
d
l
m
r
k

complex.txt looks like this:
Code:
a c d e l m n j
a d l p q r
c p r m
.........
.........

What I need is to search for all lines (single column) of chap.txt into each line of complex.txt and generate separate files for matching and non-matching strings, like this:

Code:
C1match.txt       C1non_match.txt
a                           c
d                           e
l                           n
m                           j

Similarly, 2 files for the next line:
C2match.txt       C2non_match.txt
a                           p
d                           q
l
r

If there is no match in a line of complex.txt and lines of chap.txt, there will be no output files in such cases. So, if total number of lines in the complex.txt is N and there is always a match (which won't be actually), total possible output files would be Nx2.

I hope, I explained my problem clearly.

Thanks and Regards
# 2  
Old 03-09-2013
Code:
awk ' BEGIN {
        c = 1
} FNR == NR {
        A[$0] = $0
        next
} {
        M_F = "C" c "match.txt"
        N_F = "C" c "non_match.txt"

        for ( j = 1; j <= NF; j++ )
        {
                if ( $j in A)
                        print $j > M_F
                else
                        print $j > N_F
        }
        c += 1
        close(M_F)
        close(N_F)
} ' chap.txt complex.txt

This User Gave Thanks to Yoda For This Post:
# 3  
Old 03-09-2013
Thanks bipinajith. It just that its printing non_match files for those combinations also where there is no match.

Also, Could you please help me in making files having combination pairs of non_match with match files. For example:
Code:
C1match.txt
a
d
l
m

C2non_match.txt
p
q

C1matchC2non_match.txt
a,p
a,q
d,p
d,q
l,p
l,q
m,p
m,q

I really appreciate your time and help.

Thanks
# 4  
Old 03-09-2013
Quote:
Originally Posted by AshwaniSharma09
Thanks bipinajith. It just that its printing non_match files for those combinations also where there is no match.
I didn't quite understand this statement.
Quote:
Originally Posted by AshwaniSharma09
C1matchC2non_match.txt
a,p
a,q
d,p
d,q
l,p
l,q
m,p
m,q
Also I don't really understand why are you want to merge C1 with C2?

Anyway, here is how it can be done:
Code:
awk ' FNR == NR {
        A[++j] = $0
        next
} {
        B[$0] = $0
} END {
        for ( i = 1; i <= j; i++)
        {
                for ( v in B )
                {
                        print A[i], B[v];
                }
        }
} ' OFS=, C1match.txt C2non_match.txt

# 5  
Old 03-10-2013
For your first request, you might want to give this a shot:
Code:
awk     'NR==1          {Pat = "["}
         NR==FNR        {Pat = Pat $1;next}
         FNR==1         {Pat=Pat"]"}
         {for (i=1; i<=NF; i++)
            if ($i~Pat) print $i > "C"FNR"match.txt"
              else      print $i > "C"FNR"nonmatch.txt" }
        ' file1 file2

This User Gave Thanks to RudiC For This Post:
# 6  
Old 03-11-2013
How about this ?
Code:
 
#!/usr/bin/perl
use strict;
my $file1=shift;
my $file2=shift;
my %seen;
open (FH,"$file1") or die "can not open file $file1 - $! \n";
while (<FH>) {
        chomp;
        $seen{$_}++;
}
close(FH);
my @flds;
open (FH2,"$file2") or die "can not open file $file2  - $! \n";
while (<FH2>) {
        my $match="C" . $. . "match.txt";
        my $unmatch="C" . $. . "unmatch.txt";
        open (FM,">$match");
        open (FU,">$unmatch");
        @flds=split;
        foreach (@flds) {
        if (exists $seen{$_} ) {
                print FM "$_\n";
        } else {
                print FU "$_\n";
        }
        }
        close(FM);
        close(FU);
}
close(FH2);

Invocation
Code:
 
perl colmatch.pl chap.txt complex.txt

This User Gave Thanks to pravin27 For This Post:
# 7  
Old 03-12-2013
Thanks Guys. pravin27s perl script runs fine too. I could see some problem with RudiC's scipt though. Thanks once again. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

BASH - Compare 2 Files, Output All Matches

This is probably rehash but I did look. :rolleyes: I want a bash script that will take Item 1 in File1, traverse all lines in File2 and output if a match exists. Continuing the pattern recursively, Item2, File1, traverse all lines in File2 for a match, continue this pattern until all lines... (6 Replies)
Discussion started by: rcbarr2014
6 Replies

2. UNIX for Dummies Questions & Answers

Print only '+' or '-' if string matches (two files)

I would like to add two additional conditions to the actual code I have: print '+' if in File2 field 5 is greater than 35 and also field 7 is grater than 90. while read -r line do grep -q "$line" File2.txt && echo "$line +" || echo "$line -" done < File1.txt ' Input file 1: ... (5 Replies)
Discussion started by: bernardo.bello
5 Replies

3. Shell Programming and Scripting

Merge two columns from two files into one if another column matches

I have two text files that look something like this: A:B:C 123 D:E:F 234 G:H:I 345 J:K:L 123 M:N:O 456 P:Q:R 567 A:B:C 456 D:E:F 567 G:H:I 678 J:K:L 456 M:N:O 789 P:Q:R 890 I want to find the line where the first column matches and then combine the second columns into a single... (8 Replies)
Discussion started by: pbluescript
8 Replies

4. Shell Programming and Scripting

Compare two text files and print matches

Hi, I am looking for a way to compare two text files and print the matches. For example; File1.txt 89473036 78474384 48948408 95754748 47849030 File2.txt 47849030 46730356 16734947 78474384 36340047 Output: (11 Replies)
Discussion started by: lewk
11 Replies

5. Shell Programming and Scripting

Comparing the matches in two files using awk when both files have their own field separators

I've two files with data like below: file1.txt: AAA,Apples,123 BBB,Bananas,124 CCC,Carrot,125 file2.txt: Store1|AAA|123|11 Store2|BBB|124|23 Store3|CCC|125|57 Store4|DDD|126|38 So,the field separator in file1.txt is a comma and in file2.txt,it is | Now,the output should be... (2 Replies)
Discussion started by: asyed
2 Replies

6. Shell Programming and Scripting

If string matches within 2 files, delete one file.

I have a directory with a large # of files and in each file I am looking to match a string in one file with a string in the subsequent n file(s). If there is a match between a string in one file and a string in the next n file(s) then delete the subsequent duplicate file(s). Here is sample input: ... (2 Replies)
Discussion started by: sitney
2 Replies

7. Shell Programming and Scripting

Joining columns from two files, if the key matches

I am trying to join/paste columns from two files for the rows with matching first field. Any help will be appreciated. Files can not be sorted and may not have all rows in both files. Thanks. File1 aaa 111 bbb 222 ccc 333 File2 aaa sss mmmm ccc kkkk llll ddd xxx yyy Want to... (1 Reply)
Discussion started by: sk_sd
1 Replies

8. Shell Programming and Scripting

looking for files and modify if size matches

hi there. I'm at SunOS 5.9 At my new job i'm using UNIX, and it's my first time. i'm trying to make a script for: -find files with a name passed to it as parameter -compare results with file size passed as parameter too -when comparison's true --> move file -if not--> make nothing ... (3 Replies)
Discussion started by: viko
3 Replies

9. Shell Programming and Scripting

Looking for a string in files and reporting matches

Can someone please help me figure out what the command syntax I need to use is? Here is what I am wanting to do. I have hundreds of thousands of files I need to look for a specific search string in. These files are spread across multiple subdirectories from one main directory. I would like... (4 Replies)
Discussion started by: btrotter
4 Replies

10. UNIX for Dummies Questions & Answers

Comparing two files and noting matches

I have two files. One contains names and another file (66 MB, ASCII format) contains details of persons. How do I compare the names in the first file with the second file and write the matches to a third file. I would prefer this to be solved in UNIX or VB. Thanks. (2 Replies)
Discussion started by: augustinep
2 Replies
Login or Register to Ask a Question