We have
...a file containing paragraphs, splitted by 2 newlines \r\n\r\n or \n\n. The paraghraphs themselves may contain single newlines \r\n or \n. The goal is to use a Bash one-liner to match only the first paragraph and to print it to stdout.
E.G.:
$ cat foo.txt
Foo
* Bar
Baz
* Foobar
Even more stuff to match here.
results in:
$ cat foo.txt | <some-command>
Foo
* Bar
I've already tried
...this regex (?s)(.+?)(\r?\n){2}|.+?$ with grep using
- GIT Bash on Windows (GNU grep 3.1),
- Bash on Lubuntu 20.4.1 LTS (GNU grep 3.4) and
- iTerm+Fish on Mac (BSD grep 2.5.1-FreeBSD).
The first two approaches resulted in:
$ grep -Poz '(?s)(.+?)(\r?\n){2}|.+?$' foo.txt
Foo
* Bar
Baz
* Foobar
The approach on Mac failed, due to differences between BSD grep and GNU grep.
But
... on regex101.com this regex works on foo.txt: https://regex101.com/r/uoej8O/1. This may be due to disabling the global flag?