Print code-fenced sections of a Markdown document Original code and demo at this gist.

Given a Markdown document like

Here is some text,
and some more text.
```javascript
const message = "This is JavaScript!";
```
More text follows, and then
```javascript
console.log(message);
```


I want to print out the sections in code fences, not including the code
fences, separated by a single blank line:

const message = "This is JavaScript!";

console.log(message);


I came up with the following AWK script, which seems to do the job
nicely:

#!/usr/bin/awk -f
BEGIN { in_code_block = 0 }
/^```/ {
    if (!in_code_block) {
        in_code_block = 1;
        first_line = 1;
    } else {
        in_code_block = 0;
        print "";
    }
}
{
    if (in_code_block &amp;&amp; !first_line) {
        print;
    }
    first_line = 0;
}


A goal is for the script to be dependency-minimal. I don't want to have
to install an implementation of CommonMark or an Erlang environment. AWK
fits this bill well.

Correspondingly, a non-goal is for this script to be correct in all
cases: I’m happy to accept false positives on lines starting with
```inline code``` like this, and similar edge cases.

I’m mostly looking for critique of my AWK, with respect to which I am a
total newbie. But any comments are welcome!
While your code looks ok, it could be improved greatly by making use of RS (record separator) and NR number of record, provided you're using GNU awk.

 awk -v RS='```[a-z]*\n' '(NR+1)%2' file


In this case RS is set such that it is catching everything between triple backticks with optional text.

The only awk statement is to print one record out of two.