Skip to content

Instantly share code, notes, and snippets.

@athoune
Last active January 4, 2016 17:29
Show Gist options
  • Save athoune/8654343 to your computer and use it in GitHub Desktop.
Save athoune/8654343 to your computer and use it in GitHub Desktop.
Who is writing?
#!/usr/bin/env python
import subprocess
import re
WP = re.compile(r' +')
def lsof():
p = subprocess.Popen(['lsof', '/'], stdout=subprocess.PIPE)
headers = WP.split(p.stdout.readline()) # headers
sizes = [len(a) for a in headers]
r = []
for line in p.stdout:
values = WP.split(line[:-1])
if values[4] == 'REG' and values[3].endswith('w'):
for i, value in enumerate(values):
sizes[i] = max(sizes[i], len(value))
values[6] = int(values[6])
r.append(values)
r.sort(lambda a, b: cmp(a[6], b[6]))
return headers, sizes, r
headers, sizes, lines = lsof()
for values in [headers] + lines:
print(' '.join([str(value).rjust(sizes[i]) for i, value in enumerate(values)]))
@zerodeux
Copy link

Did you mean :

lsof / |perl -ane '$s{$F[8]}=$F[6] if $F[4] eq 'REG' and $F[3]=~/w$/; END{ printf "%9d %s\n",$s{$_},$_ foreach sort{$s{$b}-$s{$a}} keys %s }'

:)

@athoune
Copy link
Author

athoune commented Jan 31, 2014

A compiler python to perl none liner can be useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment