samedi 7 février 2015

{awk} How to read a line and compare a $ it with its next/previous line?


Vote count:

0




Here is my first post :) I'm from France but working in the seismic industry. I'm currently working on a Shell script and its last thing is not working, I'm running out of ideas...


The command below is used to read an input file containing 7682 lines: I use the --field-separator then converted some fields into what I need, and the grep got rid of the 2 first lines I do not need.



awk --field-separator=";" '($1<15) {print int(a=(($1-1)/480)+1) " " ($1-((int(a)-1)*480)) " " (20*log($6)/log(10))}' 218_DW.txt | grep -v "0 480 -inf"


I used ($1<15) so that I only print 14 lines, better for testing. The output I get is exactly what I want, but, there is more I need to do on that:



1 1 48.2872
1 2 48.3021
1 3 48.1691
1 4 48.1502
1 5 48.1564
1 6 48.1237
1 7 48.1048
1 8 48.015
1 9 48.0646
1 10 47.9472
1 11 47.8469
1 12 47.8212
1 13 47.8616
1 14 47.8047


From above, $1 will increment from 1-16, $2 from 1-480, it's always continuous, so when it gets to 16 480 47.8616 it restarts from 2 1 47.8616 until last line is 16 480 10.2156


So I get 16*480=7680 lines


What I want to do is simple, but, I don't get it :)


I want to compare the current line with the next one. But not all fields, only $3, it's a value in dB that decreases when $2 increases.


In example:



The current line is 1 1 48.2872=a
Next line is 1 2 48.3021=b


If [ (a - b) > 6 ] then print $1 $2 $3


Of course (a - b) has got to be an ABS value, always > 0, how it's called in English, absolute value? :) Sorry for my English :)


The beast will be to be able to compare the current line (the $3 only) with it's next and previous line ($3). Something like this:



1 3 48.1691=a
1 4 48.1502=b
1 5 48.1564=c


If [ ABS(b - a) > 6 ] OR If [ ABS(b - c) > 6 ] then print $1 $2 $3


But of course first line can only be compared with its next one and the last one with its previous one. Is it possible?


I'm coming here because I'm stuck, and can't get it to work, and after reading here I see that you guys are crazy with awk :)


Thanks :)


raph



asked 59 secs ago







{awk} How to read a line and compare a $ it with its next/previous line?

Aucun commentaire:

Enregistrer un commentaire