Re: [Pyparsing] parsing and extracting tac_plus.conf user data
Brought to you by:
ptmcg
From: Paul M. <pt...@au...> - 2014-05-25 05:46:42
|
Pyparsing only reports one because you only told it to get one. Change to: result = OneOrMore(expr).parseString(data).asList() and you'll process the rest of the file too. Also, you will find your results easier to process if you wrap expr in a Group, as in: expr = Group(Word(alphas) + '=' + Word(alphanums) + Optional(nestedCurlies)) -- Paul -----Original Message----- From: Asif Iqbal [mailto:va...@gm...] Sent: Sunday, May 25, 2014 12:08 AM To: pyp...@li... Subject: [Pyparsing] parsing and extracting tac_plus.conf user data Hi All, I am trying to use pyparse to extract user data and it only picks up the first block. Any idea what I am doing wrong? I am using python 2.7.6 on ubuntu trusty #!/usr/bin/python import pprint, sys #from pyparsing import Word, Literal, Forward, Group, ZeroOrMore, alphas from pyparsing import * f = sys.argv[1] data = open(f,'r').read() nestedCurlies = nestedExpr('{','}') expr = Word(alphas) + '=' + Word(alphanums) + Optional(nestedCurlies) expr.ignore("#" + restOfLine) result = expr.parseString(data).asList() pprint.pprint(result) Here is sample data file: user = aa06591 { pap = PAM login = PAM member = readonly ## temporary commands so John can adjust resolver ## uplink speed/duplex on SVCS routers cmd = interface { permit "Ethernet" deny .* } cmd = speed { permit .* } cmd = duplex { permit .* } cmd = default { permit speed permit duplex deny .* } cmd = write { deny ^erase permit .* } } user = lukesd { pap = des 11uGIcdXQ6v9E login = file /etc/tacacs-passwd member = readonly } user = curryc { pap = PAM login = PAM member = implementation } user = rhodesw { pap = PAM login = PAM member = implementation } user = aa68442 { pap = PAM login = PAM member = implementation } user = jdimayu { pap = PAM login = PAM member = readonly } Here is the output, and it only displays the first block ['user', '=', 'aa60591', ['pap', '=', 'PAM', 'login', '=', 'PAM', 'member', '=', 'readonly', 'cmd', '=', 'interface', ['permit', '"Ethernet"', 'deny', '.*'], 'cmd', '=', 'speed', ['permit', '.*'], 'cmd', '=', 'duplex', ['permit', '.*'], 'cmd', '=', 'default', ['permit', 'speed', 'permit', 'duplex', 'deny', '.*'], 'cmd', '=', 'write', ['deny', '^erase', 'permit', '.*']]] -- Asif Iqbal PGP Key: 0xE62693C5 KeyServer: pgp.mit.edu A: Because it messes up the order in which people normally read text. Q: Why is top-posting such a bad thing? ---------------------------------------------------------------------------- -- "Accelerate Dev Cycles with Automated Cross-Browser Testing - For FREE Instantly run your Selenium tests across 300+ browser/OS combos. Get unparalleled scalability from the best Selenium testing platform available Simple to use. Nothing to install. Get started now for free." http://p.sf.net/sfu/SauceLabs _______________________________________________ Pyparsing-users mailing list Pyp...@li... https://lists.sourceforge.net/lists/listinfo/pyparsing-users --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com |