Python regex re.search to list
I have some code to parse linux 'df -h', 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 is achieving the desired output:
['/run', '/run/lock', '/run/user/132', '/run/user/0']
But the line print ([x.split (") [- 1] for x in newlist]) shown below looks like a hack, I am struggling to get this to work as a regex using the 'r. Search for" below can Anyone suggest a better way to do 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'm using 'df -h' as ββsome random output for a regex, so when @romanPerekhrest suggests the best real world solution for this problem, I looked for a regex solution.
+3
source to share