Skip to content

Instantly share code, notes, and snippets.

@joshland
Last active January 15, 2017 03:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshland/e89347f88d128a971768c2a711dfa5dc to your computer and use it in GitHub Desktop.
Save joshland/e89347f88d128a971768c2a711dfa5dc to your computer and use it in GitHub Desktop.
i needed a tool to identify non-wrapping columns from text commands with variable fixed witdth output. (e.g. lsblk)
#!/usr/bin/python
# -*- coding: utf-8 -*-
__author__ = 'Joshua M. Schmidlkofer <joshland@protonmail.com>'
__credits__ = ["Joshua M. Schmidlkofer"]
__status__ = "Development"
__url__ = "https://gist.github.com/joshland/e89347f88d128a971768c2a711dfa5dc"
from collections import OrderedDict
import re
def findfields(header):
"""
guess at variable fixed-width columns, based upon header.
orientation is either 'left' or 'right'
"""
fields = OrderedDict()
colName = None
colStart = -1
colEnd = -1
index = 0
while 1:
s = re.search(r'\w+', header[index:])
if s: # we found something
start, end = s.span() ## relative start/stop
sub = header[index:] ## relative stringlet
if colName: ## if we have an old col, save it now
colEnd = index + start
fields[colName] = (colStart, colEnd)
pass
colName = sub[start:end] ## new column name
colStart = index + start ## set new start
colEnd = -1 ## reset the end
if end + index == len(header):
## We reached the end
fields[colName] = (colStart, -1)
break
index = index + end
else:
print("hmm Somethign went wrong")
break
continue
return fields
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment