Solved

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

Posted on 2016-10-20
7
67 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Installing Git and chefdk via bat script 8 87
Support for Notepad++ (including downloading & installing a plugin) 5 162
ROBOFTP UNZIP 1 53
Need to combine two scripts 2 33
A set of related code is known to be a Module, it helps us to organize our code logically which is much easier for us to understand and use it. Module is an object with arbitrarily named attributes which can be used in binding and referencing. …
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
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…

829 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