Ubuntu – How to use split file into two based on line start

command lineregexsplit

I have this file 1.txt:

-e a
b
-e c

d
-e e
f

I want to split it into the following two files.

2.txt

-e a
-e c
-e e

3.txt

b
d
f

where 2.txt contains all the lines starting with -e, and 3.txt contains all the other lines. Extra newlines (such as the extra newline in the middle of the original) can be ignored or kept, and order doesn't matter.

I've tried using split, but it doesn't look like that allows me to use a pattern for splitting (instead a fixed number of lines per split file).

Best Answer

Using grep:

grep -E '^-e' 1.txt >2.txt
grep -E '[^-]' 1.txt >3.txt