awk find the first matching row

I would like to find the first matching row and continue to search from the matching row. if column3 equal to 1 in the row, find the next matching row based on the criteria below. column3 in the matching row equal to 0 and column2 equal to column2 in the matching row

test.csv - test data

2014-09-01 00:01:30|T1|1|0|RE|Y
2014-09-01 00:03:05|T1|0
2014-09-17 23:28:13|T2|1|1|RE|N
2014-09-17 23:29:14|T2|1|0|RE|N      
2014-09-17 23:31:16|T2|0
2014-09-17 23:31:18|T2|0

expected output

 
2014-09-01 00:01:30|2014-09-01 00:03:05|T1|0|RE|Y
2014-09-17 23:28:13|2014-09-17 23:31:16|T2|0|RE|N

I have tried using the awk below, but the result is not correct.

 
awk '{if (($3=="0" && $2==B[2] && B[3]=="1"))
{print B[1], $1, B[2], B[4], B[5], B[6]; next}
{split($0,B)}}' FS=\| OFS=\| test.csv

result

 
2014-09-01 00:01:30|2014-09-01 00:03:05|T1|0|RE|Y
2014-09-17 23:29:14|2014-09-17 23:31:16|T2|0|RE|N
2014-09-17 23:29:14|2014-09-17 23:31:18|T2|0|RE|N

What criteria do you want?

You have shown a program which doesn't do what you want. That's unfortunately not actually helpful in describing what you actually do want. If it was, it'd work, and you wouldn't need to ask.

I see three lines with column 3 as 1, but apparently you only want two of them? Please explain.

Yes there are 3 lines with column3 as 1.
The matching row for the third line (2014-09-17 23:28:13|T2|1|1|RE|N) is fifth line (2014-09-17 23:31:16|T2|0).
The result will be 2014-09-17 23:28:13|2014-09-17 23:31:16|T2|0|RE|N

After that, i would like to continue to search from fifth line to find the line with column3 as 1.

the description is a bit 'muddy', but see if that helps.
awk -f chail.awk myFile where chail.awk is:

BEGIN {
  FS=OFS="|"
}
$3=="1" && !($2 in f) {
   f[$2]=$0
   next
}
$3=="0" && $2 in f {
  n=split(f[$2], a, FS)
  printf("%s%c%s%c%s%c%s%c%s\n", a[1], OFS, $1, OFS, $2, OFS, $3, OFS, a[5])
}

Try:

awk '$3==1 && !p{$3=$2; p=$0} $3==0 && p{n=$1; $0=p; $2=n; p=x; print}' FS=\| OFS=\| file

---
EDIT FS and OFS had fallen off.... Thenks Ravinder...

Hello Scrutinizer,

Nice code, I think we can add FS, OFS values to same.

awk -F"|" '$3==1 && !p{$3=$2; p=$0} $3==0 && p{n=$1; $0=p; $2=n; p=x; print}' OFS="|" Input_file

Thanks,
R. Singh

awk -F'|' '$3==1 && !a[$2] {a[$2]=$1; b[$2]=($5 OFS $6); next} $3==0 && a[$2] {print a[$2], $0, b[$2]; delete a[$2]}' OFS='|' file

Output:

2014-09-01 00:01:30|2014-09-01 00:03:05|T1|0|RE|Y
2014-09-17 23:28:13|2014-09-17 23:31:16|T2|0|RE|N

Hi R. Singh

I hit syntax error when invoking the command.

awk: syntax error near line 1
awk: bailing out near line 1

Can help to explain the logic for the command?

Thanks

---------- Post updated at 08:51 PM ---------- Previous update was at 08:44 PM ----------

the awk command is working after i use /usr/xpg4/bin/awk .
btw, can help to explain the awk logic?