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 prstat log, as you all know the date and time is recorded on the top of the log file, I would like to add that date and time to the begging of each line until it gets to a new date, then I would do the same for that part of the log.

Is this possible?, is there a way to do this through commands or a script, I would've created a script to do so, but it's too late as the data has already been collected.

share|improve this question
add comment

2 Answers 2

up vote 1 down vote accepted

Assuming date format is yyyy-mm-dd hh:mi:ss you can use this perl script:

#!/usr/bin/perl -w

open(FILEH,"<","your.log") or die "cannot open file";
my $date = "0000-00-00 00:00:00";

while(<FILEH>) {
    if( /^([A-Z][a-z]{2}\s[A-Z][a-z]{2}\s[0-9]+\s[0-2][0-9]:[0-5][0-9]:[0-5][0-9]\s[0-9]{4})$/ ) {
        $date = $1;
    }
    else {
        print "$date $_";
    }
}

Change your.log with full path of your log file.

share|improve this answer
    
I've modified the pattern for date. Which is the full path? –  Laurentiu Roescu Dec 13 '12 at 7:27
    
The regex matched the timestamp in the logs correctly, but for some reason its printing the default 000-00-00 00:00:00 on each line, I cant see why, the perl script is correct by finding the timestamp and printing it when it reaches a line where it doesnt, but I cant find out why its printing the default value only... Adding the full path worked btw –  I AM L Dec 13 '12 at 7:34
    
If it is printing the default I don't think it matches the timestamp correctly. Can you give me a real timestamp example? –  Laurentiu Roescu Dec 13 '12 at 7:46
    
Actually this is my fault, I did not explain my question properly, you see a prstat log has multiple chunks of lines, after another second or so, another chunk is then logged with a new timestamp above it, on top of each chunk looks something like this: ### (1311344015) Thu Dec 13 01:03:35 2012 –  I AM L Dec 13 '12 at 7:50
    
I tested your regex with an online perl regex tester and its matching it perfectly –  I AM L Dec 13 '12 at 7:51
show 2 more comments

i don't know python much more, but i tried

#!/usr/bin/env python

Each_line = [line.split() for line in open('/tmp/prstat.log')]

for n in xrange(len(Each_line)):
    if '2008' in Each_line[n]:
         print " ".join(Each_line[n])," ".join(Each_line[n+1])
share|improve this answer
add comment

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.