_ _ | |_ ___ __| |____ ___ _ _ | __/ _ \/ _` |_ / / _ \ | | | | || __/ (_| |/ / | __/ |_| | \__\___|\__,_/___(_)___|\__,_|
The following sed command filters fenced code blocks out of a markdown file called "example.md":
cat example.md | sed '/^```/,/^```/d'
I'm using it to exclude code listings when counting the number of words in a blog post. For example...
cat example.md | sed '/^```/,/^```/d' | wc -w
Or to count the words in all markdown files in the current folder (excluding fenced code blocks)...
cat *.md | sed '/^```/,/^```/d' | wc -w
An arguable improvement on this is to use the following alternative, which deals with the individual files separately, rather than as a single continuous stream. The advantage is that if a fenced code block is opened and never closed before the end of the file, it will not continue filtering lines into the next file.
sed -s '/^```/,/^```/d' *.md
Consider the following markdown file, test.md, which contains a block of MathML:
This is the first line of the file.
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block">
<mstyle displaystyle="true" scriptlevel="0">
<mrow data-mjx-texclass="ORD">
<mtable rowspacing=".5em" columnspacing="1em" displaystyle="true">
<mtr>
<mtd>
<mi>E</mi>
<mo>=</mo>
<mi>m</mi>
<msup>
<mi>c</mi>
<mn>2</mn>
</msup>
</mtd>
</mtr>
</mtable>
</mrow>
</mstyle>
</math>
This is the last line of the file.
To strip out the MathML section of the file using sed:
sed -s '/^\s*<math/,/^\s*<\/math/d' test.txt
The resulting output is
This is the first line of the file.
This is the last line of the file.
To count the words in the file, excluding the MathML section, use the following:
sed -s '/^\s*<math/,/^\s*<\/math/d' test.md | wc -w
In this case, the output is
13