Comparing 2 files to create a third file 
Author Message
 Comparing 2 files to create a third file

Hi,
        I've got two files as follows:
   FILE 1                  FILE 2
user5:12345               user3
user2:234239              user4
user3:34908               user5
user1:234342              
user4:234993                  

and I want to use an awk script to generate a third file which
contains the information contained in FILE 1 for only those users
which are contained in FILE 2.  So my FILE 3 in this sample would
contain:
FILE 3
user3:34908
user4:234993
user5:12345

Additional info:
FILE 1 contains two fields separated with a ":"
FILE 2 only has one field
FILE 3 does not have to be in any particular order (ie. alphabetical)

If someone could provide me with a awk solution to this it would be
greatly appreciated.

Thanks,
Shane



Wed, 04 Jan 2006 02:02:49 GMT  
 Comparing 2 files to create a third file

Quote:

>Hi,
>    I've got two files as follows:
>   FILE 1                  FILE 2
>user5:12345               user3
>user2:234239              user4
>user3:34908               user5
>user1:234342              
>user4:234993                  

>and I want to use an awk script to generate a third file which
>contains the information contained in FILE 1 for only those users
>which are contained in FILE 2.  So my FILE 3 in this sample would
>contain:
>FILE 3
>user3:34908
>user4:234993
>user5:12345

>Additional info:
>FILE 1 contains two fields separated with a ":"
>FILE 2 only has one field
>FILE 3 does not have to be in any particular order (ie. alphabetical)

#!gawk
BEGIN {FS=":"}
ARGIND == 1 { x[$1] = $0;next }
$1 in x {print x[$1]}


Wed, 04 Jan 2006 02:14:33 GMT  
 Comparing 2 files to create a third file
Quote:

> Hi,
>    I've got two files as follows:
>    FILE 1                  FILE 2
> user5:12345               user3
> user2:234239              user4
> user3:34908               user5
> user1:234342              
> user4:234993                  

> and I want to use an awk script to generate a third file which
> contains the information contained in FILE 1 for only those users
> which are contained in FILE 2.  So my FILE 3 in this sample would
> contain:
> FILE 3
> user3:34908
> user4:234993
> user5:12345

> Additional info:
> FILE 1 contains two fields separated with a ":"
> FILE 2 only has one field
> FILE 3 does not have to be in any particular order (ie. alphabetical)

> If someone could provide me with a awk solution to this it would be
> greatly appreciated.

Your example data seems to suggest that you don't expect find the strings
in FILE 2 anywhere in FILE 1 except in the first field.  If this is the
case, awk is the Wrong Tool.

grep -f FILE_2 FILE_1 > FILE_3

                      Chris Mattern



Wed, 04 Jan 2006 04:06:14 GMT  
 Comparing 2 files to create a third file
Quote:


>> Hi,
>>     I've got two files as follows:
>>    FILE 1                  FILE 2
>> user5:12345               user3
>> user2:234239              user4
>> user3:34908               user5
>> user1:234342               user4:234993                  
>> and I want to use an awk script to generate a third file which
>> contains the information contained in FILE 1 for only those users
>> which are contained in FILE 2.  So my FILE 3 in this sample would
>> contain:
>> FILE 3
>> user3:34908
>> user4:234993
>> user5:12345

>> Additional info:
>> FILE 1 contains two fields separated with a ":"
>> FILE 2 only has one field
>> FILE 3 does not have to be in any particular order (ie. alphabetical)

>> If someone could provide me with a awk solution to this it would be
>> greatly appreciated.

> Your example data seems to suggest that you don't expect find the strings
> in FILE 2 anywhere in FILE 1 except in the first field.  If this is the
> case, awk is the Wrong Tool.

> grep -f FILE_2 FILE_1 > FILE_3

Hmmm, it occurs to me that you might get spurious matches by patterns in
FILE 2 matching substrings in FILE 1 ("user5" matching "user51").  That
can be avoided by appending a colon in the end of every line in FILE 2.

                       Chris Mattern



Wed, 04 Jan 2006 04:34:02 GMT  
 Comparing 2 files to create a third file

...

Quote:
>Your example data seems to suggest that you don't expect find the strings
>in FILE 2 anywhere in FILE 1 except in the first field.  If this is the
>case, awk is the Wrong Tool.

AWK is *never* the wrong tool, and is, believe it or not, the only that is
on-topic in this newsgroup.

When I read this item originally, I immediately realized that there was
some tricky grep solution where, with recent versions of grep, you could
use one file as the list of things to search for in the other file.  But
then I also realized that not once, and not twice, but several times in his
post, he said "I want an AWK solution to do ...".  And then I realized that
pretty soon, some dolt would give the grep solution, thereby demonstrating
his lack of reading comprehension.

Can I pick 'em, or what...



Wed, 04 Jan 2006 05:23:31 GMT  
 Comparing 2 files to create a third file

Quote:


> ...

>>Your example data seems to suggest that you don't expect find the strings
>>in FILE 2 anywhere in FILE 1 except in the first field.  If this is the
>>case, awk is the Wrong Tool.

> AWK is *never* the wrong tool,

Bull.  Everything is the wrong tool sometimes.  Awk is heavyweight; grep
is much more efficient if that's all you need.  Not too long ago I had
to adjust a script that was bringing a server to its knees, partially
due to many, many invocations of awk when it was not necessary.  Replacing
awk with a shell read help cut down on the script's processing demands.
It's also simpler in this case, since awk can't with one switch compare
each line with entire file of patterns.

Quote:
> and is, believe it or not, the only that is
> on-topic in this newsgroup.

True; I wouldn't have started a thread that had nothing to do with awk
in this group.  But I don't think pointing out that something besides
awk might be called for in this instance is out of line.

Quote:

> When I read this item originally, I immediately realized that there was
> some tricky grep solution where, with recent versions of grep, you could
> use one file as the list of things to search for in the other file.  But
> then I also realized that not once, and not twice, but several times in his
> post, he said "I want an AWK solution to do ...".  And then I realized that
> pretty soon, some dolt would give the grep solution, thereby demonstrating
> his lack of reading comprehension.

> Can I pick 'em, or what...

People--especially newbies unfamiliar with the entire toolbox available to
them in UNIX--can narrow down their solution parameters prematurely.  I
don't think it's out of line to point out another way.  If there is some
reason why he must use awk (he didn't give one), then obviously my advice
is no good to him.  But I don't think it's out of line to suggest the
fact that if he's not locked into awk, there's a Better Way.

                   Chris Mattern



Wed, 04 Jan 2006 05:54:18 GMT  
 Comparing 2 files to create a third file

Quote:


>...
>>Your example data seems to suggest that you don't expect find the strings
>>in FILE 2 anywhere in FILE 1 except in the first field.  If this is the
>>case, awk is the Wrong Tool.

>AWK is *never* the wrong tool, and is, believe it or not, the only that is
>on-topic in this newsgroup.

>When I read this item originally, I immediately realized that there was
>some tricky grep solution where, with recent versions of grep, you could
>use one file as the list of things to search for in the other file.  But
>then I also realized that not once, and not twice, but several times in his
>post, he said "I want an AWK solution to do ...".  And then I realized that
>pretty soon, some dolt would give the grep solution, thereby demonstrating
>his lack of reading comprehension.

>Can I pick 'em, or what...

+2 points for that

-3 points for not keeping it to yourself

Chuck Demas

P.S.  Thanks for playing  :-)

--
  Eat Healthy        |   _ _   | Nothing would be done at all,

  Die Anyway         |    v    | That no one could find fault with it.



Wed, 04 Jan 2006 06:56:42 GMT  
 Comparing 2 files to create a third file
Quote:
"Kenny McCormack" wrote...

..

Quote:
>#!gawk
>BEGIN {FS=":"}
>ARGIND == 1 { x[$1] = $0;next }
>$1 in x {print x[$1]}

While file2 could have more records than file1, the records in file1 will be
longer than those in file2. It seems to me that it might be a better idea to
store file2 rather than file1 in the array. Also, this could be done in a way
that's more general (arguable need) and doesn't require gawk.

$ cat script
!s { while ((getline s < pf) > 0) m[s]; ready = 1 }
$1 in m
$ nawk -F: -f script pf=file2 file1
user5:12345
user3:34908
user4:234993
$

--
Never attach files.
Snip unnecessary quoted text.
Never multipost (though crossposting is usually OK).
Don't change subject lines because it corrupts Google newsgroup archives.



Wed, 04 Jan 2006 08:44:41 GMT  
 Comparing 2 files to create a third file
Thanks Kenny.  Your script works great.

Shane


Quote:



>>Hi,
>>        I've got two files as follows:
>>   FILE 1                  FILE 2
>>user5:12345               user3
>>user2:234239              user4
>>user3:34908               user5
>>user1:234342              
>>user4:234993                  

>>and I want to use an awk script to generate a third file which
>>contains the information contained in FILE 1 for only those users
>>which are contained in FILE 2.  So my FILE 3 in this sample would
>>contain:
>>FILE 3
>>user3:34908
>>user4:234993
>>user5:12345

>>Additional info:
>>FILE 1 contains two fields separated with a ":"
>>FILE 2 only has one field
>>FILE 3 does not have to be in any particular order (ie. alphabetical)

>#!gawk
>BEGIN {FS=":"}
>ARGIND == 1 { x[$1] = $0;next }
>$1 in x {print x[$1]}



Fri, 06 Jan 2006 23:08:06 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. Using Refinate to compare EDIF files and verify/create BOM

2. Cross reference with third INPUT-FILE while updating MASTER-FILE

3. comparing an input file with an output file

4. Comparing file objects: what is compared?

5. How to create an TPE file (Example file)

6. Create a file with a file

7. reading ascii file and creating topspeed file in application designer - gasman.zip (0/2)

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

9. Truncated Source Files and Creating Export Files

10. Create a DDF File from Pervasive SQL since a clarion dictionnary ( DCT File)

11. Create a file with a vrml file

12. Utility for creating fonts.scale files from AFM files

 

 
Powered by phpBB® Forum Software