How to catch date and time from log files?

Hi,

Appli.log files contain the below data which updates continuously

================================================== ===============

Tuple Created OrderEntry|66|39.0|ADML.L|16.89|GBP||GFD|000002889 41ORLO1|GB00B02J6398|80|XLON|UHORIZON|null|2011-05-09 17:14:59.241+0000|UHORIZON||null|LIM|null|1

Tuple Created OrderEntry|83|10.0|ISA.L|6.355|GBP||GFD|0000028894 3ORLO1|GB00B09LSH68|80|XLON|UHORIZON|null|2011-05-09 17:29:53.339+0000|UHORIZON||null|LIM|null|1

Tuple Created OrderEntry|83|100.0|MSFT.O|0.0|USD|RICHU|GFD|00000 288946ORLO1|US5949181045|65|XNAS|DMA_FIXIN|null|20 11-05-09 17:45:14.280+0000|UHORIZON||null|LIM
.
.
.
.
.
.
.
.

file update continuously

================================================== =============

from above data ...
i want to catch the date and time differntly means

date contains only the data---->2011-05-09
time .....>17:14:59

$Date=2011-05-09
$Time=17:14:59

please help me to catch this date and time

Thanks in advance....

Regards,
Priyanka

bash-3.00$ nawk -F"\|" '{date=substr($15,1,10); time=substr($15,12,8);print date,time}' /tmp/myfile
2011-05-09 17:14:59

Assuming you want the last record in the file.

Date=`tail -1 filename|cut -d\| -f15|awk ' { print $1 } '`
Time=`tail -1 filename|cut -d\| -f15|awk ' { print substr($2,1,8) } '`

Given requirement does not look precise. If you just need last updated date then use tail command along with sed command. The below would fetch all the available date and time patterns from your log file assign it to the respective variables..

time=$(sed 's/.*|[^|]* \([0-9:]*\).*/\1/' inputfile)
date=$(sed 's/.*|\([0-9-]*\) .*/\1/' inputfile)
1 Like

we all missing a point here, if the file is continuously updated then there is no guarantee that the last record will be full (with all fields of our assumption) it may contain just one char or few chars.

So we need make check that the last record is full by checking the number of fields. If it is full then take necessary date and time.

Hi,

Thanks to all ,

actully my final aim is to display the output like below-

date:2011-05-09

Time:
17:14:59
17:29:53
17:45:14
.
.
.
.

you mean for each date you want to display the times of records ?

Hi michaelrozar17,

Thanks for helping me,

as i have mentioned in last post plz help me to disply the output as shown in below:

date:2011-05-09

Time:
17:14:59
17:29:53
17:45:14
.
.
.
.

Regards,
Priyanka

---------- Post updated at 01:22 PM ---------- Previous update was at 01:22 PM ----------

my log file contain only single date Eg:2011-05-09

which does not change.

Only the time is going to update contionuously ...

so i want an output of time should be like this

Time:
17:14:59
17:29:53
17:45:14
.
.
.
.

updated time should display on each separate line

 
nawk -F"\|" '{ if (NR==1){ printf ("Date :%s\nTime:%s\n",substr($15,1,10),substr($15,12,8)) } else {print substr($15,12,8)}}' /tmp/myfile

Its the same sed as in post# 4 except, the highlighted ones. If you need the output in a single command then you would need to resort to awk solutions..

sed -n 's/.*|\([0-9-]*\) .*/Date:\1/p' inputfile | uniq
sed -n 's/.*|[^|]* \([0-9:]*\).*/\1/p' inputfile

i am new to shellscript.can u plz tell me awk function here...

did u try my awk ?

nawk -F"\|" '{ if (NR==1){ printf ("Date :%s\nTime:%s\n",substr($15,1,10),substr($15,12,8)) } else {print substr($15,12,8)}}' /tmp/myfile

nawk is advance than awk

but the above code will work for awk also. just replace nawk with awk