Grep only numbers, not the alphanumeric entries
up vote
0
down vote
favorite
I have a list of values like:
1
2
3
4
Ak123
Ak23
Ak147
1Apple
2Apricot
3Mango
4Orange
I just want to execute a simple grep command to list me only the numbers. i.e. 1
, 2
, 3
and 4
.
I tried this command -
grep -Ein --color '^s*[0-9]' test.txt
but it returns the alphanumeric also. Instead I tried omitting the character by this command:
grep -Ein --color '^s*[0-9][^A-Z]' test.txt
but it gives 0 results.
grep regular-expression numeric-data
add a comment |
up vote
0
down vote
favorite
I have a list of values like:
1
2
3
4
Ak123
Ak23
Ak147
1Apple
2Apricot
3Mango
4Orange
I just want to execute a simple grep command to list me only the numbers. i.e. 1
, 2
, 3
and 4
.
I tried this command -
grep -Ein --color '^s*[0-9]' test.txt
but it returns the alphanumeric also. Instead I tried omitting the character by this command:
grep -Ein --color '^s*[0-9][^A-Z]' test.txt
but it gives 0 results.
grep regular-expression numeric-data
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a list of values like:
1
2
3
4
Ak123
Ak23
Ak147
1Apple
2Apricot
3Mango
4Orange
I just want to execute a simple grep command to list me only the numbers. i.e. 1
, 2
, 3
and 4
.
I tried this command -
grep -Ein --color '^s*[0-9]' test.txt
but it returns the alphanumeric also. Instead I tried omitting the character by this command:
grep -Ein --color '^s*[0-9][^A-Z]' test.txt
but it gives 0 results.
grep regular-expression numeric-data
I have a list of values like:
1
2
3
4
Ak123
Ak23
Ak147
1Apple
2Apricot
3Mango
4Orange
I just want to execute a simple grep command to list me only the numbers. i.e. 1
, 2
, 3
and 4
.
I tried this command -
grep -Ein --color '^s*[0-9]' test.txt
but it returns the alphanumeric also. Instead I tried omitting the character by this command:
grep -Ein --color '^s*[0-9][^A-Z]' test.txt
but it gives 0 results.
grep regular-expression numeric-data
grep regular-expression numeric-data
edited Nov 29 at 18:36
Kusalananda
119k16223364
119k16223364
asked Nov 29 at 18:01
Srinivasan Senthil
1
1
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
5
down vote
Assuming each line only contains a single word with no flanking whitespace:
grep -x -E '[0-9]+'
or
grep -x -E '[[:digit:]]+'
This would extract any line that contained only digits. The -x
option to grep
forces the pattern to match across a complete line. I'm using -E
to enable extended regular expression to be able to use +
.
The pattern [[:digit:]]+
would match at least one digit. The type of digit that it matches may depend on your locale.
To allow for whitespace before and after:
grep -x -E '[[:blank:]]*[0-9]+[[:blank:]]*'
or
grep -x -E '[[:blank:]]*[[:digit:]]+[[:blank:]]*'
The pattern [[:blank:]]*
matches zero or more tabs or spaces.
Your expression ^s*[0-9]
matches lines that may start with a space character (assuming s
matches a space character, it's really a PCRE) and then has a digit. After the digit, any character may occur, as in the string 2Apricot
.
Your expression ^s*[0-9][^A-Z]
is similar, but [^A-Z]
forces the matching of a second non-alphabetic non-uppercase character. This matches things like 33
, 1a
, and 5-
and longer strings, but would not match the single character string 3
.
Awesome... Thank you so much. All of them worked. I just can't understand what the -X option really does?
– Srinivasan Senthil
Nov 29 at 18:28
@SrinivasanSenthil You mean-x
(lowercase). As I wrote, it forces the match to span the whole line. It's like including^
at the start and$
at the end of the pattern. It makes it easier to read the expressions when you're matching a whole line.
– Kusalananda
Nov 29 at 18:34
add a comment |
up vote
1
down vote
Try also
$ grep -v '[^ 0-9]' file
1
2
3
4
I like this approach. Instead of "show me what I want", it's "discard what I don't want" -- It just depends what's easier to quantify, "what I want" or "what I don't want".
– glenn jackman
Nov 30 at 16:31
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
Assuming each line only contains a single word with no flanking whitespace:
grep -x -E '[0-9]+'
or
grep -x -E '[[:digit:]]+'
This would extract any line that contained only digits. The -x
option to grep
forces the pattern to match across a complete line. I'm using -E
to enable extended regular expression to be able to use +
.
The pattern [[:digit:]]+
would match at least one digit. The type of digit that it matches may depend on your locale.
To allow for whitespace before and after:
grep -x -E '[[:blank:]]*[0-9]+[[:blank:]]*'
or
grep -x -E '[[:blank:]]*[[:digit:]]+[[:blank:]]*'
The pattern [[:blank:]]*
matches zero or more tabs or spaces.
Your expression ^s*[0-9]
matches lines that may start with a space character (assuming s
matches a space character, it's really a PCRE) and then has a digit. After the digit, any character may occur, as in the string 2Apricot
.
Your expression ^s*[0-9][^A-Z]
is similar, but [^A-Z]
forces the matching of a second non-alphabetic non-uppercase character. This matches things like 33
, 1a
, and 5-
and longer strings, but would not match the single character string 3
.
Awesome... Thank you so much. All of them worked. I just can't understand what the -X option really does?
– Srinivasan Senthil
Nov 29 at 18:28
@SrinivasanSenthil You mean-x
(lowercase). As I wrote, it forces the match to span the whole line. It's like including^
at the start and$
at the end of the pattern. It makes it easier to read the expressions when you're matching a whole line.
– Kusalananda
Nov 29 at 18:34
add a comment |
up vote
5
down vote
Assuming each line only contains a single word with no flanking whitespace:
grep -x -E '[0-9]+'
or
grep -x -E '[[:digit:]]+'
This would extract any line that contained only digits. The -x
option to grep
forces the pattern to match across a complete line. I'm using -E
to enable extended regular expression to be able to use +
.
The pattern [[:digit:]]+
would match at least one digit. The type of digit that it matches may depend on your locale.
To allow for whitespace before and after:
grep -x -E '[[:blank:]]*[0-9]+[[:blank:]]*'
or
grep -x -E '[[:blank:]]*[[:digit:]]+[[:blank:]]*'
The pattern [[:blank:]]*
matches zero or more tabs or spaces.
Your expression ^s*[0-9]
matches lines that may start with a space character (assuming s
matches a space character, it's really a PCRE) and then has a digit. After the digit, any character may occur, as in the string 2Apricot
.
Your expression ^s*[0-9][^A-Z]
is similar, but [^A-Z]
forces the matching of a second non-alphabetic non-uppercase character. This matches things like 33
, 1a
, and 5-
and longer strings, but would not match the single character string 3
.
Awesome... Thank you so much. All of them worked. I just can't understand what the -X option really does?
– Srinivasan Senthil
Nov 29 at 18:28
@SrinivasanSenthil You mean-x
(lowercase). As I wrote, it forces the match to span the whole line. It's like including^
at the start and$
at the end of the pattern. It makes it easier to read the expressions when you're matching a whole line.
– Kusalananda
Nov 29 at 18:34
add a comment |
up vote
5
down vote
up vote
5
down vote
Assuming each line only contains a single word with no flanking whitespace:
grep -x -E '[0-9]+'
or
grep -x -E '[[:digit:]]+'
This would extract any line that contained only digits. The -x
option to grep
forces the pattern to match across a complete line. I'm using -E
to enable extended regular expression to be able to use +
.
The pattern [[:digit:]]+
would match at least one digit. The type of digit that it matches may depend on your locale.
To allow for whitespace before and after:
grep -x -E '[[:blank:]]*[0-9]+[[:blank:]]*'
or
grep -x -E '[[:blank:]]*[[:digit:]]+[[:blank:]]*'
The pattern [[:blank:]]*
matches zero or more tabs or spaces.
Your expression ^s*[0-9]
matches lines that may start with a space character (assuming s
matches a space character, it's really a PCRE) and then has a digit. After the digit, any character may occur, as in the string 2Apricot
.
Your expression ^s*[0-9][^A-Z]
is similar, but [^A-Z]
forces the matching of a second non-alphabetic non-uppercase character. This matches things like 33
, 1a
, and 5-
and longer strings, but would not match the single character string 3
.
Assuming each line only contains a single word with no flanking whitespace:
grep -x -E '[0-9]+'
or
grep -x -E '[[:digit:]]+'
This would extract any line that contained only digits. The -x
option to grep
forces the pattern to match across a complete line. I'm using -E
to enable extended regular expression to be able to use +
.
The pattern [[:digit:]]+
would match at least one digit. The type of digit that it matches may depend on your locale.
To allow for whitespace before and after:
grep -x -E '[[:blank:]]*[0-9]+[[:blank:]]*'
or
grep -x -E '[[:blank:]]*[[:digit:]]+[[:blank:]]*'
The pattern [[:blank:]]*
matches zero or more tabs or spaces.
Your expression ^s*[0-9]
matches lines that may start with a space character (assuming s
matches a space character, it's really a PCRE) and then has a digit. After the digit, any character may occur, as in the string 2Apricot
.
Your expression ^s*[0-9][^A-Z]
is similar, but [^A-Z]
forces the matching of a second non-alphabetic non-uppercase character. This matches things like 33
, 1a
, and 5-
and longer strings, but would not match the single character string 3
.
edited Nov 29 at 18:26
answered Nov 29 at 18:07
Kusalananda
119k16223364
119k16223364
Awesome... Thank you so much. All of them worked. I just can't understand what the -X option really does?
– Srinivasan Senthil
Nov 29 at 18:28
@SrinivasanSenthil You mean-x
(lowercase). As I wrote, it forces the match to span the whole line. It's like including^
at the start and$
at the end of the pattern. It makes it easier to read the expressions when you're matching a whole line.
– Kusalananda
Nov 29 at 18:34
add a comment |
Awesome... Thank you so much. All of them worked. I just can't understand what the -X option really does?
– Srinivasan Senthil
Nov 29 at 18:28
@SrinivasanSenthil You mean-x
(lowercase). As I wrote, it forces the match to span the whole line. It's like including^
at the start and$
at the end of the pattern. It makes it easier to read the expressions when you're matching a whole line.
– Kusalananda
Nov 29 at 18:34
Awesome... Thank you so much. All of them worked. I just can't understand what the -X option really does?
– Srinivasan Senthil
Nov 29 at 18:28
Awesome... Thank you so much. All of them worked. I just can't understand what the -X option really does?
– Srinivasan Senthil
Nov 29 at 18:28
@SrinivasanSenthil You mean
-x
(lowercase). As I wrote, it forces the match to span the whole line. It's like including ^
at the start and $
at the end of the pattern. It makes it easier to read the expressions when you're matching a whole line.– Kusalananda
Nov 29 at 18:34
@SrinivasanSenthil You mean
-x
(lowercase). As I wrote, it forces the match to span the whole line. It's like including ^
at the start and $
at the end of the pattern. It makes it easier to read the expressions when you're matching a whole line.– Kusalananda
Nov 29 at 18:34
add a comment |
up vote
1
down vote
Try also
$ grep -v '[^ 0-9]' file
1
2
3
4
I like this approach. Instead of "show me what I want", it's "discard what I don't want" -- It just depends what's easier to quantify, "what I want" or "what I don't want".
– glenn jackman
Nov 30 at 16:31
add a comment |
up vote
1
down vote
Try also
$ grep -v '[^ 0-9]' file
1
2
3
4
I like this approach. Instead of "show me what I want", it's "discard what I don't want" -- It just depends what's easier to quantify, "what I want" or "what I don't want".
– glenn jackman
Nov 30 at 16:31
add a comment |
up vote
1
down vote
up vote
1
down vote
Try also
$ grep -v '[^ 0-9]' file
1
2
3
4
Try also
$ grep -v '[^ 0-9]' file
1
2
3
4
answered Nov 29 at 21:48
RudiC
3,7171312
3,7171312
I like this approach. Instead of "show me what I want", it's "discard what I don't want" -- It just depends what's easier to quantify, "what I want" or "what I don't want".
– glenn jackman
Nov 30 at 16:31
add a comment |
I like this approach. Instead of "show me what I want", it's "discard what I don't want" -- It just depends what's easier to quantify, "what I want" or "what I don't want".
– glenn jackman
Nov 30 at 16:31
I like this approach. Instead of "show me what I want", it's "discard what I don't want" -- It just depends what's easier to quantify, "what I want" or "what I don't want".
– glenn jackman
Nov 30 at 16:31
I like this approach. Instead of "show me what I want", it's "discard what I don't want" -- It just depends what's easier to quantify, "what I want" or "what I don't want".
– glenn jackman
Nov 30 at 16:31
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f484991%2fgrep-only-numbers-not-the-alphanumeric-entries%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown