简体   繁体   中英

How to read and match across multiple lines with python

I'm trying to extract pertinent information from a large textfile (1000+ lines), most of which isn't important:

 ID: 67108866 Virtual-system: root, VPN Name: VPN-NAME-XYZ
  Local Gateway: 1.1.1.1, Remote Gateway: 2.2.2.2
  Traffic Selector Name: TS-1
  Local Identity: ipv4(10.10.10.0-10.10.10.255)
  Remote Identity: ipv4(10.20.10.0-10.20.10.255)
  Version: IKEv2
  DF-bit: clear, Copy-Outer-DSCP Disabled, Bind-interface: st0.287
  Port: 500, Nego#: 0, Fail#: 0, Def-Del#: 0 Flag: 0x2c608b29 
  Multi-sa, Configured SAs# 1, Negotiated SAs#: 1 
  Tunnel events: 

From this I need to extract only certain bits, and example output would be something like:

VPN Name: VPN-NAME-XYZ, Local Gateway: 1.1.1.1, Remote Gateway: 2.2.2.2

I've tried a couple different ways to get this, however my code keeps stopping on the 1st match, I need the code to match 1 line, then move onto the following line and match that:

with open('/path/to/vpn.txt', 'r') as file:
    for vpn in file:
        vpn = vpn.strip().lower()
        name = "xyz"
        if name in vpn:
            print(vpn)
            if "1.1.1.1" in vpn:
                print(vpn)

I'm able to print both if I move the 2nd if in line:

with open('/path/to/vpn.txt', 'r') as file:
    for vpn in file:
        vpn = vpn.strip().lower()
        name = "xyz"
        if name in vpn:
            print(vpn)
        if "1.1.1.1" in vpn:
            print(vpn)

Is it possible to match clauses on both lines? I've tried a few different ways, with my indents and matches but can't get it, also the problem with print(vpn) is it's printing the entire line

Use regex to match the regions you need and then get all matched from the entire text. You need not do this line by line as well. An example below.

import re
found_text = []
with open('/path/to/vpn.txt', 'r') as file:
    file_text = file.read()
    [found_text.extend(found.split(",")) for found in [finds.group(0) for finds in
                                                       re.finditer(
                                                           r"((VPN Name|Local Gateway|Remote Gateway):.*)",
                                                           file_text)]]
    # split by comma, if you want it to be splitted further

print(found_text)

This will yield an output like

['VPN Name: VPN-NAME-XYZ', 'Local Gateway: 1.1.1.1', ' Remote Gateway: 2.2.2.2']

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