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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to compare current record,with next and previous record in awk without using array?
# 1  
Old 04-14-2013
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
Code:
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
Code:
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
Code:
previous+3 < current < next+3 

or

previous-3 > current < next-3

print BAD

being beginner getting many syntax error those who know please help
# 2  
Old 04-14-2013
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.
# 3  
Old 04-14-2013
I think without array it may be little difficult to handle
you could try
Code:
getline and  save column

in some variable and then you can compare

something like this

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

I am not sure about thisSmilie


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 Smilie

Code:
                           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
Code:
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


Last edited by Akshay Hegde; 04-14-2013 at 12:28 PM..
# 4  
Old 04-14-2013
Not clear. Why is neither your record 9 nor second record 0 "BAD"? Please explain your logics in plain English.
# 5  
Old 04-15-2013
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
Code:
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
# 6  
Old 04-15-2013
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 Smilie)

It would be interesting to compare the performance of some C program vs some awk solution.
# 7  
Old 04-15-2013
Yes hanson44...I am also interested...
Thats why I left $0 records by
Code:
j!=1

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to compare previous and current item in for loop in bash?

Hey, I am trying to compare formated login and logout dates from one user at a host which I have stored in a tmp directory in order to find out the total login time. I need to compare them in order to find overlapping intervals. At first I tried to store each log in and logo date in an array... (3 Replies)
Discussion started by: Mumu123
3 Replies

2. Shell Programming and Scripting

Need code for updating second record to first record in shell scripting

Hi,, I have requirement that i need to get DISTINCT values from a table and if there are two records i need to update it to one record and then need to submit INSERT statements by using the updated value as a parameter. Here is the example follows.. SELECT DISTINCT ID FROM OFFER_GROUP WHERE... (1 Reply)
Discussion started by: Samah
1 Replies

3. Shell Programming and Scripting

Replace a string for every record after the 1st record

I have data coming in the below format for each record <?xml version="1.0" encoding="UTF-8" standalone="no"?><test_sox xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><testdetials>....</test_sox> <?xml version="1.0" encoding="UTF-8" standalone="no"?><test_sox... (8 Replies)
Discussion started by: dsravanam
8 Replies

4. Shell Programming and Scripting

Extract timestamp from first record in xml file and it checks if not it will replace first record

I have test.xml <emp><id>101</id><name>AAA</name><date>06/06/14 1811</date></emp> <Join><id>101</id><city>london</city><date>06/06/14 2011</date></join> <Join><id>101</id><city>new york</city><date>06/06/14 1811</date></join> <Join><id>101</id><city>sydney</city><date>06/06/14... (2 Replies)
Discussion started by: vsraju
2 Replies

5. Shell Programming and Scripting

awk - compare 1st 15 fields of record with 20 fields

I'm trying to compare 2 files for differences in a selct number of fields. When differnces are found it will write the whole record of the second file including appending '|C' out to a delta file. Each record will have 20 fields, but only want to do comparison of 1st 15 fields. The 1st field of... (7 Replies)
Discussion started by: sljnk
7 Replies

6. Shell Programming and Scripting

Reject the record if the record in the next line does not begin with 2.

Hi, I have a input file with the following entries: 1one 2two 3three 1four 2five 3six 1seven 1eight 1nine 2ten 2eleven 2twelve 1thirteen 2fourteen The output should be: (5 Replies)
Discussion started by: supchand
5 Replies

7. Shell Programming and Scripting

Reject the record if the record in the next line does not satisfy the pattern

Hi, I have a input file with the following entries: 1one 2two 3three 1four 2five 3six 1seven 1eight 1nine 2ten The output should be 1one 2two 3three 1four 2five 3six (2 Replies)
Discussion started by: supchand
2 Replies

8. Shell Programming and Scripting

AWK Compare previous value with current.

Hi, I have one small doubt how to go ahead and process the below requirement. File Content 1,abc,10 2,xyz,11 3,pqr,12 4,pqr,13 5,pqr,14 Output file expected: 1,mnq,1 1,ddd,2 1,qqq,3 1,sss,4 1,ddd,5 1,eee,6 1,fff,7 1,ddr,8 1,rrd,9 (3 Replies)
Discussion started by: dikesm
3 Replies

9. UNIX and Linux Applications

sqlite: calculating with dates - compare current date minus 6 months with stored record

Hi I have a table with name, date in format DD.MM.YYYY. I need to something like this (I try to explain in pseudo code) if SYSDATE (current date) minus 6 months > $expiry date print OK else print NOK with $name and $expiry date I know this is possible with Oracle. How to do this... (0 Replies)
Discussion started by: slashdotweenie
0 Replies

10. Shell Programming and Scripting

Help for record (2 dimensional array.)

I am going to develop a address book using the shell scripting commands without sed, awk, .... I am thinking to apply the concept of 2 dimenstional array. Can I create a two dimensional array for the insertion/updation/deletion of record in unix. If yes then tell me plz or recommend me some... (1 Reply)
Discussion started by: murtaza
1 Replies
Login or Register to Ask a Question