Extract date from files

Hi Guys,

I am having a file in below format:

job1|start|01/01/1988 10:10:10
job2|start|01/01/1988 10:10:10
job1|start|01/01/1988 11:10:10
job1|end|01/01/1988 12:10:10
job2|end|01/01/1988 12:10:10
job3|start|01/01/1988 14:10:10
job4|end|01/01/1988 15:10:10

I need the output like this:

job1|start|01/01/1988 11:10:10|end|01/01/1988 12:10:10
job2|start|01/01/1988 10:10:10|end|01/01/1988 12:10:10
job3|start|01/01/1988 14:10:10
job4|end|01/01/1988 15:10:10

If the job has start it should consider the latest start time my file will have the latest end time only.

If the job is having only start time or end tim , then display start time in the format above like how job3 and job4 is having

I have tried something like this but couldn't able to achieve :

awk '{i=$1; $1=x; A=A $0} $2=="end"{print i A}' FS=\| OFS=\| file

Perhaps this:

awk '
/start/{s[$1]="start|"$3}
/end/{e[$1]="end|"$3;s[$1]=s[$1]}
END{for(k in s) print k,s[k](length(s[k])&&(k in e)?"|":x)e[k] }' FS=\| OFS=\| file
$ cat file
job1|start|01/01/1988 10:10:10
job2|start|01/01/1988 10:10:10
job1|start|01/01/1988 11:10:10
job1|end|01/01/1988 12:10:10
job2|end|01/01/1988 12:10:10
job3|start|01/01/1988 14:10:10
job4|end|01/01/1988 15:10:10
$ awk -F\| '{A[$1] = /start/ ? $0 : $1 in A && /end/  ? A[$1] FS $2 FS $3 : $0 }END{for(i in A)print A}' file

Resulting

job1|start|01/01/1988 11:10:10|end|01/01/1988 12:10:10
job2|start|01/01/1988 10:10:10|end|01/01/1988 12:10:10
job3|start|01/01/1988 14:10:10
job4|end|01/01/1988 15:10:10
1 Like

Thanks Akshay for your snippet which really worked