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 }
' input.md

First, cd into the same folder as the file (here called input.md. Then the above will generate titled .md documents from every section that starts with ##.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply