Filtering out numbers that have sequential- consecutive or non-consecutive digits
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
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
text-processing
add a comment |Â
up vote
2
down vote
favorite
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
text-processing
11
You should show the script you are working on and state where the problem is. Otherwise you seem to be attempting to get someone to do your homework for you without making any effort yourself.
â jww
Sep 23 at 17:35
2
Possible duplicate of Filtering list of numbers containing sequential digits
â roaima
Sep 24 at 8:53
Close Voters, @roaima this is not duplicated of question Filtering list of numbers containing sequential digits.
â Ã±ÃÂsýù÷
Sep 24 at 10:02
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
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
text-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
text-processing
text-processing
edited Sep 23 at 22:20
Goro
6,28652763
6,28652763
asked Sep 22 at 18:37
Ahmed
474
474
11
You should show the script you are working on and state where the problem is. Otherwise you seem to be attempting to get someone to do your homework for you without making any effort yourself.
â jww
Sep 23 at 17:35
2
Possible duplicate of Filtering list of numbers containing sequential digits
â roaima
Sep 24 at 8:53
Close Voters, @roaima this is not duplicated of question Filtering list of numbers containing sequential digits.
â Ã±ÃÂsýù÷
Sep 24 at 10:02
add a comment |Â
11
You should show the script you are working on and state where the problem is. Otherwise you seem to be attempting to get someone to do your homework for you without making any effort yourself.
â jww
Sep 23 at 17:35
2
Possible duplicate of Filtering list of numbers containing sequential digits
â roaima
Sep 24 at 8:53
Close Voters, @roaima this is not duplicated of question Filtering list of numbers containing sequential digits.
â Ã±ÃÂsýù÷
Sep 24 at 10:02
11
11
You should show the script you are working on and state where the problem is. Otherwise you seem to be attempting to get someone to do your homework for you without making any effort yourself.
â jww
Sep 23 at 17:35
You should show the script you are working on and state where the problem is. Otherwise you seem to be attempting to get someone to do your homework for you without making any effort yourself.
â jww
Sep 23 at 17:35
2
2
Possible duplicate of Filtering list of numbers containing sequential digits
â roaima
Sep 24 at 8:53
Possible duplicate of Filtering list of numbers containing sequential digits
â roaima
Sep 24 at 8:53
Close Voters, @roaima this is not duplicated of question Filtering list of numbers containing sequential digits.
â Ã±ÃÂsýù÷
Sep 24 at 10:02
Close Voters, @roaima this is not duplicated of question Filtering list of numbers containing sequential digits.
â Ã±ÃÂsýù÷
Sep 24 at 10:02
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
6
down vote
With GNU awk
which supports empty string as field separator.
awk -F '' ' is_sequential=0;
for (i=2; i<=NF; i++) is_sequential+=($0 ~ $i-1 ;
!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).
add a comment |Â
up vote
2
down vote
You can try either
awk '
split ("", N) # delete array N
L = 1 # initialise boolean L to TRUE
for (i=1; i<=length($1); i++) # for each digit
P = substr($1, i, 1)
if (N[P-1]
L
' file
Output:
370
94
or
awk '
split ("", N)
L = 1
for (i=1; i<=length; i++)
N[substr($0,i,1)] = 1 # set all N elements for the digits in string
for (i=0; i<9; i++)
if (N[i] + N[i+1] == 2) # check for two adjacent elements to be TRUE
L = 0
break
L
' file
Output:
370
94
Tested on Ubuntu 18.04
add a comment |Â
up vote
0
down vote
Here, as the list of combinations is relatively small, you might as well consider them all in a ERE alternation:
grep -vE '0.*1|1.*[02]|2.*[13]|3.*[24]|4.*[35]|5.*[46]|6.*[57]|7.*[68]|8.*[79]|9.*8'
The same with perl
but using perl code in (??...)
inside the regexp to match the next or previous digit:
perl -ne 'print unless /([0-8]).*(??$1+1)/ || /([1-9]).*(??$1-1)/'
With sed, you could append the list of consecutive pairs to the pattern space, and use back references to find the matches:
sed -ne '1x;s/$/0123456789876543210/;x;' -e 'G;/(.).*(.).*n.*12/!P'
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
6
down vote
With GNU awk
which supports empty string as field separator.
awk -F '' ' is_sequential=0;
for (i=2; i<=NF; i++) is_sequential+=($0 ~ $i-1 ;
!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).
add a comment |Â
up vote
6
down vote
With GNU awk
which supports empty string as field separator.
awk -F '' ' is_sequential=0;
for (i=2; i<=NF; i++) is_sequential+=($0 ~ $i-1 ;
!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).
add a comment |Â
up vote
6
down vote
up vote
6
down vote
With GNU awk
which supports empty string as field separator.
awk -F '' ' is_sequential=0;
for (i=2; i<=NF; i++) is_sequential+=($0 ~ $i-1 ;
!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).
With GNU awk
which supports empty string as field separator.
awk -F '' ' is_sequential=0;
for (i=2; i<=NF; i++) is_sequential+=($0 ~ $i-1 ;
!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).
edited Sep 28 at 16:03
Isaac
7,58311037
7,58311037
answered Sep 23 at 17:03
ñÃÂsýù÷
16k92563
16k92563
add a comment |Â
add a comment |Â
up vote
2
down vote
You can try either
awk '
split ("", N) # delete array N
L = 1 # initialise boolean L to TRUE
for (i=1; i<=length($1); i++) # for each digit
P = substr($1, i, 1)
if (N[P-1]
L
' file
Output:
370
94
or
awk '
split ("", N)
L = 1
for (i=1; i<=length; i++)
N[substr($0,i,1)] = 1 # set all N elements for the digits in string
for (i=0; i<9; i++)
if (N[i] + N[i+1] == 2) # check for two adjacent elements to be TRUE
L = 0
break
L
' file
Output:
370
94
Tested on Ubuntu 18.04
add a comment |Â
up vote
2
down vote
You can try either
awk '
split ("", N) # delete array N
L = 1 # initialise boolean L to TRUE
for (i=1; i<=length($1); i++) # for each digit
P = substr($1, i, 1)
if (N[P-1]
L
' file
Output:
370
94
or
awk '
split ("", N)
L = 1
for (i=1; i<=length; i++)
N[substr($0,i,1)] = 1 # set all N elements for the digits in string
for (i=0; i<9; i++)
if (N[i] + N[i+1] == 2) # check for two adjacent elements to be TRUE
L = 0
break
L
' file
Output:
370
94
Tested on Ubuntu 18.04
add a comment |Â
up vote
2
down vote
up vote
2
down vote
You can try either
awk '
split ("", N) # delete array N
L = 1 # initialise boolean L to TRUE
for (i=1; i<=length($1); i++) # for each digit
P = substr($1, i, 1)
if (N[P-1]
L
' file
Output:
370
94
or
awk '
split ("", N)
L = 1
for (i=1; i<=length; i++)
N[substr($0,i,1)] = 1 # set all N elements for the digits in string
for (i=0; i<9; i++)
if (N[i] + N[i+1] == 2) # check for two adjacent elements to be TRUE
L = 0
break
L
' file
Output:
370
94
Tested on Ubuntu 18.04
You can try either
awk '
split ("", N) # delete array N
L = 1 # initialise boolean L to TRUE
for (i=1; i<=length($1); i++) # for each digit
P = substr($1, i, 1)
if (N[P-1]
L
' file
Output:
370
94
or
awk '
split ("", N)
L = 1
for (i=1; i<=length; i++)
N[substr($0,i,1)] = 1 # set all N elements for the digits in string
for (i=0; i<9; i++)
if (N[i] + N[i+1] == 2) # check for two adjacent elements to be TRUE
L = 0
break
L
' file
Output:
370
94
Tested on Ubuntu 18.04
edited Sep 25 at 23:06
Ketzak
1033
1033
answered Sep 23 at 17:32
RudiC
1,6969
1,6969
add a comment |Â
add a comment |Â
up vote
0
down vote
Here, as the list of combinations is relatively small, you might as well consider them all in a ERE alternation:
grep -vE '0.*1|1.*[02]|2.*[13]|3.*[24]|4.*[35]|5.*[46]|6.*[57]|7.*[68]|8.*[79]|9.*8'
The same with perl
but using perl code in (??...)
inside the regexp to match the next or previous digit:
perl -ne 'print unless /([0-8]).*(??$1+1)/ || /([1-9]).*(??$1-1)/'
With sed, you could append the list of consecutive pairs to the pattern space, and use back references to find the matches:
sed -ne '1x;s/$/0123456789876543210/;x;' -e 'G;/(.).*(.).*n.*12/!P'
add a comment |Â
up vote
0
down vote
Here, as the list of combinations is relatively small, you might as well consider them all in a ERE alternation:
grep -vE '0.*1|1.*[02]|2.*[13]|3.*[24]|4.*[35]|5.*[46]|6.*[57]|7.*[68]|8.*[79]|9.*8'
The same with perl
but using perl code in (??...)
inside the regexp to match the next or previous digit:
perl -ne 'print unless /([0-8]).*(??$1+1)/ || /([1-9]).*(??$1-1)/'
With sed, you could append the list of consecutive pairs to the pattern space, and use back references to find the matches:
sed -ne '1x;s/$/0123456789876543210/;x;' -e 'G;/(.).*(.).*n.*12/!P'
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Here, as the list of combinations is relatively small, you might as well consider them all in a ERE alternation:
grep -vE '0.*1|1.*[02]|2.*[13]|3.*[24]|4.*[35]|5.*[46]|6.*[57]|7.*[68]|8.*[79]|9.*8'
The same with perl
but using perl code in (??...)
inside the regexp to match the next or previous digit:
perl -ne 'print unless /([0-8]).*(??$1+1)/ || /([1-9]).*(??$1-1)/'
With sed, you could append the list of consecutive pairs to the pattern space, and use back references to find the matches:
sed -ne '1x;s/$/0123456789876543210/;x;' -e 'G;/(.).*(.).*n.*12/!P'
Here, as the list of combinations is relatively small, you might as well consider them all in a ERE alternation:
grep -vE '0.*1|1.*[02]|2.*[13]|3.*[24]|4.*[35]|5.*[46]|6.*[57]|7.*[68]|8.*[79]|9.*8'
The same with perl
but using perl code in (??...)
inside the regexp to match the next or previous digit:
perl -ne 'print unless /([0-8]).*(??$1+1)/ || /([1-9]).*(??$1-1)/'
With sed, you could append the list of consecutive pairs to the pattern space, and use back references to find the matches:
sed -ne '1x;s/$/0123456789876543210/;x;' -e 'G;/(.).*(.).*n.*12/!P'
answered Sep 30 at 8:19
Stéphane Chazelas
287k53529867
287k53529867
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f470741%2ffiltering-out-numbers-that-have-sequential-consecutive-or-non-consecutive-digit%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
11
You should show the script you are working on and state where the problem is. Otherwise you seem to be attempting to get someone to do your homework for you without making any effort yourself.
â jww
Sep 23 at 17:35
2
Possible duplicate of Filtering list of numbers containing sequential digits
â roaima
Sep 24 at 8:53
Close Voters, @roaima this is not duplicated of question Filtering list of numbers containing sequential digits.
â Ã±ÃÂsýù÷
Sep 24 at 10:02