I'm running a utility that parses the output of the df
command. I capture the output and send it to my parser. Here's a sample:
Filesystem 512-blocks Used Available Capacity iused ifree %iused Mounted on
/dev/disk2 1996082176 430874208 1564695968 22% 2429281 4292537998 0% /
devfs 668 668 0 100% 1156 0 100% /dev
map -hosts 0 0 0 100% 0 0 100% /net
map auto_home 0 0 0 100% 0 0 100% /home
Here's the function:
def parse_df(self, content):
"""Parse the `df` content output
:param content: The command content output
:return: (list) A list of objects of the type being parsed
"""
entries = []
if not content:
return entries
# Split the content by line and check if we should ignore first line
for line in content.split("\n"):
if line.startswith("Filesystem"):
continue
tokens = line.split()
print tokens
However I'm getting the following output:
['/dev/disk2', '1996082176', '430876480', '1564693696', '22%', '2429288', '4292537991', '0%', '/']
['devfs', '668', '668', '0', '100%', '1156', '0', '100%', '/dev']
['map', '-hosts', '0', '0', '0', '100%', '0', '0', '100%', '/net']
['map', 'auto_home', '0', '0', '0', '100%', '0', '0', '100%', '/home']
The issue is map -host
is supposed to be a single element (for the Filesystem
column).
I've tried to apply a regex tokens = re.split(r'\s{2,}', line)
but the result was still not correct:
['/dev/disk2', '1996082176 430869352 1564700824', '22% 2429289 4292537990', '0%', '/']
What would be the correct way to parse the output?