如何提取文档的一部分并将其保存到新文档中(命令行Mac)

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

我们的构建服务器需要将应用发布到应用中心。 App Center 允许我提供发行说明文件。

问题是,我们的 ReleaseNotes.md 包含所有版本并且变得太大以至于 App Center 无法接受它。

笔记格式如下:

# 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

如何只抓取第一个条目并将其保存到文件中?:

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

并将其保存到文件中?

如果研究过 awk、grep、sed 和 pcergrep 等工具,但我对这些工具并不熟悉,我不知道哪个工具适合这项工作。

通过将 RS 设置为空字符串然后将其保存到另一个文件,在“段落模式”中使用 awk

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

这个 awk 应该可以完成工作:

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

使用sed:

sed -n '/^##/,/^$/{p;/^$/q;}' input_file > output_file
  • -n: 默认不打印行
  • /^##/:查找以##.
  • 开头的行
  • /^$/: 找空行
  • /^##/, /^$/:这意味着对于以 ## 开头的行和空行之间的每一行。
  • p: 打印该行。
  • /^$/q:如果该行为空则退出。我们这样做是因为否则 sed 将处理下一个更改块。

所以我做了一些 bash 脚本学习并创建了以下脚本:

#!/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"