Reputation: 93
Say you have a file like this:
Rowname
useless number
useless number
1596 1787
1595 1787
1594 1787
1565 1803
1565 1804
I know how to tell awk to skip the useless number
and awk row 5 and 6 based on Rowname
. BUT how can I tell awk to skip the row if the difference is only 1 from the previous row? The output should be:
1596 1787
1565 1803
My problem is that in some cases I get this:
1595 1787
1594 1787
Said another way: I always want the 3rd row from the row name, but I don't want row 4 (or 5 or 6) if the difference from row 3 only are 1 (or 2 or 3).
Does that make sense?
Upvotes: 0
Views: 174
Reputation: 753475
This seems to meet your requirement if you want the 'Rowname' printed; if not, omit the print
in the first line of the script:
$ awk '/^Rowname/ { nr=NR; print; next }
> NR==nr+1||NR==nr+2 { next }
> NR==nr+3 { v1=$1; print; next }
> { if (abs(v1-$1) > 1) print; v1=$1; }
> function abs(x) { return (x < 0) ? -x : x }
> ' data
Rowname
1596 1787
1565 1803
$
Upvotes: 1
Reputation: 23374
This may do it
awk '#print line 4
NR == 4;
#print any line that meets the "sum" comparison condition
NR > 4 && ($1 + $2 - prev_sum != -1) && ($1 + $2 - prev_sum != 1);
#set the sum for this line into a variable
{prev_sum=$1 + $2}' file.txt
Upvotes: 0