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.