Python Forum
using vars from one file to match lines in another
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
using vars from one file to match lines in another
#1
Hello all,

I have a problem where I have two very long lists and I need to use variables (ip addresses) to match lines in another.
fileseen is the list of all IP addresses observed at a certain location, filemaybe is a list of hosts in subnets available in various locations.

I am hoping to use IPs in fileseen to get the SUBLOC location code in file two when a match between an actual and potential occured.

My code so far looks like this, it does the regex's fine, but it does not loop back the next line of the
first and ip match to extract the next set of variables:

cat file1:
(unique $1, not unique $2,$3)
10.0.1.x LO1 192.168.1.11
10.0.2.x LO2 192.168.1.11

cat file2:
(all potential ip's for a given LOC site, $1 unique)
10.0.1.x SUBLOC

#!/usr/bin/python3

import sys
import re

def extract_ips(data):
	regex=re.compile(r"[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}")
	return (regex.findall(data))

def extract_proxy(data):
	regex=re.compile(r"\ [A-Z0-9]{1,5}\ ")
	return (regex.findall(data))

def extract_proxyip(data):
	regex=re.compile(r"[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}")
	return (regex.findall(data))


fileseen = open ('file1' ,'r', encoding='utf-8')
filemaybe = open ('file2','r', encoding='utf-8')

seen_hosts=[]
for line in fileseen:
	for ip in extract_ips(line):
		if ip not in seen_hosts:
        seen_hosts.append(ip)
			for proxy in extract_proxy(line):
				for proxyip in extract_proxyip(line):
#				print ('Seen:', seenip, ' BEHIND Proxy:',boproxy, ' BO Proxy IP:',proxyip)
					for line in filemaybe:
						if re.search(ip, line):
					#		print ('Matched actual:', ip, 'Sublocation: ').  # help is needed here (Want SUBLOC, I only get 1 match


                    #print ('Seen:', seenip, ' BEHIND Proxy:',proxy, ' BO Proxy IP:',proxyip)
Thank you very much.

Greetings - G

sorry I cleaned this up a bit.
Works for the first file. Now how can I used this to get the stuff I want from file two?
I had some help some years ago for Perl via Perlmonks, they used hash indexes, but I am obviously not a coder.

#!/usr/bin/python3

import sys
import re

def extract_ips(data):
    regex=re.compile(r"^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}")
    return (regex.findall(data))

def extract_bo(data):
    regex=re.compile(r"\ [A-Z0-9]{1,5}\ ")
    return (regex.findall(data))

def extract_proxyip(data):
    regex=re.compile(r"[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$")
    return (regex.findall(data))



filemaybe = open ('all_pot_hosts.txt','r', encoding='utf-8')
fileseen = open ('seen_hosts_by_proxy.txt' ,'r', encoding='utf-8')


seen_hosts=[]
for line in fileseen:
    for ip in extract_ips(line):
        if ip not in seen_hosts:
            for boproxy in extract_bo(line):
                for proxyip in extract_proxyip(line):
                    print ('Seen:', ip, ' BEHIND Proxy:',boproxy, ' BO Proxy IP:',proxyip)
Reply


Messages In This Thread
using vars from one file to match lines in another - by gt76_noobster - Jan-29-2019, 02:50 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [solved] how to delete the 10 first lines of an ascii file paul18fr 7 1,777 Aug-07-2024, 08:18 PM
Last Post: Gribouillis
  Delete multiple lines from txt file Lky 6 3,919 Jul-10-2022, 12:09 PM
Last Post: jefsummers
  failing to print not matched lines from second file tester_V 14 8,955 Apr-05-2022, 11:56 AM
Last Post: codinglearner
  Extracting Specific Lines from text file based on content. jokerfmj 8 5,489 Mar-28-2022, 03:38 PM
Last Post: snippsat
  Importing a function from another file runs the old lines also dedesssse 6 3,734 Jul-06-2021, 07:04 PM
Last Post: deanhystad
  vars() can't be used in list interpretation? Cheng 3 2,973 Jun-22-2021, 11:59 AM
Last Post: snippsat
  [Solved] Trying to read specific lines from a file Laplace12 7 5,113 Jun-21-2021, 11:15 AM
Last Post: Laplace12
  all i want to do is count the lines in each file Skaperen 13 7,103 May-23-2021, 11:24 PM
Last Post: Skaperen
  Split Characters As Lines in File quest_ 3 3,380 Dec-28-2020, 09:31 AM
Last Post: quest_
  Find lines from one file in another tester_V 8 4,814 Nov-15-2020, 03:29 AM
Last Post: tester_V

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020