This is actually quite easy, you just need to think things through first. Lets go back to your original file:
06/26/2017 23:40:40 CAUAJM_I_10082 [aspsun14 connected for IOALPPRXXBD_ALPGLGENFAALL 55443.15215291.1]
06/26/2017 23:40:40 CAUAJM_I_40245 EVENT: CHANGE_STATUS STATUS: STARTING JOB: IOALPPRXXBD_ALPGLGENFAALL MACHINE: aspsun14
06/26/2017 23:40:42 CAUAJM_I_40245 EVENT: CHANGE_STATUS STATUS: RUNNING JOB: IOALPPRXXBD_ALPGLGENFAALL MACHINE: aspsun14
06/26/2017 23:49:19 CAUAJM_I_40245 EVENT: CHANGE_STATUS STATUS: SUCCESS JOB: IOALPPRXXBD_ALPGLGENFAALL MACHINE: aspsun14 EXITCODE: 0
06/27/2017 23:40:23 CAUAJM_I_40245 EVENT: CHANGE_STATUS STATUS: STARTING JOB: IOALPPRXXBD_ALPGLGENFAALL MACHINE: aspsun14
06/27/2017 23:40:24 CAUAJM_I_10082 [aspsun14 connected for IOALPPRXXBD_ALPGLGENFAALL 55443.15236942.1]
06/27/2017 23:40:25 CAUAJM_I_40245 EVENT: CHANGE_STATUS STATUS: RUNNING JOB: IOALPPRXXBD_ALPGLGENFAALL MACHINE: aspsun14
06/27/2017 23:48:19 CAUAJM_I_40245 EVENT: CHANGE_STATUS STATUS: SUCCESS JOB: IOALPPRXXBD_ALPGLGENFAALL MACHINE: aspsun14 EXITCODE: 0
06/28/2017 23:41:36 CAUAJM_I_40245 EVENT: CHANGE_STATUS STATUS: STARTING JOB: IOALPPRXXBD_ALPGLGENFAALL MACHINE: aspsun14
06/28/2017 23:41:37 CAUAJM_I_10082 [aspsun14 connected for IOALPPRXXBD_ALPGLGENFAALL 55443.15258301.1]
06/28/2017 23:41:38 CAUAJM_I_40245 EVENT: CHANGE_STATUS STATUS: RUNNING JOB: IOALPPRXXBD_ALPGLGENFAALL MACHINE: aspsun14
06/28/2017 23:48:47 CAUAJM_I_40245 EVENT: CHANGE_STATUS STATUS: SUCCESS JOB: IOALPPRXXBD_ALPGLGENFAALL MACHINE: aspsun14 EXITCODE: 0
Of this file you only need the lines containing "STATUS: STARTING" and some ending notification. This could perhaps be "STATUS: SUCCESS", but somehow i don't believe that all jobs end that way - what other possible ending codes do you have, because your script will need to cover them too.
Let us go on, assuming for the moment that the only endng condition is "SUCCESS", this can be corrected later.
Next we make up some "rules" what to do with the respective lines, because at some point we need to read our input line by line:
-
When we encounter a "STARTING" line we need to remember two things, the job name and the timestamp.
-
When we encounter a "SUCCESS"-line we need to read also two things: the job name and the timestamp. But we don't need to store ("remember") them: the job name should be searched in our list of remembered (=started) jobs. If it is found, the stored starting time, the end time and the job name is printed.
The last point begs two questions: what are we going to do if we encounter a job with a start but no end? And what are we going to do with jobs with an end but no start?
Here is the skeleton of a shell script that implements what i said above. Neither is it very clever nor very mature, its intention to make it obvious how to implement common reasing like above into code. It also won't take the raised questions into account and implicitly assume that all jobs end with success and every starting job also ends and vice versa.
We start by filtering and displaying only the lines we are interested in:
#! /bin/ksh
typeset infile="/path/to/some/file" # file we read from
typeset cond="" # condition of the job, "STARTING" or "SUCCESS"
typeset name="" # jobs name
grep -e "STATUS: SUCCESS" -e "STATUS: STARTING" "$infile" |\
while read junk junk junk junk junk junk cond junk name junk ; do
echo "name is: $name condition is: $cond"
done
exit 0
Now let that run and watch if: a) the lines are filtered correctly and b) the names and conditions are displayed correctly. You can let the shell split the input lines and use different variables to distribute the split input to (all the ones i am not interested in i name "junk" out of habit), but it is easy to get the number of fields wrong in long lines. So try and verify it before going on. It is good practice make sure everything is correct so far before going on.
Next thing is to implement the "rules" we identified above. First the STARTING-lines:
We need to remember the date and time, so two of the fields not interested in before are now in a variable (junk->date, junk->time).
#! /bin/ksh
typeset infile="/path/to/some/file" # file we read from
typeset date="" # jobs date
typeset time="" # jobs tme
typeset cond="" # condition of the job, "STARTING" or "SUCCESS"
typeset name="" # jobs name
count=1 # counter for the storage arrays
# aname[] # these arrays hold the remembered jobs
# atime[]
grep -e "STATUS: SUCCESS" -e "STATUS: STARTING" "$infile" |\
while read date time junk junk junk junk cond junk name junk ; do
echo "name is: $name condition is: $cond time is: $date $time" # we leave that in for now, to see what the script works on
case $cond in
STARTING)
typeset aname[$count]="$name"
typeset atime[$count]="$date $time"
(( count += 1 ))
;;
esac
done
exit 0
You see we just add to the arrays whe we find a new job, all very easy. The next rule is a a little trickier. Upon encountering such a line we need to search our array for the respective job entry, then print both starting and ending times:
#! /bin/ksh
typeset infile="/path/to/some/file" # file we read from
typeset date="" # jobs date
typeset time="" # jobs tme
typeset cond="" # condition of the job, "STARTING" or "SUCCESS"
typeset name="" # jobs name
typeset -i count=1 # counter for the newest element of the storage arrays
typeset -i i=1 # counter for searching the storage arrays
# aname[] # these arrays hold the remembered jobs
# atime[]
grep -e "STATUS: SUCCESS" -e "STATUS: STARTING" "$infile" |\
while read date time junk junk junk junk cond junk name junk ; do
echo "name is: $name condition is: $cond time is: $date $time" # we leave that in for now, to see what the script works on
case $cond in
STARTING)
typeset aname[$count]="$name"
typeset atime[$count]="$date $time"
(( count += 1 ))
;;
SUCCESS)
(( i = 1 ))
while [ $i -lt ${#aname[@]} ] ; do # walk though the array
if [ "${aname[$i]}" = "$name" ] ; then # we found the corresponding entry
print "${aname[$i]} \t${atime[$i]} \t $date $time"
else
(( i += 1 ))
fi
done
;;
esac
done
exit 0
Again, this is not meant to be put in production as it is. But analysing how we arrived at it and how it works should give you the idea how to create a proper script for your purpose.
I hope this helps.
bakunin