1

Through one command linux (lsof) I get a serie of data in a table:

COMMAND     PID       USER   FD      TYPE DEVICE  SIZE/OFF   NODE NAME
init          1       root  cwd   unknown                         /proc/1/cwd (readlink: Permission denied)
init          1       root  rtd   unknown                         /proc/1/root (readlink: Permission denied)
python    30077      user1  txt       REG    8,1   2617520 461619 /usr/bin/python2.6

As we can see some places are emptly, Im looking the way to split every cell into a list (one list per row), im trying in this way:

lsof_list = commands.getoutput('lsof | sed \'1d\'')
k = 1
list_lsof = {}

j = 1
apps = {}

for line in lsof_list.splitlines():
    list_lsof[k] = line
    print(list_lsof[k])
    for apps[j] in list_lsof[k].split():
        #print(' app ', list_lsof[j])
        j += 1
        #print(apps[k])

        if j == 9:
            print(apps[1], apps[2], apps[3], apps[4], apps[5], apps[6], apps[7],apps[8])
            j = 1

But due there are different long of spaces I got a lot of emptly " " items, how can i avoid it? I guess the problem is in lsof_list.splitlines(): but I cant find what I need to change. Thank you!

1
  • 2
    Maybe you should have a look at lsof's '-F' option (see also the section 'OUTPUT FOR OTHER PROGRAMS' in the lsof man page). When using that option the output is in a well-known, parseable format. Commented Nov 27, 2013 at 21:03

1 Answer 1

5

Since each line of output is almost a record of fixed-width fields, you could do something like the following to parse it (which is based on what is in another answer of mine).  I determined the width of the fields manually because of the difficulty involved of determining it automatically primarily due to the mixing of left- and right-justified fields (as well as the lone variable length one at the end).

import struct

lsof_list = """\
COMMAND     PID       USER   FD      TYPE DEVICE  SIZE/OFF   NODE NAME
init          1       root  cwd   unknown                         /proc/1/cwd (readlink: Permission denied)
init          1       root  rtd   unknown                         /proc/1/root (readlink: Permission denied)
python    30077      user1  txt       REG    8,1   2617520 461619 /usr/bin/python2.6
""".splitlines()

# note: variable-length NAME field at the end intentionally omitted
base_format = '8s 1x 6s 1x 10s 1x 4s 1x 9s 1x 6s 1x 9s 1x 6s 1x'
base_format_size = struct.calcsize(base_format)

for line in lsof_list:
    remainder = len(line) - base_format_size
    format = base_format + str(remainder) + 's'  # append NAME field format
    fields = struct.unpack(format, line)
    print fields

Output:

('COMMAND ', '   PID', '      USER', '  FD', '     TYPE', 'DEVICE', ' SIZE/OFF', '  NODE', 'NAME')
('init    ', '     1', '      root', ' cwd', '  unknown', '      ', '         ', '      ', '/proc/1/cwd (readlink: Permission denied)')
('init    ', '     1', '      root', ' rtd', '  unknown', '      ', '         ', '      ', '/proc/1/root (readlink: Permission denied)')
('python  ', ' 30077', '     user1', ' txt', '      REG', '   8,1', '  2617520', '461619', '/usr/bin/python2.6')
Sign up to request clarification or add additional context in comments.

Comments

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.