#!/bin/sh # Use like (using smbclient's grepable option): # smbclient -g -L //server | sort | samba-to-json.sh # (the sort is important as services are intermingled with disk shares) awk -F '|' ' BEGIN { first=1; printf("{\n"); } { if (NF < 3) { next; } key=$1 name=$2 description=$3 if (key != lastkey) { if (!first) { printf("],\n"); } printf("\"%s\": [",key); lastkey=key; itemfirst=1; first=0; } if (!itemfirst) { printf(","); } printf("{ \"name\": \"%s\", \"description\": \"%s\" }", name, description); itemfirst=0; } END { printf("]\n}\n"); }'