سؤال

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

هل كانت مفيدة؟

المحلول

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

نصائح أخرى

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

You can say:

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

For your input, it'd print:

445 sss
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top