Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This problem is <em>not</em> shaped like a regular expression nail, so please put that hammer down.</p> <p>The correct tool would be to parse the contents into a python structure, filtering out the items you don't want, then writing out the remaining entries again.</p> <p><a href="http://pyparsing.wikispaces.com/" rel="nofollow">pyparsing</a> would make the parsing job easy; the following is based on <a href="http://pyparsing.wikispaces.com/file/view/dhcpd_leases_parser.py/33450777/dhcpd_leases_parser.py" rel="nofollow">an existing example</a>:</p> <pre><code>from pyparsing import * LBRACE,RBRACE,SEMI,QUOTE = map(Suppress,'{};"') ipAddress = Combine(Word(nums) + ('.' + Word(nums))*3) hexint = Word(hexnums,exact=2) macAddress = Combine(hexint + (':'+hexint)*5) hdwType = Word(alphanums) yyyymmdd = Combine((Word(nums,exact=4)|Word(nums,exact=2))+ ('/'+Word(nums,exact=2))*2) hhmmss = Combine(Word(nums,exact=2)+(':'+Word(nums,exact=2))*2) dateRef = oneOf(list("0123456"))("weekday") + yyyymmdd("date") + \ hhmmss("time") startsStmt = "starts" + dateRef + SEMI endsStmt = "ends" + (dateRef | "never") + SEMI tstpStmt = "tstp" + dateRef + SEMI tsfpStmt = "tsfp" + dateRef + SEMI hdwStmt = "hardware" + hdwType("type") + macAddress("mac") + SEMI uidStmt = "uid" + QuotedString('"')("uid") + SEMI bindingStmt = "binding" + Word(alphanums) + Word(alphanums) + SEMI leaseStatement = startsStmt | endsStmt | tstpStmt | tsfpStmt | hdwStmt | \ uidStmt | bindingStmt leaseDef = "lease" + ipAddress("ipaddress") + LBRACE + \ Dict(ZeroOrMore(Group(leaseStatement))) + RBRACE input = open(DHCPLEASEFILE).read() with open(OUTPUTFILE, 'w') as output: for lease, start, stop in leaseDef.scanString(input): if lease.hardware.mac != mac: output.write(input[start:stop]) </code></pre> <p>The above code tersely defines the grammar of a <code>dhcp.leases</code> file, then uses <code>scanString()</code> to parse out each lease in the file. <code>scanString()</code> returns a sequence of matches, each consisting of a parse result and the start and end positions in the original string.</p> <p>The parse result has a <code>.hardware.mac</code> attribute (you may want to catch <code>AttributeError</code> exceptions on that, in case no hardware statement was present in the input), making it easy to test for your MAC address to remove. If the MAC address doesn't match, we write the whole lease back to an output file, using the <code>start</code> and <code>stop</code> positions to get the original text for that lease (much easier than formatting the lease from the parsed information).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload