shellterminalzsh

Split text to files between regular repeating text strings using terminal


I have several blocks of text between regular intervals that I am trying to use shell to parse into separate files. The file resembles this format:

<Line delimiter>
The lines of text to extract
<Line delimiter>
Other lines that are to be extracted but
also have an unpredictable length
<Line delimiter>
...
...
<Line delimiter>
and so forth

I was attempting a Split command and grepping between lines as:

for line in $(grep -n 'Line Delimiter' target.txt | cut -d ':' -f 1); do split -l $line target.txt  ; done 

With also, this with a head | tail pipe as what appeared to be one of the eloquent solutions seen in this example. I have searched it out a little more, with a few other attempts but I feel like this should be relatively easy but accomplishing this feat for more than 1 iteration is proving difficult. Any help thanks in advance.


Solution

  • trivial example hopefully be helpful

    $ cat input.txt
    <Line delimiter>
    The lines of text to extract
    <Line delimiter>
    Other lines that are to be extracted but
    also have an unpredictable length
    <Line delimiter>
    these should also go...
    into another file ...
    <Line delimiter>
    and so forth
    $
    $ #repeat the split as many times as the match is made
    $ csplit -q input.txt '/<Line delimiter>/' '{*}'
    $
    $ # file xx00 is empty as the first occurence of <Line delimiter> is line 1   
    $ cat xx01
    <Line delimiter>
    The lines of text to extract
    $ cat xx02
    <Line delimiter>
    Other lines that are to be extracted but
    also have an unpredictable length
    $ cat xx03
    <Line delimiter>
    these should also go...
    into another file ...
    $ cat xx04
    <Line delimiter>
    and so forth
    

    check out the csplit manpage for additional info.