[PLUG] Help with log file merge script?

Russell Senior seniorr at aracnet.com
Wed Sep 24 00:52:02 UTC 2003


>>>>> "Sean" == Sean, Sharon and Kyle Harbour <sharbours at yahoo.com> writes:

Sean> Can someone provide me with an example on how to do this? I have
Sean> two log files that are in this format, a date stamp and a value:

Sean> log file 1 Log file 2

Sean> 200309231012 50 200309231012 25 200309231013 75 200309231013 50
Sean> ...

Sean> I want to create a third log file that is has the aggregate
Sean> values of the first two log files, that looks like this:

Sean> log file aggregate of 1 and 2

Sean> 200309231012 75 200309231013 125 ...

Sean> What's a quick way to do this?  

Here's a first approximation (not tested):

  sort logfile1 logfile2 | awk '$1 != last { print last, sum ; sum = $2 } $1 == last { sum += $2 } { last = $1 } END { print last, sum }' > logfile3

This gives you an extra line at the top, but you can get rid of that
by branching around the first "print" if "last" is empty (or the lazy
way, by just stripping the first line of output with "tail -n +2").

If you are certain that the two input files are already sorted, then
you can give sort the "-m" option which will be faster.


-- 
Russell Senior         ``shtal latta wos ba padre u prett tu nashtonfi
seniorr at aracnet.com      mrlosh''  -- Bashgali Kafir for ``If you have
                         had diarrhoea many days you will surely die.''




More information about the PLUG mailing list