#!/usr/bin/awk -f BEGIN { idx = 0; } /^===/ { print "```" for(i=0; i < (idx - 1); i++) print ar[i] print "```" delete ar idx = 0 print("#",prev); next; } /^---/ { print "```" for(i=0; i < (idx - 1); i++) print ar[i] print "```" delete ar idx = 0 print("##", prev); next; } /^### / { print "```" for(i=0; i < (idx); i++) print ar[i] print "```" print $0 delete ar idx = 0 next; } { prev = $0 ar[idx++] = $0 }