jucho jucho - 1 month ago 14
Bash Question

Parse a changelog and extract changes for a version

I have a changelog file in markdown which contains all changes between each version of my app like that :

## Version 1.0.6

* first change
* second change
* third change

## Version 1.0.5

* first foo change
* second foo change

## Version 1.0.4

* and so on...


What I want is to extract in a script the changes content for a version. For example I would to extract the changes for the version 1.0.5, so it should print :

* first foo change
* second foo change


The ideal way would be
./getVersionChanges version filename
which would those 2 params :

version
: the version to extract changes

filename
: the filename to parse

How can I achieve this with sed, awk, grep, or whatever ?

Answer

A slightly more elaborate awk solution, which

  • exits once the block of interest has been printed,
  • ignores blank lines,
  • doesn't include the header line.
awk -v ver=1.0.5 '
 /^## Version/ { if (p) { exit }; if ($3 == ver) { p=1; next} } p && NF
' file

As script getVersionChanges:

#!/usr/bin/env bash

awk -v ver="$1" '
 /^## Version/ { if (p) { exit }; if ($3 == ver) { p=1; next} } p && NF
' "$2"