AlexW
AlexW

Reputation: 2587

Search a text file with another text file

I thought I found a similar question here (Python search a file for text using input from another file) but that doesn't seem to work for me, the print is empty, as in none found, but there is definitley matches for them all

source file sample is:

MAC Address     
0800.0f5b.b739
0800.0f69.d860
0800.0f6b.9177
0800.0f6c.2e4d
0800.0f77.2879
0800.0f7f.4c07
0800.0f83.4785
0800.0f9c.f608

data file sample is:

MAC Address     IP Address
000c.2912.57db  10.1.7.254
000c.294a.4b75  10.1.7.253
002a.6a5e.e381  10.1.6.3
0050.56ac.5f41  10.1.7.8
0050.56ac.5f41  10.1.7.9
0050.56ac.6067  10.1.6.249
0050.56ac.6067  10.1.6.254
0050.56ac.9d49  10.1.7.104
0800.0f5b.b739  10.1.7.153
0050.56ac.e9c9  10.1.7.250
0800.0f48.7f40  10.1.6.180
0800.0f51.9d99  10.1.6.112
0800.0f51.a32a  10.1.6.47
0800.0f51.a915  10.1.6.241

using the source file I want to find the matching IP addresses from the data file. I've tried the sample seen from the other question

d_file = 'C:\Python Scripts\VLAN_Data.txt'
s_file = 'C:\Python Scripts\SourceData.txt'

keywords = set()
with open(s_file) as list_file:
    for line in list_file:
        if line.strip():
            keywords.add(line.strip())

with open(d_file) as master_file:
        for line in master_file:
            if set(line.split()[:-1]) & keywords:
                print line  

EDIT

ok it does work....i was copy and pasting into shell and it failed, i saved it as a .py and run it in the module instead and it works. anyone know why copy pasta into shell would fail?

Upvotes: 1

Views: 102

Answers (2)

Gal Dreiman
Gal Dreiman

Reputation: 4009

Another solution for is:

d_file = 'Data\data.txt'
s_file = 'Data\source.txt'

keywords = set()
with open(s_file) as list_file:
    for line in list_file:
        if line.strip():
            keywords.add(line.strip())

data = set()
with open(d_file) as master_file:
        for line in master_file:
            data.add(line.strip().split('  ')[0])

print keywords.issubset(data)

This solution is based on set intersection: create two set of MAC Addressed and check if one is fully contained in the other.

Upvotes: 1

Ma0
Ma0

Reputation: 15204

This is how i would do it:

with open(r'C:\Users\evkouni\Desktop\file_sample.txt', 'r') as f_in:
    content = f_in.readlines()
    add_dict = {}
    for line in content:
        add_dict[line.split()[0]] = line.split()[1]

with open(r'C:\Users\evkouni\Desktop\target.txt', 'r') as f_t:
    content = f_t.readlines()
    matches = {}
    for line in content:
        if line.strip() in add_dict:
            matches[line.strip()] = add_dict[line.strip()]
            continue

print(matches)
#{'0800.0f5b.b739': '10.1.7.153'}

The first with block loads the MAC-to-IP address blocks and stores the pairs in a dictionary add_dict.

The second with block opens the target file and goes through it line-by-line searching for the keys previously stored. When it finds them, it stores the pair on a new dict called matches. The type of the container matches depends on what you are planning on doing with it.

Upvotes: 2

Related Questions