Grep for a range of numbers in parenthesis
Clash Royale CLAN TAG#URR8PPP
up vote
4
down vote
favorite
I am trying to grep for only for a range of numbers located inside parenthesis. My current grep will pull everything within the parenthesis but I just want anything from [0001-9999]
. How can I fix my grep to do this?
I have tried a few different version of this (?<=()[0001-9999](?=))
but they don't return anything
$ cat /tmp/output
This is R3trans version 6.26 (release 745 - 24.03.17 - 20:17:03).
R3trans finished (0000).
R3trans finished (0001).
R3trans finished (9999).
05.03.2018 16:30:02
enserver, EnqueueServer, RED, Running, 2018 02 27 09:15:52, 151:14:10, 19069
Current output
$ cat /tmp/output | grep -Po '(?<=().*(?=))'
release 745 - 24.03.17 - 20:17:03
0000
0001
9999
Desired output
$ cat /tmp/output | grep -Po '(?<=().*(?=))'
0001
9999
grep regular-expression
add a comment |Â
up vote
4
down vote
favorite
I am trying to grep for only for a range of numbers located inside parenthesis. My current grep will pull everything within the parenthesis but I just want anything from [0001-9999]
. How can I fix my grep to do this?
I have tried a few different version of this (?<=()[0001-9999](?=))
but they don't return anything
$ cat /tmp/output
This is R3trans version 6.26 (release 745 - 24.03.17 - 20:17:03).
R3trans finished (0000).
R3trans finished (0001).
R3trans finished (9999).
05.03.2018 16:30:02
enserver, EnqueueServer, RED, Running, 2018 02 27 09:15:52, 151:14:10, 19069
Current output
$ cat /tmp/output | grep -Po '(?<=().*(?=))'
release 745 - 24.03.17 - 20:17:03
0000
0001
9999
Desired output
$ cat /tmp/output | grep -Po '(?<=().*(?=))'
0001
9999
grep regular-expression
You want only four digit numbers? Why is[0-9]4
not working? Or, if your RE implementation does not support curly-brace counters,[0-9][0-9][0-9][0-9]
?
â DopeGhoti
Mar 6 at 17:29
That is close but I only want 0001 to 9999$ cat /tmp/output | grep -Po '(?<=()[0-9]4(?=))' 0000 0001 9999
â SpruceTips
Mar 6 at 17:32
So take that and strip out0000
with an inverse search.
â DopeGhoti
Mar 6 at 17:37
add a comment |Â
up vote
4
down vote
favorite
up vote
4
down vote
favorite
I am trying to grep for only for a range of numbers located inside parenthesis. My current grep will pull everything within the parenthesis but I just want anything from [0001-9999]
. How can I fix my grep to do this?
I have tried a few different version of this (?<=()[0001-9999](?=))
but they don't return anything
$ cat /tmp/output
This is R3trans version 6.26 (release 745 - 24.03.17 - 20:17:03).
R3trans finished (0000).
R3trans finished (0001).
R3trans finished (9999).
05.03.2018 16:30:02
enserver, EnqueueServer, RED, Running, 2018 02 27 09:15:52, 151:14:10, 19069
Current output
$ cat /tmp/output | grep -Po '(?<=().*(?=))'
release 745 - 24.03.17 - 20:17:03
0000
0001
9999
Desired output
$ cat /tmp/output | grep -Po '(?<=().*(?=))'
0001
9999
grep regular-expression
I am trying to grep for only for a range of numbers located inside parenthesis. My current grep will pull everything within the parenthesis but I just want anything from [0001-9999]
. How can I fix my grep to do this?
I have tried a few different version of this (?<=()[0001-9999](?=))
but they don't return anything
$ cat /tmp/output
This is R3trans version 6.26 (release 745 - 24.03.17 - 20:17:03).
R3trans finished (0000).
R3trans finished (0001).
R3trans finished (9999).
05.03.2018 16:30:02
enserver, EnqueueServer, RED, Running, 2018 02 27 09:15:52, 151:14:10, 19069
Current output
$ cat /tmp/output | grep -Po '(?<=().*(?=))'
release 745 - 24.03.17 - 20:17:03
0000
0001
9999
Desired output
$ cat /tmp/output | grep -Po '(?<=().*(?=))'
0001
9999
grep regular-expression
edited Mar 6 at 17:59
ilkkachu
49.2k672136
49.2k672136
asked Mar 6 at 17:27
SpruceTips
170214
170214
You want only four digit numbers? Why is[0-9]4
not working? Or, if your RE implementation does not support curly-brace counters,[0-9][0-9][0-9][0-9]
?
â DopeGhoti
Mar 6 at 17:29
That is close but I only want 0001 to 9999$ cat /tmp/output | grep -Po '(?<=()[0-9]4(?=))' 0000 0001 9999
â SpruceTips
Mar 6 at 17:32
So take that and strip out0000
with an inverse search.
â DopeGhoti
Mar 6 at 17:37
add a comment |Â
You want only four digit numbers? Why is[0-9]4
not working? Or, if your RE implementation does not support curly-brace counters,[0-9][0-9][0-9][0-9]
?
â DopeGhoti
Mar 6 at 17:29
That is close but I only want 0001 to 9999$ cat /tmp/output | grep -Po '(?<=()[0-9]4(?=))' 0000 0001 9999
â SpruceTips
Mar 6 at 17:32
So take that and strip out0000
with an inverse search.
â DopeGhoti
Mar 6 at 17:37
You want only four digit numbers? Why is
[0-9]4
not working? Or, if your RE implementation does not support curly-brace counters, [0-9][0-9][0-9][0-9]
?â DopeGhoti
Mar 6 at 17:29
You want only four digit numbers? Why is
[0-9]4
not working? Or, if your RE implementation does not support curly-brace counters, [0-9][0-9][0-9][0-9]
?â DopeGhoti
Mar 6 at 17:29
That is close but I only want 0001 to 9999
$ cat /tmp/output | grep -Po '(?<=()[0-9]4(?=))' 0000 0001 9999
â SpruceTips
Mar 6 at 17:32
That is close but I only want 0001 to 9999
$ cat /tmp/output | grep -Po '(?<=()[0-9]4(?=))' 0000 0001 9999
â SpruceTips
Mar 6 at 17:32
So take that and strip out
0000
with an inverse search.â DopeGhoti
Mar 6 at 17:37
So take that and strip out
0000
with an inverse search.â DopeGhoti
Mar 6 at 17:37
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
8
down vote
accepted
Two approaches to reach the goal:
grep
approach (with Perl support):
grep -Po '(K(?!0000)([0-9]4)(?=))' /tmp/output
GNU awk
approach:
awk -v FPAT='\([0-9]4\)' '$1 n = substr($1,2,4); if (int(n) > 0) print n ' /tmp/output
The output:
0001
9999
add a comment |Â
up vote
3
down vote
Apparently you have numeric constrains. If this is the case, why not using a programming language? Example:
perl -nE '/((d4))/ and $1 > 0 and say $1'
1
(assuming there's only one occurrence per line)
â Stéphane Chazelas
Mar 6 at 17:44
add a comment |Â
up vote
3
down vote
This appears to work:
grep -Eo '[0-9]4' /path/to/file | grep -v '0000'
Less readable, but with only one grep
:
grep -Eo '[0-9]3[1-9]|[0-9]2[1-9][0-9]|[0-9][1-9][0-9]2|[1-9][0-9]3' /path/to/file
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
8
down vote
accepted
Two approaches to reach the goal:
grep
approach (with Perl support):
grep -Po '(K(?!0000)([0-9]4)(?=))' /tmp/output
GNU awk
approach:
awk -v FPAT='\([0-9]4\)' '$1 n = substr($1,2,4); if (int(n) > 0) print n ' /tmp/output
The output:
0001
9999
add a comment |Â
up vote
8
down vote
accepted
Two approaches to reach the goal:
grep
approach (with Perl support):
grep -Po '(K(?!0000)([0-9]4)(?=))' /tmp/output
GNU awk
approach:
awk -v FPAT='\([0-9]4\)' '$1 n = substr($1,2,4); if (int(n) > 0) print n ' /tmp/output
The output:
0001
9999
add a comment |Â
up vote
8
down vote
accepted
up vote
8
down vote
accepted
Two approaches to reach the goal:
grep
approach (with Perl support):
grep -Po '(K(?!0000)([0-9]4)(?=))' /tmp/output
GNU awk
approach:
awk -v FPAT='\([0-9]4\)' '$1 n = substr($1,2,4); if (int(n) > 0) print n ' /tmp/output
The output:
0001
9999
Two approaches to reach the goal:
grep
approach (with Perl support):
grep -Po '(K(?!0000)([0-9]4)(?=))' /tmp/output
GNU awk
approach:
awk -v FPAT='\([0-9]4\)' '$1 n = substr($1,2,4); if (int(n) > 0) print n ' /tmp/output
The output:
0001
9999
edited Mar 6 at 17:44
answered Mar 6 at 17:38
RomanPerekhrest
22.4k12144
22.4k12144
add a comment |Â
add a comment |Â
up vote
3
down vote
Apparently you have numeric constrains. If this is the case, why not using a programming language? Example:
perl -nE '/((d4))/ and $1 > 0 and say $1'
1
(assuming there's only one occurrence per line)
â Stéphane Chazelas
Mar 6 at 17:44
add a comment |Â
up vote
3
down vote
Apparently you have numeric constrains. If this is the case, why not using a programming language? Example:
perl -nE '/((d4))/ and $1 > 0 and say $1'
1
(assuming there's only one occurrence per line)
â Stéphane Chazelas
Mar 6 at 17:44
add a comment |Â
up vote
3
down vote
up vote
3
down vote
Apparently you have numeric constrains. If this is the case, why not using a programming language? Example:
perl -nE '/((d4))/ and $1 > 0 and say $1'
Apparently you have numeric constrains. If this is the case, why not using a programming language? Example:
perl -nE '/((d4))/ and $1 > 0 and say $1'
edited Mar 6 at 17:44
answered Mar 6 at 17:39
JJoao
6,7011826
6,7011826
1
(assuming there's only one occurrence per line)
â Stéphane Chazelas
Mar 6 at 17:44
add a comment |Â
1
(assuming there's only one occurrence per line)
â Stéphane Chazelas
Mar 6 at 17:44
1
1
(assuming there's only one occurrence per line)
â Stéphane Chazelas
Mar 6 at 17:44
(assuming there's only one occurrence per line)
â Stéphane Chazelas
Mar 6 at 17:44
add a comment |Â
up vote
3
down vote
This appears to work:
grep -Eo '[0-9]4' /path/to/file | grep -v '0000'
Less readable, but with only one grep
:
grep -Eo '[0-9]3[1-9]|[0-9]2[1-9][0-9]|[0-9][1-9][0-9]2|[1-9][0-9]3' /path/to/file
add a comment |Â
up vote
3
down vote
This appears to work:
grep -Eo '[0-9]4' /path/to/file | grep -v '0000'
Less readable, but with only one grep
:
grep -Eo '[0-9]3[1-9]|[0-9]2[1-9][0-9]|[0-9][1-9][0-9]2|[1-9][0-9]3' /path/to/file
add a comment |Â
up vote
3
down vote
up vote
3
down vote
This appears to work:
grep -Eo '[0-9]4' /path/to/file | grep -v '0000'
Less readable, but with only one grep
:
grep -Eo '[0-9]3[1-9]|[0-9]2[1-9][0-9]|[0-9][1-9][0-9]2|[1-9][0-9]3' /path/to/file
This appears to work:
grep -Eo '[0-9]4' /path/to/file | grep -v '0000'
Less readable, but with only one grep
:
grep -Eo '[0-9]3[1-9]|[0-9]2[1-9][0-9]|[0-9][1-9][0-9]2|[1-9][0-9]3' /path/to/file
edited Mar 7 at 16:00
answered Mar 6 at 17:30
DopeGhoti
40.2k54779
40.2k54779
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%2f428570%2fgrep-for-a-range-of-numbers-in-parenthesis%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
You want only four digit numbers? Why is
[0-9]4
not working? Or, if your RE implementation does not support curly-brace counters,[0-9][0-9][0-9][0-9]
?â DopeGhoti
Mar 6 at 17:29
That is close but I only want 0001 to 9999
$ cat /tmp/output | grep -Po '(?<=()[0-9]4(?=))' 0000 0001 9999
â SpruceTips
Mar 6 at 17:32
So take that and strip out
0000
with an inverse search.â DopeGhoti
Mar 6 at 17:37