Solved

Python:compare IP with IP:blahblah and output the entire line??

Posted on 2016-10-20
7
84 Views
Last Modified: 2016-10-21
Can anyone help me with a python script to do this?ok so this is my 1'st list:

78.55.37.235:silben1
125.167.122.99:SVEIF88
174.62.238.99:website6875
114.128.194.42:kikusom1
88.223.37.157:bobikas13
93.86.62.242:Perftech10

This is my 2'nd list

78.55.37.235
100.100.100.100
200.200.200.200
88.223.37.157
93.86.62.242

So we have 3 lines that exist in both lists,the output.txt  should look like this:

78.55.37.235:silben1
88.223.37.157:bobikas13
93.86.62.242:Perftech10

thank you

P.S.
I hope the script will handle big files like : 200 mb
0
Comment
Question by:john lambert
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 41852890
Which operating system are you running this on?
1
 

Author Comment

by:john lambert
ID: 41852895
windows os
0
 
LVL 16

Accepted Solution

by:
Walter Ritzel earned 500 total points
ID: 41853029
Here is one code that solve your problem:
with open('f1.txt', 'r') as f1, open('f2.txt', 'r') as f2, open('output.txt', 'w') as o1:
    check_list = [item.replace("\n", "") for item in f2]
    for line in f1:
        ip,name = line.split(':')
        if ip in check_list:
            o1.write(line)

Open in new window


The only assumption is related to the file 2 (which contains ips only), that could not be so big. Although I think that 200mb files will be piece of cake for this script.
1
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 

Author Closing Comment

by:john lambert
ID: 41853293
work fine with f1 10 lines and f2 10 lines :P
0
 
LVL 16

Expert Comment

by:Walter Ritzel
ID: 41853408
Try this:
with open('f1.txt', 'r') as f1, open('f2.txt', 'r') as f2, open('output.txt', 'w') as o1:
    check_list = [item.replace("\n", "") for item in f2]
    for line in f1:
        ip = line.split(':')
        if ip[0] in check_list:
            o1.write(line)

Open in new window

1
 

Author Comment

by:john lambert
ID: 41853489
working now!thank you...
0
 
LVL 16

Expert Comment

by:Walter Ritzel
ID: 41854147
Try this:
with open('f1.txt', 'r') as f1, open('f2.txt', 'r') as f2, open('output.txt', 'w') as o1:
    check_list = [item.replace("\n", "") for item in f2]
    for line in f1:
        ip = line.split(':')
        if len(ip[0]) > 0 and ip[0] in check_list:
            o1.write(line)

Open in new window

0

Featured Post

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here I am using Python IDLE(GUI) to write a simple program and save it, so that we can just execute it in future. Because when we write any program and exit from Python then program that we have written will be lost. So for not losing our program we…
This article will show, step by step, how to integrate R code into a R Sweave document
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question