I don�t know if it�s the best idea...but works....
your sugestion do something like this?
BEGIN {
FS="|";
c1 = -1;
c4 = -1;
c5 = -1;
c8 = 0;
time = 0;
}
{
if (($1 == c1) && ($4 == c4) && ($5 == c5))
{
# accumulate value $8
if ($7 <= time)
{
c8 = c8 + $8
# updates variable time
time = $7 + $8 + 2
} #write to output
if ($9 == 4)
{
print c1"|"c2"|"c3"|"c4"|"c5"|"c6 "|"c7 "|"c8"|1|"c10"|"c11"|"c12"|"c13"|"c14"|"c15 >> file_complete
close(file_complete) #restart variables
time = 0
c8 = 0
next
}
}
else
{ # the record don't have the same key has the last record
if (c1 == -1)#when reads the 1st line of the file only keeps the relevant fields of the first record
{
if ($9 == 2) #save the fields from the beginning of the call, to put in output
{
c1 = $1
c2 = $2
c3 = $3
c4 = $4
c5 = $5
c6 = $6
c7 = $7
c8 = $8
c10 = $10
c11 = $11
c12 = $12
c13 = $13
c14 = $14
c15 = $15
time = $7 + $8 + 2
next
}
}
if (c1 != -1)#print output of the previous
{
print c1"|"c2"|"c3"|"c4"|"c5"|"c6"|"c7"|"c8"|1|"c10"|"c11"|"c12"|"c13"|"c14"|"c15 >> file_complete
close(file_complete)
# save the current record if is the beginning of the call
if ($9 == 2)
{ # save the fields from the beginning of the call, to put in output
c1 = $1
c2 = $2
c3 = $3
c4 = $4
c5 = $5
c6 = $6
c7 = $7
c8 = $8
c10 = $10
c11 = $11
c12 = $12
c13 = $13
c14 = $14
c15 = $15
time = $7 + $8 + 2
}
}
}
}
END { #print the output for the case that not caught the last registration with closing ($ 9 = 4, but caught with $ 9 = 3)
if ($9 == 3)
{
print c1 "|" c2 "|" c3 "|" c4 "|" c5 "|" c6 "|" c7 "|" c8 "|1|" c10 "|" c11 "|" c12 "|" c13 "|" c14 "|" c15 >> file_complete
close(file_complete)
}
}
You can save yourself a lot of time by setting OFS to "|" and just doing print $0 instead of every variable independently. Also, while you can do it in one big awk program, its easier -- syntactically -- to break it up into multiple awk programs. Now, I don't mean multiple instances of awk. Every awk invocation can contain multiple programs, like this:
If condition1 matches, program1 will run. If condition2 matches, program2 will run (regardless of whether program1 ran or not). If you want program1 to stop processing and go to the next line, you use "next;". If you want program2 to get the next line now, you can do "getline;". (That might be GNU specific.)
Also, when you post on this forum, it helps to embed your code in [code] tags. It keeps the spacing.