Is it possible to sort a markdown file by level 1 heading? Looking for sed
or similar command line solution
#B
a content of B
#A
b content of A
to...
#A
b content of A
#B
a content of B
A perl one-liner, split for readability
perl -0777 -ne '
(undef,@paragraphs) = split /^#(?=[^#])/m;
print map {"#$_"} sort @paragraphs;
' file.md
You'll want to end the file with a blank line, so there's a blank line before #B
. Or you could changemap {"#$_"}
to map {"#$_\n"}
to forcibly insert one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With