Hamidreza Sahraei
Hamidreza Sahraei

Reputation: 627

Get the text between first occurrence of two patterns with sed

I have a CHANGELOG.md file from which I want to get the unreleased section text with help of sed command. My file has a structure like this:

[Changelog]
============

## [Unrelease]

## Added
- Text1

## Fixed
- Text2

## [5.0.0]

## Added
- Text3

## [4.0.0]

## Fixed
- Text4

In this case, the desired output is:

## Added
- Text1

## Fixed
- Text2

My idea is to get the text somehow with sed between the first line that start with ## [Unreleased] and the fisrt line which start with ## [a number.

Do you know how can do such thing with sed? Or is there a better way for doing this?

Upvotes: 0

Views: 413

Answers (4)

Digvijay S
Digvijay S

Reputation: 2705

using awk

awk '/\[Unrelease\]/,/[0-9]/'

Demo:

$cat file.txt 
[Changelog]
============

## [Unrelease]

## Added
- Text1

## Fixed
- Text2

## [5.0.0]

## Added
- Text3

## [4.0.0]

## Fixed
- Text48,2524
8,1215
-9,25%
-5,32%
$awk  '/\[Unrelease\]/,/[0-9]/'  <  file.txt
## [Unrelease]

## Added
- Text1
$

Upvotes: 1

potong
potong

Reputation: 58361

This might work for you (GNU sed):

sed -n '/^## \[Unrelease\]/{n;:a;n;/^## \[[0-9]/q;p;ba}' file

Turn off implicit printing -n.

On encountering a line beginning ## [Unrelease] , throwaway that line and the next and print further lines until a line beginning ## [ followed by an integer, and then quit.

Upvotes: 1

Quas&#237;modo
Quas&#237;modo

Reputation: 4004

The task is certainly better carried out with awk

awk '
  /## \[Unrelease\]/{flag=1;next}
  /## \[[0-9]/{flag=0}
  flag                            #Prints the line if flag!=0
' file

Upvotes: 1

Shawn
Shawn

Reputation: 52334

You can use an address range to implement your idea with sed, but it'll also include the section lines. They can easily be trimmed out, though:

$ sed -n '/^## \[Unrelease\]/,/^## \[[0-9]/p; /^## \[[0-9]/q' input.txt | sed '1d;$d'

## Added
- Text1

## Fixed
- Text2

(This does assume that the unreleased bit comes before any numbered section, like in your example, because it stops processing after the first numbered one for efficiency's sake)

Upvotes: 1

Related Questions