1

I have a simple text file and I want to split it into 2 parts whenever I see 3 (or more) consecutive new lines. For example:

Fiona Gallagher
Frank Gallagher
Sheila Jackson



Dominique Winslow
Kermit

Should create 2 text files:

Fiona Gallagher
Frank Gallagher
Sheila Jackson

and

Dominique Winslow
Kermit

Though similar posts in SO address the problem of splitting text files into 2 parts (this PHP solution, this C# solution, and this Java solution) I could not find a solution in bash. It seems reasonable enough that a neat bash solution is out there somewhere (?). Thanks!

OrenIshShalom
  • 5,974
  • 9
  • 37
  • 87

1 Answers1

2

At the request of OP, a csplit solution that only works with 2 consecutive \n\n or more.


The easiest way would be to use the GNU csplit which any modern bash system has. Suppose bla contains:

Fiona Gallagher
Frank Gallagher
Sheila Jackson

Dominique Winslow
Kermit






a
v
b

then

csplit --suppress-matched -z bla "/^$/" '{*}'

will do what you want, creating

>cat xx00 
Fiona Gallagher
Frank Gallagher
Sheila Jackson
>cat xx01
Dominique Winslow
Kermit
>cat xx02
a
v
b

You can change the xx to any prefix using -f or --prefix=. The --suppress-matched makes sure the thing you split on (empty lines) will not be output and -z ensures consecutive empty lines do not form empty files. {*} Means split as many times as possible.

kabanus
  • 24,623
  • 6
  • 41
  • 74