使用bash在changelog文件中读取文件直到regex

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用bash在changelog文件中读取文件直到regex相关的知识,希望对你有一定的参考价值。

我想在文件中获取内容,直到找到内容中的正则表达式。我需要这个来报告我的上次更改日志

例:

## <small>0.27.3 (2019-03-18)</small>

* Fix integration tests
* Change log message


## <small>0.27.2 (2019-03-18)</small>

* Change find to filter
* Fix bug in typo
* Format message in request

我想要一个正则表达式只返回我最新版本的内容。例:

## <small>0.27.3 (2019-03-18)</small>

* Fix integration tests
* Change log message

我如何使用sed,grep或awk制作它?

谢谢你

编辑:

我可以做到:

CHANGELOG_MESSAGE=$(head -n 1 CHANGELOG.md)$(head -n 20 CHANGELOG.md | tail -n 19 | sed '/##/q' | awk '!/##/')

我认为这个解决方案有点复杂,但有效

答案

试试这个:

sed '1p;1,/^##/!d;/##/d' CHANGELOG.md

说明

1p             # print first line
;           
1,/^##/!d      # delete everything after second ##-line
;               
/##/d          # delete all lines start with ##

产量

## <small>0.27.3 (2019-03-18)</small>

* Fix integration tests
* Change log message

以上是关于使用bash在changelog文件中读取文件直到regex的主要内容,如果未能解决你的问题,请参考以下文章