Compare two files

I have two files in this format

file1= filename val1 val2
file2= filename val3 val4

I want to compare file names, and if they have the same names, I want to get a third file, for example:

filename val1 val2 val3 val4

I select the file name from file1and iterate over file2to see if I get it. Then search for the pointer back to the beginning file2for the next file name. Is there a more efficient way to do this?

+3
source share
2 answers

It looks like you're looking for a standard command join(not Python, but a standard Unix shell utility). Here it bounces off the man page on the system (which seems to have more detailed information than the Linux man page for connecting ):

        join phonedir names

        If the phonedir file contains the following names:

        Adams A.        555-6235
        Dickerson B.    555-1842
        Erwin G.        555-1234
        Jackson J.      555-0256
        Lewis B.        555-3237
        Norwood M.      555-5341
        Smartt D.       555-1540
        Wright M.       555-1234
        Xandy G.        555-5015

        and the names file contains these names and department numbers:

        Erwin           Dept. 389
        Frost           Dept. 217
        Nicholson       Dept. 311
        Norwood         Dept. 454
        Wright          Dept. 520
        Xandy           Dept. 999

        the join command displays:

        Erwin G.        555-1234        Dept. 389
        Norwood M.      555-5341        Dept. 454
        Wright M.       555-1234        Dept. 520
        Xandy G.        555-5015        Dept. 999
+4
source

create a dict file2[filename] = (val3, val4). you can create this once, and then the search time for a given file name, which you get from file1, takes a constant time (i.e. more or less regardless of file size2)

+2
source

All Articles