Kwoak
Kwoak

Reputation: 13

How to use same pattern with multiples occurrences?

I want to extract text from my changelog to put it in a variable.

But i can't tell to sed/awk that i want the text between the first and the second occurrence of my pattern.

Here is my changelog:

This is the changelog of the application
You may find Features and Bug Fixes

# [1.5.0]() (2019-08-20)

### Fixes

- **iOS** Correct some display bugs

# [1.4.0]() (2019-08-15)

### Features

- **Options** New function

...

I want the output to be:

# [1.5.0]() (2019-08-20)

### Fixes

- **iOS** Correct some display bugs

I already tried:

sed -n '/# \[/,/# \[/p; /# \[/q' CHANGELOG.md

# [1.5.0]() (2019-08-20)


awk '/# \[/ && ++n == 2, /# \[/' < CHANGELOG.md

# [1.4.0]() (2019-08-15)

I use '# \[' as my pattern. Anyone can help?

Upvotes: 0

Views: 97

Answers (1)

Aaron
Aaron

Reputation: 24812

You could use the following GNU sed command :

sed -n '/^# \[/{:l;p;n;/^# \[/q;bl}'

Explanation :

  • -n : do not print lines by default
  • /^# \[/{...} : when a version line is encountered, execute the commands inside {...}
  • :l : label l, denoting the start of a loop
  • p;n : print the current line, start working on the next line
  • /^# \[/q if that next line is a version line, have sed exit
  • bl : (otherwise) jump to the label l, looping until a version line is encountered

Upvotes: 2

Related Questions