reading two files and comparing them 
Author Message
 reading two files and comparing them

I have two files with each of them containing two columns(fields) of
data separated by a space. I need to read in both files into an array
I guess and compare the first column(field) of the first file with the
first column(field) of the second file. I have an example of the two
files below:

FileA                FileB

rich fdfewfew        john sdsd32das
tom 324dfdsfs        bill sad213dsd
bill fdd34dsd        ray dfdf3fdd
mary dsdfs3r         steve fdfr4rfds
steve fdsf3zf        rich sddsf3r43

I need the output from the comparison to show what FileA has the FileB
doesn't and what FileB has that FileA doesn't based on the comparison
of column(field one) only.

Example output:

Output from FileA      Output from FileB

Tom 324dfdsfs          john sdsd32das
mary dsdfs3r           ray dfdf3fdd

So basically, how do I read in two files and compare the first field
of each file against each other and produce output using AWK.

Thanks



Mon, 03 Oct 2005 04:18:43 GMT  
 reading two files and comparing them

X I have two files with each of them containing two columns(fields) of
X data separated by a space. I need to read in both files into an array
X I guess and compare the first column(field) of the first file with the
X first column(field) of the second file. I have an example of the two
X files below:
X
X FileA                FileB
X
X rich fdfewfew        john sdsd32das
X tom 324dfdsfs        bill sad213dsd
X bill fdd34dsd        ray dfdf3fdd
X mary dsdfs3r         steve fdfr4rfds
X steve fdsf3zf        rich sddsf3r43
X
X I need the output from the comparison to show what FileA has the FileB
X doesn't and what FileB has that FileA doesn't based on the comparison
X of column(field one) only.
X
X Example output:
X
X Output from FileA      Output from FileB
X
X Tom 324dfdsfs          john sdsd32das
X mary dsdfs3r           ray dfdf3fdd
X
X So basically, how do I read in two files and compare the first field
X of each file against each other and produce output using AWK.
X
X Thanks

This may not be exactly what you want, but it is a start.

#!/bin/ksh
awk '
    FILENAME != current_file { pass++; current_file = FILENAME }
    pass == 1 { fileA[$1] = $2 }
    pass == 2 { fileB[$1] = $2 }
    END {
        for( name in fileA ) {
            if ( name in fileB ) {
                delete fileA[name]
                delete fileB[name]
            }
        }
        print "Output from FileA"
        for( name in fileA ) {
            print name, fileA[name]
        }
        print ""
        print "Output from FileB"
        for( name in fileB ) {
            print name, fileB[name]
        }
    }
' fileA fileB

                                        Bob Harris



Mon, 03 Oct 2005 05:37:00 GMT  
 reading two files and comparing them

Quote:

> I have two files with each of them containing two columns(fields) of
> data separated by a space. I need to read in both files into an array
> I guess and compare the first column(field) of the first file with the
> first column(field) of the second file. I have an example of the two
> files below:

> FileA                FileB

> rich fdfewfew        john sdsd32das
> tom 324dfdsfs        bill sad213dsd
> bill fdd34dsd        ray dfdf3fdd
> mary dsdfs3r         steve fdfr4rfds
> steve fdsf3zf        rich sddsf3r43

> I need the output from the comparison to show what FileA has the FileB
> doesn't and what FileB has that FileA doesn't based on the comparison
> of column(field one) only.

> Example output:

> Output from FileA      Output from FileB

> Tom 324dfdsfs          john sdsd32das
> mary dsdfs3r           ray dfdf3fdd

> So basically, how do I read in two files and compare the first field
> of each file against each other and produce output using AWK.

Bob Harris seems to have answered your question, but
let me just add that the Unix "join" command (available
for other OSs too) already does this.

John.



Mon, 03 Oct 2005 06:53:40 GMT  
 reading two files and comparing them

Quote:



> X I have two files with each of them containing two columns(fields) of
> X data separated by a space. I need to read in both files into an array
> X I guess and compare the first column(field) of the first file with the
> X first column(field) of the second file. I have an example of the two
> X files below:
> X
> X FileA                FileB
> X
> X rich fdfewfew        john sdsd32das
> X tom 324dfdsfs        bill sad213dsd
> X bill fdd34dsd        ray dfdf3fdd
> X mary dsdfs3r         steve fdfr4rfds
> X steve fdsf3zf        rich sddsf3r43
> X
> X I need the output from the comparison to show what FileA has the FileB
> X doesn't and what FileB has that FileA doesn't based on the comparison
> X of column(field one) only.
> X
> X Example output:
> X
> X Output from FileA      Output from FileB
> X
> X Tom 324dfdsfs          john sdsd32das
> X mary dsdfs3r           ray dfdf3fdd
> X
> X So basically, how do I read in two files and compare the first field
> X of each file against each other and produce output using AWK.
> X
> X Thanks

> This may not be exactly what you want, but it is a start.

> #!/bin/ksh
> awk '
>     FILENAME != current_file { pass++; current_file = FILENAME }
>     pass == 1 { fileA[$1] = $2 }
>     pass == 2 { fileB[$1] = $2 }
>     END {
>         for( name in fileA ) {
>             if ( name in fileB ) {
>                 delete fileA[name]
>                 delete fileB[name]
>             }
>         }
>         print "Output from FileA"
>         for( name in fileA ) {
>             print name, fileA[name]
>         }
>         print ""
>         print "Output from FileB"
>         for( name in fileB ) {
>             print name, fileB[name]
>         }
>     }
> ' fileA fileB

>                                         Bob Harris

Bob, I think I forgot to mention that fileA & fileB are actually
strings created within the beginning of the Korn Shell script. Ex:

fileA='bdf | tail +2 | sort | some sed substitution and deletion'
fileB='cat some file | sort | some sed substitution and deletion'

The reason I'm doing it this way, is that I don't want to create any
files whatsoever.

Could I still use your awk example to compare the strings or is there
some modifications that need to be made?? e.g. awk -v ??



Tue, 04 Oct 2005 02:09:19 GMT  
 reading two files and comparing them





X >
X > X I have two files with each of them containing two columns(fields) of
X > X data separated by a space. I need to read in both files into an array
X > X I guess and compare the first column(field) of the first file with the
X > X first column(field) of the second file. I have an example of the two
X > X files below:
X > X
X > X FileA                FileB
X > X
X > X rich fdfewfew        john sdsd32das
X > X tom 324dfdsfs        bill sad213dsd
X > X bill fdd34dsd        ray dfdf3fdd
X > X mary dsdfs3r         steve fdfr4rfds
X > X steve fdsf3zf        rich sddsf3r43
X > X
X > X I need the output from the comparison to show what FileA has the FileB
X > X doesn't and what FileB has that FileA doesn't based on the comparison
X > X of column(field one) only.
X > X
X > X Example output:
X > X
X > X Output from FileA      Output from FileB
X > X
X > X Tom 324dfdsfs          john sdsd32das
X > X mary dsdfs3r           ray dfdf3fdd
X > X
X > X So basically, how do I read in two files and compare the first field
X > X of each file against each other and produce output using AWK.
X > X
X > X Thanks
X >
X > This may not be exactly what you want, but it is a start.
X >
X > #!/bin/ksh
X > awk '
X >     FILENAME != current_file { pass++; current_file = FILENAME }
X >     pass == 1 { fileA[$1] = $2 }
X >     pass == 2 { fileB[$1] = $2 }
X >     END {
X >         for( name in fileA ) {
X >             if ( name in fileB ) {
X >                 delete fileA[name]
X >                 delete fileB[name]
X >             }
X >         }
X >         print "Output from FileA"
X >         for( name in fileA ) {
X >             print name, fileA[name]
X >         }
X >         print ""
X >         print "Output from FileB"
X >         for( name in fileB ) {
X >             print name, fileB[name]
X >         }
X >     }
X > ' fileA fileB
X >
X >                                         Bob Harris
X
X Bob, I think I forgot to mention that fileA & fileB are actually
X strings created within the beginning of the Korn Shell script. Ex:
X
X fileA='bdf | tail +2 | sort | some sed substitution and deletion'
X fileB='cat some file | sort | some sed substitution and deletion'
X
X The reason I'm doing it this way, is that I don't want to create any
X files whatsoever.
X
X Could I still use your awk example to compare the strings or is there
X some modifications that need to be made?? e.g. awk -v ??

That is a bit different.  I might play games with piping the output of
both of your sed commands into awk with a unique string between each
file to trigger awk to switch to different arrays.  Note the use of ()
to group the sed commands into a single pipe into awk:

#!/bin/ksh
(bdf | tail +2 | sort | some sed substitution and deletion;

 cat some file | sort | some sed substitution and deletion
) | awk '

    pass == 0 { fileA[$1] = $2 }
    pass == 1 { fileB[$1] = $2 }
    END {
        for( name in fileA ) {
            if ( name in fileB ) {
                delete fileA[name]
                delete fileB[name]
            }
        }
        print "Output from FileA"
        for( name in fileA ) {
            print name, fileA[name]
        }
        print ""
        print "Output from FileB"
        for( name in fileB ) {
            print name, fileB[name]
        }
    }
'

                                        Bob Harris



Tue, 04 Oct 2005 05:25:36 GMT  
 reading two files and comparing them

Quote:




> > X I have two files with each of them containing two columns(fields) of
> > X data separated by a space. I need to read in both files into an array
> > X I guess and compare the first column(field) of the first file with the
> > X first column(field) of the second file. I have an example of the two
> > X files below:
> > X
> > X FileA                FileB
> > X
> > X rich fdfewfew        john sdsd32das
> > X tom 324dfdsfs        bill sad213dsd
> > X bill fdd34dsd        ray dfdf3fdd
> > X mary dsdfs3r         steve fdfr4rfds
> > X steve fdsf3zf        rich sddsf3r43
> > X
> > X I need the output from the comparison to show what FileA has the FileB
> > X doesn't and what FileB has that FileA doesn't based on the comparison
> > X of column(field one) only.
> > X
> > X Example output:
> > X
> > X Output from FileA      Output from FileB
> > X
> > X Tom 324dfdsfs          john sdsd32das
> > X mary dsdfs3r           ray dfdf3fdd
> > X
> > X So basically, how do I read in two files and compare the first field
> > X of each file against each other and produce output using AWK.
> > X
> > X Thanks

> > This may not be exactly what you want, but it is a start.

> > #!/bin/ksh
> > awk '
> >     FILENAME != current_file { pass++; current_file = FILENAME }
> >     pass == 1 { fileA[$1] = $2 }
> >     pass == 2 { fileB[$1] = $2 }
> >     END {
> >         for( name in fileA ) {
> >             if ( name in fileB ) {
> >                 delete fileA[name]
> >                 delete fileB[name]
> >             }
> >         }
> >         print "Output from FileA"
> >         for( name in fileA ) {
> >             print name, fileA[name]
> >         }
> >         print ""
> >         print "Output from FileB"
> >         for( name in fileB ) {
> >             print name, fileB[name]
> >         }
> >     }
> > ' fileA fileB

> >                                         Bob Harris

> Bob, I think I forgot to mention that fileA & fileB are actually
> strings created within the beginning of the Korn Shell script. Ex:

> fileA='bdf | tail +2 | sort | some sed substitution and deletion'
> fileB='cat some file | sort | some sed substitution and deletion'

> The reason I'm doing it this way, is that I don't want to create any
> files whatsoever.

> Could I still use your awk example to compare the strings or is there
> some modifications that need to be made?? e.g. awk -v ??

If your system supports a file-descriptor-directory /dev/fd
in ksh and bash you might try process-substitution:

untested

instead
awk 'awk-program'  fileA fileB

use
awk 'awk-program'  <(bdf | tail +2 | sort | some sed substitution and
deletion) \
                   <(sort some_file| some sed substitution and deletion)

Regards
Juergen



Wed, 05 Oct 2005 19:02:22 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. how to compare two columns in two files?

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

3. comparing two files

4. : Comparing facts from two files

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

6. how to read one file, print to two files

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

8. Comparing file objects: what is compared?

9. read two files

10. reading from two files, how?

11. NEWBIE TIP: Reading a file two different ways...from ENGSOL

12. reading two separate files

 

 
Powered by phpBB® Forum Software