Solved

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

Posted on 2016-10-20
7
42 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 37

Expert Comment

by:Gerwin Jansen
Comment Utility
Which operating system are you running this on?
1
 

Author Comment

by:john lambert
Comment Utility
windows os
0
 
LVL 15

Accepted Solution

by:
Walter Ritzel earned 500 total points
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Closing Comment

by:john lambert
Comment Utility
work fine with f1 10 lines and f2 10 lines :P
0
 
LVL 15

Expert Comment

by:Walter Ritzel
Comment Utility
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
Comment Utility
working now!thank you...
0
 
LVL 15

Expert Comment

by:Walter Ritzel
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Installing Python 2.7.3 version on Windows operating system For installing Python first we need to download Python's latest version from URL" www.python.org " You can also get information on Python scripting language from the above mentioned we…
This article will show the steps for installing Python on Ubuntu Operating System. I have created a virtual machine with Ubuntu Operating system 8.10 and this installing process also works with upgraded version of Ubuntu OS. For installing Py…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now