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:
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.
Also I don't really understand why are you want to merge C1 with C2?
Anyway, here is how it can be done:
awk ' FNR == NR {
A[++j] = $0
next
} {
B[$0] = $0
} END {
for ( i = 1; i <= j; i++)
{
for ( v in B )
{
print A, B[v];
}
}
} ' OFS=, C1match.txt C2non_match.txt
#!/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);