3

I have some code to parse the linux 'df -h', the normal command line output looks like this:

Filesystem      Size  Used Avail Use% Mounted on
udev            987M     0  987M   0% /dev
tmpfs           201M  9.2M  191M   5% /run
/dev/sda1        38G   11G   25G  30% /
tmpfs          1001M  416K 1000M   1% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs          1001M     0 1001M   0% /sys/fs/cgroup
tmpfs           201M   28K  201M   1% /run/user/132
tmpfs           201M   28K  201M   1% /run/user/0

Currently my code achieves the desired output:

['/run', '/run/lock', '/run/user/132', '/run/user/0']

But the 'print ([x.split(" ")[-1] for x in newlist])' line shown below feels like a hack, I'm struggling to get this working as a regex using 'r.search' below, can anyone advise a better way of doing this please ?

import subprocess
import re


cmd = 'df -h'
output = subprocess.check_output(cmd, shell=True).decode('utf8')
ln = output.split('\n')
r = re.compile('/run.*')
newlist = list(filter(r.search, ln))

print ([x.split(" ")[-1] for x in newlist])

Edit * I am using 'df -h' as some random output to practice regex on, so while @romanPerekhrest offers the best real world solution for this problem I was looking for a regex solution.

1
  • 1
    Actually I believe your solution is better than regexp (and almost certainly faster). Commented Jul 11, 2017 at 15:23

2 Answers 2

3

The fastest approach:

df -h --output=target | grep '/run.*'

The output:

/run
/run/lock
/run/user/132
/run/user/0

  • --output=target - to output only mount points
Sign up to request clarification or add additional context in comments.

4 Comments

thanks for this, however I am using the 'df' tool as practice for parsing cmd output in python
@biscuitlover, it's simple: if you want to stay at Python, your current script would be simplified to cmd = "df -h --output=target | grep '/run.*'" output = subprocess.check_output(cmd, shell=True).decode('utf8') lines = output.split('\n')
this is the better real world solution, but i was using 'df -h' as some random output to perform regex on, I should have put that in my original question, thanks again.
@biscuitlover, yes, I tend to suggest a REAL solutions
2

how about

re.findall(r'/run.*$', output, re.MULTILINE)

I don't know about better or speed, but it cuts your code down to 3 lines, and you're regexing anyway.

3 Comments

thanks for this, this is exactly what i wanted to achieve !
to be fair though, the one by @romanPerekhrest is objectively better.
indeed, I have made an edit to my original question for clarification.

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.