Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
share|improve this question
    
Updated, thanks –  Stradigos Aug 20 at 14:13

3 Answers 3

up vote 6 down vote accepted

How about something like

awk '/--- LOG REPORT ---/ {n++;next} {print > "test"n".out"}' logname.log
share|improve this answer
    
Oh wow, that's brilliant. So 'next' is what would select the whole log data until the next occurrence, or end of file, and then n increments the filename. Nice. –  Stradigos Aug 20 at 14:17
2  
The next just prevents the --- LOG REPORT --- record separator line from getting included in the output file(s). You can remove it if you want to include that - I just guessed that is redundant. –  steeldriver Aug 20 at 14:32

How about this command?

csplit logname.log /---\ LOG\ REPORT\ ---/ {*}

Testing

cat logname.log
--- LOG REPORT ---
Mary
Had
A
Little
Lamb
--- LOG REPORT ---
Her
Fleece
Was
White
As
Snow

After running the above command, the output I get is,

cat xx01
--- LOG REPORT ---
Mary
Had
A
Little
Lamb
cat xx02
--- LOG REPORT ---
Her
Fleece
Was
White
As
Snow
share|improve this answer

Wrong tool, use either perl or awk e.g.

cat > splitFileByLogReport.pl <<EOF
#!/usr/bin/perl

undef $/;
$_ = <>;
$n = 0;

for $match (split(/(?=--- LOG REPORT ---)/)) {
      open(O, ">$ARGV[$argnum]" . ++$n);
      print O $match;
      close(O);
}

EOF

Then to run:

perl splitFileByLogReport.pl yourFile.txt
share|improve this answer
    
There's definitely more than one way to skin a cat. I think I'm going to stick with awk for now though. –  Stradigos Aug 20 at 14:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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