« Back to Index

AWK: extract first changelog entry

View original Gist on GitHub

Tags: #awk #changelog

extract-first-changelog-entry.md

We want just the ‘v1.0.0-beta.2’ changelog block, not ‘v1.0.0-beta.1’…

# Changelog

## [v1.0.0-beta.2](...)

**Bug fixes:**

- ...

**Enhancements:**

- ...
- ...

**Documentation:**

- ...
- ...
- ...

## [v1.0.0-beta.1](...)

**Enhancements:**

* More stuff

...

The following awk script does this for us…

cat CHANGELOG.md | awk '/^##/{block++} {if (block==1) {print}}'

Awk works by reading the input ‘line by line’. Our script checks if the conditional pattern ^## matches, which is the start of the changelog entry, and if so it increments the variable block. Next, it checks the variable value and prints each line as long as the variable is equal to 1.

The moment we reach the next changelog entry, the block variable will be incremented to 2 and so the block that prints each line will not execute.