1

This question already has an answer here:

I have an inputfile that looks like this:

<event>
foo
bar
</event>
<event>
random text
</event>

I would like to split this up into several output files, such that each file has exactly one <event>...</event>.

I tried doing this with awk, but it is not clear to me how I can redirect the output to different output files. Is it possible? Or do I have to resort to something like e.g. python?

marked as duplicate by don_crissti, Archemar, Jeff Schaller, roaima, Gilles shell-script Apr 2 '17 at 23:43

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2

If we told re awk

awk '
    /<event>/{
        start=1
        n++
    }
    start{
        print >"output" n
    }
    /<\/event>/{
        start=0
        close("output" n)
    }
    ' input.file

Not the answer you're looking for? Browse other questions tagged or ask your own question.