Splitting a markdown document sections into separate documents with awk in Bash

awk ' /^## / { if (file) close(file) title = substr($0, 4) gsub(/[^a-zA-Z0-9 _-]/, "", title) gsub(/ /, "_", title) file = title ".md" } file { print file