0

I have a log file that I would like to break up into several files

Table of Contents

*** Configuration Database
*** Logs
*** Configuration Logs
*** Query Logs
    .
    .
    and so on

The issue is, the titles of the section appear again in the body with the appropriate data or logs between the sections, so I’d have to search for the second instance of each title, and copy the data up to the second instance of the next title.

Body

*** Configuration Database
    (Data/Logs)

*** Logs
    (Data/Logs)

*** Configuration Logs
    (Data/Logs)

*** Query Logs
    (Data/Logs)

I’d basically like to take the debug text file, run a script against it, and have it break the file into many text files, one for each section in the Table of Contents, keeping the original intact. So I would basically have debug.txt, Configuration Database.txt, Logs.txt, etc, each having all the data in each section, up to when the new section starts. Any help would be greatly appreciated, as this is a little over my head :)

2

Try this:

awk '/^[*][*][*] /{ if ($0 in seen) fname=$0; else seen[$0];} fname{print>fname}' file

How it works

awk implicitly reads a file line by line. For each line read, we do the following:

  • /^[*][*][*] /{ if ($0 in seen) fname=$0; else seen[$0];}

    For any line that begins with three stars and a space, we check to see if we have seen that line before. If we have, we set fname to that line. Otherwise, we update the associative array seen to indicate that we have seen this line once.

  • fname{print>fname}

    If fname is a non-empty value, then write the current line to the file named fname.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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