简体   繁体   中英

how to remove delimiters from MAC address in line

Hi I have a script that is processing a txt file line by line and the problem I'm having is that the input is not very consistent. I have "cleaned up" much of it so it is consistent in this form:

172.31.8.72|web302-eth0|172.31.8.72|52|54|00|58|74|E9|web302

However, though I have achieved consistency, I'd like to remove the "|" (pipes) from the MAC address. so that it looks like:

172.31.8.72|web302-eth0|172.31.8.72|5254005874E9|web302

I could use some help please :)

Here is the function in my script that is doing this processing (so far) I know I'm close!
Any help is greatly appreciated! :)

def processScan():
  with open(latest_file, "r") as scanInput:
    with open(pingOutput, "w") as pingOnly:
      with open(assetOutput, "w") as knownAssets:
        with open(assetVsRT, "w") as assetRTdiff:
          for record in scanInput:
            ppattern = '[0-9] PING'
            khpattern = '^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\s+.*\s+\d'
            if re.search(ppattern, record):
              record = re.sub("PING only", "PING-Only", record.strip())
              record = re.sub(" ", "|", record.strip())
              pingOnly.writelines(record + "\n")
            elif re.search(khpattern, record):
              record = re.sub('"', '', record.strip())
              record = re.sub("  ", " ", record.strip())
              record = re.sub(" ", "|", record.strip())
              knownAssets.writelines(record + "\n")

Thanks in advance!

You're already using Regex, so use some more!

re.sub('\\|([A-F0-9]{2})\\|([A-F0-9]{2})\\|([A-F0-9]{2})\\|([A-F0-9]{2})\\|([A-F0-9]{2})\\|([A-F0-9]{2})\\|', '|\\1\\2\\3\\4\\5\\6|', record)

The pattern is long because you need the results of all the captures and repeated captures are overwritten.

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