How to extract part of a document and save it to a new one (command line Mac)

Viewed 125

Our buildserver needs to release apps to App Center. App Center allows me to provide a release note file.

The problem is, our ReleaseNotes.md contains ALL the versions and become to big to for App Center to accept it.

The notes are formatted like this:

# Project - Release Notes - Android

## 1.20.2 - 2020-09-11
* [UID-4782] - Connectivity issues fixed

## 1.20.1 - 2020-09-08
 * [UID-4639] - Update Color
 * [UID-4760] - Changed some stuff

How can I grab just the first entry and save it to a file?:

## 1.20.2 - 2020-09-11
 * [UID-4782] - Connectivity issues fixed

and save it to a file?

If looked into tools like awk, grep, sed and pcergrep but I'm not familiar with these in anyway and I have no idea which one would be the right tool for the job.

4 Answers

This awk should get the job done:

awk -v RS= '/^##/{print; exit}' file
## 1.20.2 - 2020-09-11
* [UID-4782] - Connectivity issues fixed

Using awk in "paragraph mode" by setting RS to the empty string and then saving it to another file:

awk -v RS= '/issues fixed/' file > file2
## 1.20.2 - 2020-09-11
* [UID-4782] - Connectivity issues fixed

Using sed:

sed -n '/^##/,/^$/{p;/^$/q;}' input_file > output_file
  • -n: Do not print lines by default.
  • /^##/: Find a line starting with ##.
  • /^$/: Find an empty line.
  • /^##/, /^$/: Together this means for each line between a line starting with ## and an empty line.
  • p: Print the line.
  • /^$/q: If the line is empty quit. We do this because otherwise sed will process the next block of changes.

So I've did some bash script learning and created the following script:

#!/bin/bash

foundFirstEntry=false

"ReleaseNotes.md" | while read p; do
    if [[ $p = \#\#* ]]
    then
        foundFirstEntry=true
    fi

    if [[ $foundFirstEntry = true && $p = "" ]]
    then
        break
    fi

    if $foundFirstEntry 
    then
        echo "$p"
    fi
done > "ShortReleaseNotes.md" < "ReleaseNotes.md"
Related