LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   compare two values using any script in a file. (http://www.linuxquestions.org/questions/programming-9/compare-two-values-using-any-script-in-a-file-4175455990/)

rajes4india 03-29-2013 01:54 AM

compare two values using any script in a file.
 
Hello all,

i am new to scripting. In have to compare third column with the previous line 3rd column value and if the value is greater than or equal to 100 i have to print the line. i have a data like below i my file.

B 23 3233555383 ArpIndex
B 23 3233555390 ArpLock
E 23 3233555397 ArpLock
B 23 3233555407 ArpUnLock
E 23 3233555415 ArpUnLock
E 23 3233555422 ArpIndex

manual example:

3233555390 - 3233555383 = 7. like this i have to compare and if the value is >= 100. i have to print the line.

thanks in advance.

thanks
Rajesh

pan64 03-29-2013 03:54 AM

do you know awk? it would be quite easy:
awk ' m-$2 > 100 { print } { m=$2 } '

rajes4india 03-29-2013 04:43 AM

Quote:

Originally Posted by pan64 (Post 4920967)
do you know awk? it would be quite easy:
awk ' m-$2 > 100 { print } { m=$2 } '

Thanks for your response.

unluckly its not working.

i have created the above line in a file named "file".

[root@RaJesH tmp]# cat file
B 23 3233555383 ArpIndex
B 23 3233555390 ArpLock
E 23 3233555397 ArpLock
B 23 3233555407 ArpUnLock
E 23 3233555415 ArpUnLock
E 23 3233555422 ArpIndex

and i am running awk as per suggestion.

awk ' m-$3 > 3 { print } { m=$3 } ' file.

it should not print line one and two (3233555390 - 3233555383 ) = 7. Even the difference is more than 3.

any idea/ suggestion.

pan64 03-29-2013 05:58 AM

that should have been found:
$3-m

rajes4india 03-29-2013 06:36 AM

[QUOTE=pan64;4921002]that should have been found:
$3-m[/
it works ....

thanks a lot.

pan64 03-29-2013 06:40 AM

glad to help you
if you really want to say thanks just press yes (bottom right corner) and also please mark thread solved if you were satisfied.


All times are GMT -5. The time now is 05:31 AM.