Solved

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

Posted on 2016-10-20
7
74 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 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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Strings in Python are the set of characters that, once defined, cannot be changed by any other method like replace. Even if we use the replace method it still does not modify the original string that we use, but just copies the string and then modif…
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…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

685 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