0

I am trying to find out the decrements in a column and if found then print the last highest value.

For example:

From 111 to 445 there is a continous increment in the column.But 333 is less then the number before it.

111 aaa  
112 aaa  
112 aaa  
113 sdf  
115 aaa  
222 ddd  
333 sss  
333 sss  
444 sss  
445 sss  
333 aaa<<<<<<this is less then the number above it (445)  

If any such scenario is found then print 445 sss

ole
  • 5,166
  • 5
  • 29
  • 57

3 Answers3

0

Like this, for example:

$ awk '{if (before>$1) {print before_line}} {before=$1; before_line=$0}' a
445 sss

What is it doing? Check the variable before and compare its value with the current. In case it is bigger, print the line.

It works for many cases as well:

$ cat a
111 aaa
112 aaa
112 aaa
113 sdf
115 aaa  <--- this
15 aaa
222 ddd
333 sss
333 sss
444 sss
445 sss  <--- this
333 aaa
$ awk '{if (before>$1) {print before_line}} {before=$1; before_line=$0}' a
115 aaa
445 sss
fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • I just need line above the decremented value, for example "445 sss" –  Oct 23 '13 at 10:54
0

Store each number in a single variable called prevNumber then when you come to print the next one do a check e.g. if (newNumber < prevNumber) print prevNumber;

dont really know what language you are using

0

You can say:

awk '$1 > max {max=$1; maxline=$0}; END{ print maxline}' inputfile

For your input, it'd print:

445 sss
devnull
  • 118,548
  • 33
  • 236
  • 227