?
Solved

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

Posted on 2016-10-20
7
Medium Priority
?
129 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
  • 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 2000 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
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 

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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Article by: Swadhin
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
Measuring Server's processing rate with a simple powershell command. The differences in processing rate also was recorded in different use-cases, when a server in free and busy states.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses
Course of the Month17 days, 5 hours left to enroll

864 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