Monday, April 4, 2022

[SOLVED] Using grep/awk to extract info from a file

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.

  1. the Version number, 1.2.0 I can use this:
grep 'Version:' info.txt | cut -d\   -f3
  1. 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 : section
    • s==2 : subsection
    • s==1 : subsubsection (not really needed)
  • d stores another state - nonzero when in changelog subsection
  • p stores another state - nonzero when printable line
  • v holds the version once found
  • p && $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)