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).
Using
grep
:@braemar: Using
grep -v
with the same regexp would erroneously detect blank lines, text lines, etc. Not what was wanted.Here is
awk
solution:A performance test:
Preserving empty lines:
giving
If you prefer to omit empty lines, then add an "any character" regex to the last write:
Here is a
sed
solution by using ofd
elete flag:The above command has two regex, the first
'/^-/!d'
will match to all lines that doesn't start with-
and they will be deleted from the output, the second'/^[[:space:]]*$/d'
will match to all lines that contains only white spaces and they will be deleted from the output.The above command also has two regex, the first
'/^-/d'
will match to all lines that starts with-
and they will be deleted from the output, the second is the same as in the previews case.Another way is to preserve
-n
the normal output ofsed
and thenp
rint only the matched lines:Here is a performance test: