Splitting a text file using a separate file of line numbers

I have a text file that contains multiple (~80) OneNote pages all concatenated together, which I'm trying to split into component files for each page. I'm trying to do this by the line numbers of the page titles, as the pages are of variable length, and while I've been able to extract the line numbers into a seperate file I've not been able to figure out how to make the split with them. Eg

Log.txt:

Tuning             //Page Title
09 November 2016   //Date
23:19              //Time
 
Content text...    //Page Content
 
Week 46            //Another title, want to split here
14 November 2016
13:47
 
Text..
More text...       //Content can be over multiple lines

Week 47            //Another title, want to split here
22 November 2016
11:15

Text
etc...

Lines:

1
7
14

Expected output in this example would give three files, each going from the page title down to the last line before the next page title.

log1.txt log2.txt log3.txt

$ cat log1.txt
Tuning             
09 November 2016
23:19

Content text...

$

I found a lot of answers regarding splitting into fixed chunks (eg every 50 lines) which doesn't work here as the sections are of variable length. Most of those around splitting at fixed line numbers dealt with just a few line numbers that could be hardcoded in, eg using head or tail commands.

This answer came really close to what I'm looking for, but again the input of line numbers to split at is very small and can be written directly into the command. I'm using bash on macos, and am quite new to this level of work at the command line and have no real experience using grep, sed, awk etc, so its hard for me to generalise other answers to this particular case.

PS I can include the code I used to get the line numbers if necessary, although I'm sure it's far from optimal. (It involves grepping for the line numbers of the time stamps with a regex, then stripping away the matching text and subtracting 2 from each line to get the page titles)



from Recent Questions - Stack Overflow https://ift.tt/2KMbLLS
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Spring Elasticsearch Operations

Network Error and Timeout on Authorize.net JS

Object oriented programming concepts (OOPs)