How to compare current record,with next and previous record in awk without using array?

Hi! all

can any one tell me how to compare current record of column with next and previous record in awk without using array

my case is like this

input.txt

0 32
1 26 
2 27
3 34
4 26
5 25
6 24
9 23
0 32
1 28
2 15
3 26
4 24
5 25

output needed

0 32
1 26 
2 27
3 34 BAD
4 26
5 25
6 24
9 23
0 32
1 28
2 15 BAD
3 26
4 24
5 25

just try to implement this logic without using array
and it has to consider first column also that is if next record of column 1 is greater than current record

previous+3 < current < next+3 

or

previous-3 > current < next-3

print BAD

being beginner getting many syntax error those who know please help

I do not understand what you try to do.
Post real data, and no example data. It would help to understand what you like to do.

I think without array it may be little difficult to handle
you could try

getline and  save column

in some variable and then you can compare

something like this

awk '{p=$2;getline;c=$2;getline;n=$2;print p,c,n}'

I am not sure about this:)

but with array you may get output as you expected from following code, I assume you might be trying to detect peak values in file, test with your original file and post the same as Jotne suggested :b:

                           awk ' FNR==NR{
                                         x=NR
                                         for(i=1;i<=NF;i++)
                                         D[x,i]=$i;next
                                        }

                                    END{
                                           for(j=1;j<=x;j++)
                                           if(j!=1)
                                           if(D[j,1]>D[j-1,1] && D[j-1,2]+3 < D[j,2] && D[j,2] > D[j+1,2]+3 || \
                                           D[j-1,2]-3 > D[j,2] && D[j,2] < D[j+1,2]-3)
           
                                                           print D[j,1],D[j,2],"BAD"
                                           else 
                                                           print D[j,1],D[j,2]
                                        }' your file

output

admin@IEEE:~$ sh test.sh 
1 26
2 27
3 34 BAD
4 26
5 25
6 24
9 23
0 32
1 28
2 15 BAD
3 26
4 24
5 25

Not clear. Why is neither your record 9 nor second record 0 "BAD"? Please explain your logics in plain English.

That is because column 1 has 0
Either a zero or any value which is less than previous would be considered as new data
for example

0 xx  data 1
1 xx  data 1
2 xx  data 1
3 xx  data 1
2 xx data 2
3 xx  data 2
4 xx data 2
5 xx data 2
6 xx data 2

Akshay's code is giving some result but very slow, I am having about 4 GB data file

Still not clear why "1 26" (line #2) is not BAD.

  • Not "new data", by your definition.
  • Previous number was 32, so difference is 4.

Can you compile a simple C program? This would be easy to write in C, if you can spell out exactly what you want to do. (And if that does not break the forum rules :D)

It would be interesting to compare the performance of some C program vs some awk solution.

Yes hanson44...I am also interested...
Thats why I left $0 records by

j!=1

Please revise and explain/rephrase that logics.