首页 > 解决方案 > 在文本文件中查找和替换正则表达式(mac 地址)

问题描述

这已在其他地方被问过,但在尝试这些解决方案时并不高兴。我正在尝试使用 open(file) 而不是文件输入来搜索和替换。原因是我正在打印“x of y completed”消息(文件输入将其放入文件而不是终端)。我的测试文件是由新行分隔的 100 个 mac 地址。

我想做的就是找到匹配mac地址的正则表达式并将其替换为“MAC ADDRESS WAS HERE”。下面是我所拥有的,它只将替换字符串放在文件底部一次。

#!/usr/bin/env python3
import sys
import getopt
import re
import socket
import os
import fileinput
import time

file = sys.argv[1]

regmac = re.compile("^(([a-fA-F0-9]{2}-){5}[a-fA-F0-9]{2}|([a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2}|([0-9A-Fa-f]{4}\.){2}[0-9A-Fa-f]{4})?$")
regmac1 = "^(([a-fA-F0-9]{2}-){5}[a-fA-F0-9]{2}|([a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2}|([0-9A-Fa-f]{4}\.){2}[0-9A-Fa-f]{4})?$"
regv4 = re.compile(r'^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$')
regv41 = '^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$'

menu = {}
menu['1']="MAC"
menu['2']="IPV4"
menu['3']="IPV6"
menu['4']="STRING"
menu['5']="EXIT"

while True:
    options=menu.keys()
    sorted(options)
    for entry in options:
        print(entry, menu[entry])

    selection = input("Please Select:")
    if selection == '1':
        print("MAC chosen...")
        id = str('mac')
        break
    elif selection == '2':
        print("IPV4 chosen")
        id = str('ipv4')
        break
    elif selection == '3':
        print("IPV6 chosen")
        id = str('ipv6')
        break
    elif selection == '4':
        print("String chosen")
        id = str('string')
        break
    elif selection == '5':
        print("Exiting...")
        exit()
    else:
        print("Invalid selection!")

macmatch = 0
total = 0

while id == 'mac': 
    with open(file, 'r') as i:
        for line in i.read().split('\n'):
            matches = regmac.findall(line)
            macmatch += 1
        print("I found",macmatch,"MAC addresses")
        print("Filtering found MAC addresses")
    i.close()

    with open(file, 'r+') as i:
        text = i.readlines()
        text = re.sub(regmac, "MAC ADDRESS WAS HERE", line)
        i.write(text)

上面将“MAC ADDRESS WAS HERE”放在最后一行的末尾,而不替换任何 MAC 地址。
我从根本上错过了一些东西。如果有人能指出我正确的方向,那就太好了!

警告,我通过文件输入有这个工作,但无法显示它的进度,所以尝试使用上面。再次感谢!

标签: pythonpython-3.x

解决方案


全部,我想通了。发布工作代码以防万一有人看到这篇文章。

#!/usr/bin/env python3
#Rewriting Sanitizer script from bash
#Import Modules, trying to not download any additional packages.  Using regex to make this python2 compatible (does not have ipaddress module).

import sys
import getopt
import re
import socket
import os
import fileinput
import time

#Usage Statement sanitize.py /path/to/file, add help statement
#Test against calling entire directories, * usage

#Variables
file = sys.argv[1]
regmac = re.compile("^(([a-fA-F0-9]{2}-){5}[a-fA-F0-9]{2}|([a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2}|([0-9A-Fa-f]{4}\.){2}[0-9A-Fa-f]{4})?$")
regmac1 = "^(([a-fA-F0-9]{2}-){5}[a-fA-F0-9]{2}|([a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2}|([0-9A-Fa-f]{4}\.){2}[0-9A-Fa-f]{4})?$"
regv4 = re.compile(r'^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$')
regv41 = '^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$'

#Functions

menu = {}
menu['1']="MAC"
menu['2']="IPV4"
menu['3']="IPV6"
menu['4']="STRING"
menu['5']="EXIT"

while True:
    options=menu.keys()
    sorted(options)
    for entry in options:
        print(entry, menu[entry])

    selection = input("Please Select:")
    if selection == '1':
        print("MAC chosen...")
        id = str('mac')
        break
    elif selection == '2':
        print("IPV4 chosen")
        id = str('ipv4')
        break
    elif selection == '3':
        print("IPV6 chosen")
        id = str('ipv6')
        break
    elif selection == '4':
        print("String chosen")
        id = str('string')
        break
    elif selection == '5':
        print("Exiting...")
        exit()
    else:
        print("Invalid selection!")

macmatch = 0
total = 0

while id == 'mac': 
    with open(file, 'r') as i:
        for line in i.read().split('\n'):
            matches = regmac.findall(line)
            macmatch += 1
        print("I found",macmatch,"MAC addresses")
        print("Filtering found MAC addresses")
    i.close()

    with open(file, 'r') as i:
        lines = i.readlines()
        
    with open(file, 'w') as i:
        for line in lines:
                line = re.sub(regmac, "MAC ADDRESS WAS HERE", line)
                i.write(line)
        i.close()
    break

上面用“MAC ADDRESS WAS HERE”覆盖了正则表达式匹配(找到的 MAC 地址)。希望这可以帮助某人。欢迎提出任何建议以提高效率或以其他方式完成。一旦我能够,2天后将标记为答案。


推荐阅读