Parse multiple sections of data into separate files

awksedtext processing

I have a concatenated log file with multiple logs inside that I'm trying to parse out into individual log files. I will later rename them to the date/time of each. Each log is separated by "— LOG REPORT —".

So far I have:

sed -n '/--- LOG REPORT ---/,/--- LOG REPORT ---/p' logname.log > test.out

However, as you can imagine, that only outputs the first instance of the pattern. I looked over the man page for sed and I'm not convinced it can output multiple files. Perhaps I could keep extracting from a file until it's empty but that seems like too much work. How I can achieve this? Maybe I should be using awk instead?

Example of input file filename.log

--- LOG REPORT ---
Mary
Had
A
Little
Lamb
--- LOG REPORT ---
Her
Fleece
Was
White
As
Snow

Desired output:

In filename_1.log

--- LOG REPORT ---
Mary
Had
A
Little
Lamb

In filename_2.log

--- LOG REPORT ---
Her
Fleece
Was
White
As
Snow

Best Answer

How about something like

awk '/--- LOG REPORT ---/ {n++;next} {print > "test"n".out"}' logname.log
Related Question