Filtering out numbers that have sequential- consecutive or non-consecutive digits

awkgrepperlsedtext processing

I have a file filled with numbers, number per line. Each number consists of two or three digits.

I would like to filter out this file by any number has two or more sequential digits. These sequential digits can be consecutive (e.g. 127, 215, 781), or non-consecutive (e.g. 506). The order of the sequential digits is not important. It can be small to large (e.g. 127) or large to small (e.g. 215).

For example:

127
215
781
874
370
01
10
142
506
94

The expected output:

370
94

Because:

127 # Has two sequential and consecutive digits (1 and 2)
215 # Has two sequential and consecutive digits (1 and 2)
781 # Has two sequential and consecutive digits (7 and 8)
874 # Has two sequential and consecutive digits (7 and 8)
370 # Keep
01  # Has two sequential and consecutive digits (0 and 1)
10  # Has two sequential and consecutive digits (0 and 1)
142 # Has two sequential and non-consecutive digits (1 and 2)
506 # Has two sequential and non-consecutive digits (5 and 6)
94  # Keep

Best Answer

With awk and setting FS to empty string (the effect of using empty FS is undefined behavior per POSIX and depending on what version awk you are using it's can be result differently). Below is tested in GNU awk:

awk -F '' '{ is_sequential=0;
    for (i=2; i<=NF; i++) { is_sequential+=($0 ~ $i-1 || $0 ~ $i+1)?1:0 }; }
    !is_sequential{ print }' infile

we are checking on each number $i for a number that it's equal with number-1 $i-1 or number+1 $i+1 against the whole line, meaning that if there was a number number-1 or number+1 or both seen in a line, so we found there are at least two numbers are next to each other (the first, the number $i itself and next one either $i-1 or $i+1 or both (sequential) and with Ternary condition it will increment the value of is_sequential variable otherwise always it will be 0.

In next block is_sequential{ print }, we print that line where the value is unchanged (the value is still 0, no at least two numbers seen that were sequential).

Related Question