how to compare two columns in two files? 
Author Message
 how to compare two columns in two files?

Hello

I have the following problem:

I have two files. Each file has only 3 collumns, let's say:

file 1           file 2
X1 Y1 Z1  X2 Y2 Z2

Now I want to print  the complete rows from both files where

X1 = X2 and Y1 != Y2

Any idea on how to do this? Awk of bourneshell is ok

Thanx for any input.
R.
BTW I can merge the files to one file, but can _that_ be helpfull??



Sun, 05 Oct 2003 01:48:25 GMT  
 how to compare two columns in two files?

Do you recognise this? Just mailed the ng and found some other info?

It just occured to me that there are also lines that need to be printed
where the x value's are the same, but differ in Y value's. So, also in
one file itself :-(
Hope this will not increase the difficulty - help! :-)

Regards,
???
 \_/

Quote:

> Hello

> I have the following problem:

> I have two files. Each file has only 3 collumns, let's say:

> file 1           file 2
> X1 Y1 Z1  X2 Y2 Z2

> Now I want to print  the complete rows from both files where

> X1 = X2 and Y1 != Y2

> Any idea on how to do this? Awk of bourneshell is ok

> Thanx for any input.
> R.
> BTW I can merge the files to one file, but can _that_ be helpfull??



Sun, 05 Oct 2003 01:55:57 GMT  
 how to compare two columns in two files?
what about doing it in ordinary shell:
    exec 3< file2
    cat file1 | while read x1 y1 z1; do
        read -u3 x2 y2 z2
        if [[ $x1 = $x2 && $y1 != $y2 ]]; then
            echo $x1 $y1 $z1
            echo $x2 $y2 $z2
        fi
    done

ben

Quote:

> Hello

> I have the following problem:

> I have two files. Each file has only 3 collumns, let's say:

> file 1           file 2
> X1 Y1 Z1  X2 Y2 Z2

> Now I want to print  the complete rows from both files where

> X1 = X2 and Y1 != Y2

> Any idea on how to do this? Awk of bourneshell is ok

> Thanx for any input.
> R.
> BTW I can merge the files to one file, but can _that_ be helpfull??



Sun, 05 Oct 2003 02:19:26 GMT  
 how to compare two columns in two files?
why is there only one message in this group ?


Quote:
> what about doing it in ordinary shell:
>     exec 3< file2
>     cat file1 | while read x1 y1 z1; do
>         read -u3 x2 y2 z2
>         if [[ $x1 = $x2 && $y1 != $y2 ]]; then
>             echo $x1 $y1 $z1
>             echo $x2 $y2 $z2
>         fi
>     done

> ben


> > Hello

> > I have the following problem:

> > I have two files. Each file has only 3 collumns, let's say:

> > file 1           file 2
> > X1 Y1 Z1  X2 Y2 Z2

> > Now I want to print  the complete rows from both files where

> > X1 = X2 and Y1 != Y2

> > Any idea on how to do this? Awk of bourneshell is ok

> > Thanx for any input.
> > R.
> > BTW I can merge the files to one file, but can _that_ be helpfull??



Sun, 05 Oct 2003 02:52:29 GMT  
 how to compare two columns in two files?

Quote:

> Hello

> I have the following problem:

> I have two files. Each file has only 3 collumns, let's say:

> file 1           file 2
> X1 Y1 Z1  X2 Y2 Z2

> Now I want to print  the complete rows from both files where

> X1 = X2 and Y1 != Y2

> Any idea on how to do this? Awk of bourneshell is ok

> Thanx for any input.
> R.
> BTW I can merge the files to one file, but can _that_ be helpfull??

If I understand the problem ;
1)  For each possible value of the first word;
2)     If there are two or more diffenent values of the second word,
then
           print the line.
3)  The lines could be either in file1 or file2.

Solution:
First combine the two files and rearrainge so similiar lines are
together.
Then look for changes.

In Unix;
cat file1 file2 | sort | gawk -f copm2col.awk

In MSDos;
type file1 file2 | sort | gawk -f copm2col.awk

Where comp2col.awk is;

x != $1{if (flg){for (i = 1; i <= cnt; i++){print L[i]}};
           flg = cnt = 0; y =$2}
y != $2{flg = 1}
{L[++cnt] = $0; x = $1; y = $2}
END{if (flg){for (i = 1; i <= cnt; i++){print L[i]}}}

The first line test for a change of "x", the first word;
If the flag "flg" is set, it prints the "remembered" lines,
Then it resets the flag and count of lines to remember.

The second line sets the flag when "y", the second word changes.
It will NOT set the flag on the first line with any value of x.

The third line increments the cnt, and remembers the line in array "L",
and remembers the first and second words as "x" and "y".

The last line prints the output for the last value of "x".

Good luck

Herb Wollman



Sun, 05 Oct 2003 10:52:58 GMT  
 how to compare two columns in two files?

Quote:

>why is there only one message in this group ?

A problem with your news server?  I see 26 messages posted in the last
two days.


Mon, 06 Oct 2003 14:08:34 GMT  
 how to compare two columns in two files?
Because he just entered a parallel news group in the Twilight Zone.... :-)
Quote:


> >why is there only one message in this group ?

> A problem with your news server?  I see 26 messages posted in the last
> two days.



Mon, 06 Oct 2003 22:09:19 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. read two sets of data from two spread sheet files to two arrays problem

2. Two strings in two columns in Exel?

3. Merge selected columns from two different files into another file

4. merging 2 columns from two files in one file

5. Writing two equal files without creating two files in dct

6. Two files, two linking keys

7. reading two files and comparing them

8. comparing two files

9. : Comparing facts from two files

10. How to compare two files line-by-line

11. Comparing two databases.

12. Comparing fields in a two records

 

 
Powered by phpBB® Forum Software