Skip to content

Instantly share code, notes, and snippets.

@papamoose
Last active December 22, 2025 05:47
Show Gist options
  • Select an option

  • Save papamoose/5e68da14eba8c618eeb56903b0c85eb2 to your computer and use it in GitHub Desktop.

Select an option

Save papamoose/5e68da14eba8c618eeb56903b0c85eb2 to your computer and use it in GitHub Desktop.
Sort sacctmgr lines
#!/usr/bin/env python3
import sys
from collections import defaultdict, OrderedDict
def main():
# Read all lines and track original order
lines = [line.strip() for line in sys.stdin if line.strip()]
# Build hierarchy while preserving order
hierarchy = defaultdict(OrderedDict)
account_lines = {}
root_accounts = OrderedDict()
for line in lines:
parts = line.split()
account = parts[3]
parent = parts[-1].split('=')[1]
account_lines[account] = line
if parent == 'root':
root_accounts[account] = None # OrderedDict preserves insertion order
else:
hierarchy[parent][account] = None # OrderedDict preserves insertion order
# Print in correct order
def print_account(account):
print(account_lines[account])
for child in hierarchy.get(account, []):
print_account(child)
for account in root_accounts:
print_account(account)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment