Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

compare hash keys originating from different files 1

Status
Not open for further replies.

LinguaFranca

Technical User
Jan 4, 2005
18
0
0
NL
Hello,

I try to compare the hash keys of two individual files.

The content of file1 is (tab separated columns):
Carmen !
Poekie
This is a test .
Winter ?
Test !

The content of file2 is (tab separated columns):
this is a test test.res, bla.frm
carmen test.res, carmen,txt
poekie poeki.frm

The desired output should be:
This is a test . test.res, bla.frm
Carmen ! test.res, carmen.txt
Poekie poekie.frm
NO MATCH: Winter ?
NO MATCH: TEST !

My problem is that the while construct loops through file1 without starting the comparison line by line. It only takes the last line of file1. This is the current output:

NO MATCH: Test !
NO MATCH: Test !
NO MATCH: Test !

Please, be patient, I am still learning Perl. Hope you can help me.

Here's the code snippet I wrote:
Code:
#!usr/bin/perl
use locale;

open FILE1, "file1.ans" or die;
open FILE2, "file2.ans" or die;

while (<FILE1>) {
	chomp;
	($orig, $punct)=(split (/\t/), $_) [0,1];
	$orig{$orig}=$_;
	}
close FILE1;	
	
while (<FILE2>) {
	chomp;
	($klein, $files) = split (/\t/);
	$kleinarr{$klein}=$files;
	if (lc($orig) eq $klein) {
		print "$orig{$orig}\t$kleinarr{$klein}\n";
		} else {
			print "NO MATCH:\t$orig\t$punct\n";
			}
}
close FILE2;
exit;
 
Code:
BEGIN { FS=OFS="\t" }

NR==FNR { a[tolower($1)] = $2 ; next }

{ if ( tolower($1) in a )
    print $0, a[tolower($1)]
  else
    print "NO MATCH: " $0
}
Run with [tt]awk -f 2files.awk file2 file1[/tt]
 
Thanks for this very quick reply.
Your awk script has nearly tackled my problem. The output of your script is:

your script output:
this is a test test.res, bla.frm .
carmen test.res, carmen.txt !
poekie poeki.frm

which is good. All I need now is the original spelling from file1.ans. So the desired output would be:

This is a test test.res, bla.frm .
Carmen test.res, carmen.txt !
Poekie peoki.frm

Hope you have an idea how to solve this.
 
instead of
Code:
    print $0, a[tolower($1)]
try
Code:
    print $0, a[$1]

Just clutching though, but its bound to be something like that
--Paul


cigless ...
 
Code:
#![COLOR=blue]/[/color]usr/bin/perl [COLOR=green]-w[/color]
[COLOR=green]
use warnings;
use strict;
[/color]
use locale;

open FILE1, "file1.ans" or die;
open FILE2, "file2.ans" or die;

[COLOR=green]my %orig;
my %orig_from_lc;
my %to_be_used;[/color]

while (<FILE1>) {
    chomp;
    [COLOR=green]my [/color]($orig, $punct)=[COLOR=blue]split /\t/[/color];
    $orig{$orig}=[COLOR=blue]$punct[/color];
    [COLOR=blue]$orig_from_lc{lc $orig} = $orig;
    $to_be_used{$orig} = 1;[/color]
    }
close FILE1;    
    
while (<FILE2>) {
    chomp;
    [COLOR=green]my [/color]($klein, $files) = split (/\t/);
    [COLOR=red][s]# $kleinarr{$klein}=$files;[/s][/color]
    if ([COLOR=blue]exists $orig_from_lc{$klein}[/color]) {
        [COLOR=blue]my $orig = $orig_from_lc{$klein};
        delete $to_be_used{$orig};[/color]
        print "[COLOR=blue]$orig [/color]$orig{$orig}\t[COLOR=blue]$files[/color]\n";
        }
}
close FILE2;

[COLOR=blue]
for (keys %to_be_used) {
    print "NO MATCH:\t$_\t$orig{$_}\n";
}[/color]

--------------------

Denis
 
PERFECT! The revised perl script did the job. Many thanks! Carmen
 
your script output:
this is a test test.res, bla.frm .
carmen test.res, carmen.txt !
poekie poeki.frm
The result of
Code:
awk -f 2files.awk file2 file1
is
[tt]
Carmen ! test.res, carmen,txt
Poekie poeki.frm
This is a test . test.res, bla.frm
NO MATCH: Winter ?
NO MATCH: Test !
[/tt]
You must have run the command
[tt]
awk -f 2files.awk file1 file2
[/tt]
 
your script output:
this is a test test.res, bla.frm .
carmen test.res, carmen.txt !
poekie poeki.frm

The result of
Code:
awk -f 2files.awk file2 file1
is
[tt]
Carmen ! test.res, carmen,txt
Poekie poeki.frm
This is a test . test.res, bla.frm
NO MATCH: Winter ?
NO MATCH: Test !
[/tt]
You must have run the command
[tt]
awk -f 2files.awk file1 file2
[/tt]
 
Stupid, me! I have to apologize. As you have pointed out I didn't type in the correct sequence of file names on the command line. [sadeyes] Many thanks to all of you, now I have two scripts I can use in different contexts to get the results I need. Carmen
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top