0

I am trying to iterate a list with for loop and build a second list through regex capture group. I looked through documentation related to the re module but for some reason, the regex always return one result. The regex was tested so I am certain that is works.

Any thoughts?

Edited

#! /usr/bin/python
import re
import subprocess
try:
    cmd = "ps aux|grep -i \'zabbi[x]\'"
    cmd_stdout = subprocess.check_output(cmd, shell=True).split('\n')
    cmd_stdout_lst = []
    ps_re = re.compile(r'^(\S+)\s+(\d+)\s+\d+\.\d+\s+\d+\.\d+\s+(\d+)\s+(\d+).+')
    for line in cmd_stdout:
        match = ps_re.findall(line)
        if match:
            print match
        cmd_stdout_lst.append('\n\t\t{"{#USER}":'+'"' + match[0][0] + ',"{#PID}":'+'"'+match[0][1]+'"}'+',"{#PID}":'+'"'+match    [0][2]+'"}'+',"{#PID}":'+'"'+match[0][3]+'"}')
    print '{\n\t"data":['+','.join(cmd_stdout_lst)+']\n}'
except:
    raise

$ python proc_discovery.py
[('zabbix', '14479', '96784', '680')]
4
  • The expected output should be a new json list, something like this: {"data":[{"{#USER}":"zabbix"},{"{#PID}":"14479"},{"{#VSZ}":"96784"},{"{#RSS}":"680"}]} Commented May 8, 2017 at 18:16
  • switch your except: exit(1) to except: raise (or just remove it entirely) so you aren't hiding the exception you're getting. That is your problem. You're probably getting list index out of range exceptions. Commented May 8, 2017 at 18:22
  • Yes I suspect the match group is the issue, but when I thought match group is a list?! $ python proc_discovery.py [('zabbix', '14479', '96784', '680')] Traceback (most recent call last): File "proc_discovery.py", line 15, in <module> print match[2] IndexError: list index out of range Commented May 8, 2017 at 18:27
  • Also, remember: using a bare except: is almost never a good idea. Commented May 8, 2017 at 18:30

1 Answer 1

1

I see two things wrong with this:

  1. Your grep regex matches the same thing as simply zabbix would. Why the extra [ ]?
  2. Your program crashes after the first print inside the loop. You don't notice because you catch and ignore the exception. Remove the try-except to get a more helpful error message.

Specifically, problem number 2 is that re.findall() returns a list of tuples. The outer list has a single element in your case, the inner tuples have four. When you try to build the string for the second print statement, you are actually concatenating the entire tuple to the prefix string, which causes the exception. Use match[0][0] instead ofmatch[0] and so on, to fix this.

Sign up to request clarification or add additional context in comments.

2 Comments

That's it! 1. I added the square bracket so the grep command doesn't show up on the list. 2. I didn't aware I am working on multi-dimentional list as I assume I am always working on the inner list.
Ah, I see, that's a clever way to avoid the grep process itself showing up in the output :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.