简体   繁体   中英

python: read a file and match string in another file and pull out the lines

data masterfile :

192.168.42.2
192.68.42.12

searching file:

  1 17.248.154.203                           =>     7.88Kb     28.6Kb     28.6Kb     28.6KB
     192.168.42.12                            <=       208b      151Kb      151Kb      151KB
   2 a23-58-18-217.deploy.static.akamaitechn  =>     6.61Kb     8.60Kb     8.60Kb     8.60KB
     192.168.42.12                            <=     15.0Kb     4.95Kb     4.95Kb     4.95KB
   3 a23-207-129-216.deploy.static.akamaitec  =>         0b     8.09Kb     8.09Kb     8.09KB
     192.168.42.12                            <=         0b     3.25Kb     3.25Kb     3.25KB
   4 17.252.172.5                             =>         0b     4.92Kb     4.92Kb     4.92KB
     192.168.42.2                             <=         0b     3.73Kb     3.73Kb     3.73KB
     192.168.42.2                             <=         0b     3.73Kb     3.73Kb     3.73KB

Desired ouput:

192.168.42.12 3.25 (example)

192.168.42.2 3.73

[Link] https://pastebin.com/Ms4TyLMW -- i have changed my code based on your suggestion

This is what i got!

'192.168.42.12 151

192.168.42.12 4.95

192.168.42.12 3.25

192.168.42.2 3.73

192.168.42.2 3.73

192.168.42.12 5.36

192.168.42.12 705'

@saul

How can i avoid printing this multiple same data.I am trying to print same IP and corresponding usage in single line like this??

'192.168.42.12 2043(total usage of 192.168.42.12)

192.168.42.2 123(total usage of 192.168.42.2)'

I have two IP address in a text file. First I need to read the IP address and then check the another file which is holding IP address with usage(please refer my question above).

Now I need to extract the corresponding the IP and usage from another file. this code locate the particular position of IP and usage. The problem is when I tried to iterate iplist it always takes the input of first data whichever is in the file. Thanks in advance.

Code:

    for line in data_consumed:
        for element in iplist:
            if element in line:
                result = line
                ip = line[5:-50]
                result_ip = ip.replace(" ","")
                usage = line[-8:]
                d = usage.replace('KB', '')
                usage = d.replace('B','')
                usage = usage.replace('\n','')
                final_usage += float(usage)
                try:
                    final_usage += float(usage)
                except ValueError:
                    pass
                megabyte = float(0.000976562)
                result_usage = megabyte * final_usage
        print result_ip + '\t\t\t' + str(result_usage)

Desired output:

192.168.42.12  usage(total value)

192.168.42.2  usage(totalvalue)

You can do like the code:

strStr = ["192.168.42.2", "192.168.42.12"]

# the "e:\temp\111.txt" is the searching file storage path.
with open(r"e:\temp\111.txt") as f:
    lines = f.readlines()
    for ii in strStr:
        sumResult = 0
        for line in lines:
            if ii in line:
                sumResult += float(line.strip(" ").split(" ")[-1].split("KB")[0])
        print(ii, sumResult)

The code result is:

192.168.42.2 7.46
192.168.42.12 159.2

There are some question, because you don't finger out the match condition, just split the last value from the line data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM