【发布时间】:2020-07-29 07:50:40
【问题描述】:
我需要使用 grep(或 shell/bash 中的 awk 等)解析 Keep a changelog format 中的 CHANGELOG 并获取最新版本([Unreleased] 标记之后的第一个版本)。
意思是,用块'\n##'分割这个文件,忽略第一个([Unreleased])并获取第二个(如果存在)。
使用nodeJS,它非常简单易读CHANGELOG.split(/\n## /)[2];
但我不能让它与 grep 一起工作...grep -zoP -m 1 "(\n## .*)(\n## .*)?(\n## )?" CHANGELOG.md
即使使用(.|\n)+,我也无法使用多行创建正则表达式匹配组
由于几天以来我一直在使用它并一次又一次地尝试,Machine Learning 找到了这个##(?:[^be]+[^#]*###)+[^#]* 但是,对于“与\n## 进行块拆分”来说,它看起来太重了。
# Changelog
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased]
## [1.0.0] - 2017-06-20
### Added
{...}
### Changed
{...}
### Removed
{...}
## [0.3.0] - 2015-12-03
{...}
我需要捕获块:
## [1.0.0] - 2017-06-20
### Added
{...}
### Changed
{...}
### Removed
{...}
更新 #1
我发现一个与(?=\n## .*?)(\n## .*?)(?=\n## |$) 一起工作(见regex101.com),现在只需要打印Match 2
有什么帮助吗?谢谢!
【问题讨论】:
标签: regex bash file split grep