Latest "awk" Articles
Workshop » Reference Section » Grimoires » IT » Platforms » Linux » Packages » awk

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

[code]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[/code]

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 ##.