Regular Expression – Splitting Text Files Based on a Pattern

regular expressionsplit

I have a text file which I want to split into 64 unequal parts, according to the 64 hexagrams of the Yi Jing. Since the passage for each hexagram begins with some digit(s), a period, and two newlines, the regex should be pretty easy to write.

But how do I actually split the text file into 64 new files according to this regex? It seems like more of a task for perl. But maybe there's a more obvious way that I'm just totally missing.

Best Answer

This would be csplit except that the regex has to be a single line. That also makes sed difficult; I'd go with Perl or Python.

You could see if

csplit foo.txt '/^[0-9][0-9]*\.$/' '{64}'

is good enough for your purposes. (csplit requires a POSIX BRE, so it can't use \d or +, among others.)

Related Question