Issue
I have a file named info.txt
that contains information like this:
=== Some Unique Headline ===
Version: 1.2.0
== Changelog ==
= 1.2.0 =
* Mac release
* Security fix
= 1.0.0 =
* Windows release
I want to extract 2 sections.
- the
Version
number,1.2.0
I can use this:
grep 'Version:' info.txt | cut -d\ -f3
- I want to extract the info under the changelog heading that matches that version. For example, since the
Version
is 1.2.0 the following text will be extracted (text under heading 1.2.0 in the changelog):
* Mac release
* Security fix
Is there a way to do this with grep
or awk
?
Solution
You can build a simple state machine in awk to select the relevant lines:
/^=== / { s = 3; next }
s==3 && $1=="Version:" { v = $2 }
/^== / { s = 2; d = ($2=="Changelog") }
d && /^= / { s = 1; p = ($2==v); next }
p && $0 { print }
s
stores a state:s==3
: sections==2
: subsections==1
: subsubsection (not really needed)
d
stores another state - nonzero when in changelog subsectionp
stores another state - nonzero when printable linev
holds the version once foundp && $0
: output non-empty printable lines
Store in a file (say "script") and invoke as awk -f script info.txt
Answered By - jhnc Answer Checked By - Pedro (WPSolving Volunteer)