regexgrep

How to make multi-line regexp work in grep?


Let's take the following example text:

some:
- 1test@test.nl\\
- test@test.nlk
- test@test.nl
- test@test.nl test
- some@test.nl


list:
- 1test@test.nl
- test@test.nlk
- test@test.nl
- test@test.nl test
- some@test.nl

I only want to match a specific email that comes before list: e.g. we want to see if test@test.nl is in the first list (some:). I made the following regex:

(- test@test.nl\s*\n).*list:

which seems to do exactly what we want when trying online.

However, when I try to use it with grep, it does not work. I tried with -e, -P and -E. The errors I get: invalid option -- ' ' or no match at all.

How can one use the given regex with grep?

note: we are on a stripped down linux version, we are only allowed to use commands that are in standard linux


Solution

  • You can also grep everything before 'list:' and pipe to grep for email:

    grep -B 1000000 "list:" example.txt | grep -P "test@test.nl\s*$"