Skip to content

Instantly share code, notes, and snippets.

@manics
Created January 25, 2018 16:08
Show Gist options
  • Save manics/f51afa3b924d80980ea9de779ad8b353 to your computer and use it in GitHub Desktop.
Save manics/f51afa3b924d80980ea9de779ad8b353 to your computer and use it in GitHub Desktop.
Parse the output of `lsof -F`
#!/usr/bin/env python
# Convert the output of `lsof -F` into PID USER CMD OBJ
import sys
rs = []
pid = -1
cmd = ''
user = ''
for line in sys.stdin:
k = line[0]
v = line[1:].rstrip('\n')
if k == 'p':
pid = int(v)
user = ''
cmd = ''
if k == 'L':
user = v
if k == 'c':
cmd = v
if k == 'n':
obj = v
print('%d\t%s\t%s\t%s' % (pid, user, cmd, obj))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment