Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What about this:</p> <pre><code>import fileinput for line in fileinput.input(): print(line.split()) </code></pre> <p>You can try it like that:</p> <pre><code>lsof | python your_script.py </code></pre> <p><strong>Addressing the 'spaces in <em>NAME</em> problem'</strong></p> <p>For addressing the issue about possible spaces in <em>NAME</em> column mentioned in the comments I can propose the following solution. It's based on my desire to keep it simple and on the fact that only the last column could have spaces. </p> <p>The algorithm is simple: 1. Find the position where the last columns start - I use the header <em>NAME</em> starting position 2. Cut the line after that position> What you just cut is the value of the <em>NAME</em> column 3. <em>split()</em> the rest of the line. </p> <p>Here is the code:</p> <pre><code>import fileinput header_limits = dict() records = list() input = fileinput.input() header_line = None for line in input: if not header_line: header_line = line col_names = header_line.split() for col_name in col_names: header_limits[col_name] = header_line.find(col_name) continue else: record = dict() record['NAME'] = line[header_limits['NAME']:].strip() line = line[:header_limits['NAME'] - 1] record.update(zip(col_names, line.split())) records.append(record) for record in records: print "%s\n" % repr(record) </code></pre> <p>The result is a list of dictionaries. Every dictionary correspond to one line of the <em>lsof</em> output.</p> <p>This is interesting task showing the power of python for everyday tasks. </p> <p>Any way, if it's possible I would prefer the use of some python library as the proposed <em>psutils</em></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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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