How to grep any digit from sentence
Clash Royale CLAN TAG#URR8PPP
up vote
-4
down vote
favorite
Could You help me with greping?
I have a:
variable="RMN quota: 0 bytes"
and
variable="RMN quota: 1.56 bytes"
The target id to get output: 0 or 1.56.
What will be grep for it?
bash text-processing grep
add a comment |Â
up vote
-4
down vote
favorite
Could You help me with greping?
I have a:
variable="RMN quota: 0 bytes"
and
variable="RMN quota: 1.56 bytes"
The target id to get output: 0 or 1.56.
What will be grep for it?
bash text-processing grep
it must be read from a variable.
â Andrzej TI
Nov 27 '17 at 19:28
add a comment |Â
up vote
-4
down vote
favorite
up vote
-4
down vote
favorite
Could You help me with greping?
I have a:
variable="RMN quota: 0 bytes"
and
variable="RMN quota: 1.56 bytes"
The target id to get output: 0 or 1.56.
What will be grep for it?
bash text-processing grep
Could You help me with greping?
I have a:
variable="RMN quota: 0 bytes"
and
variable="RMN quota: 1.56 bytes"
The target id to get output: 0 or 1.56.
What will be grep for it?
bash text-processing grep
edited Nov 27 '17 at 19:37
Stéphane Chazelas
282k53521854
282k53521854
asked Nov 27 '17 at 19:20
Andrzej TI
11
11
it must be read from a variable.
â Andrzej TI
Nov 27 '17 at 19:28
add a comment |Â
it must be read from a variable.
â Andrzej TI
Nov 27 '17 at 19:28
it must be read from a variable.
â Andrzej TI
Nov 27 '17 at 19:28
it must be read from a variable.
â Andrzej TI
Nov 27 '17 at 19:28
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
1
down vote
accepted
This seems to work:
grep -Eo '[0-9]+(.[0-9]+)?' inputfile
If you're checking the value of a shell variable rather than the contents of a file, you can do this:
echo "$variable" | grep -Eo '[0-9]+(.[0-9]+)?'
it must be read from a variable.
â Andrzej TI
Nov 27 '17 at 19:29
It Rules :) Than You so much :)))
â Andrzej TI
Nov 27 '17 at 19:31
add a comment |Â
up vote
5
down vote
POSIXly:
n=$variable% bytes # strip the trailing " bytes"
n=$n##*[[:blank:]] # strip the leading part up to the rightmost blank
add a comment |Â
up vote
0
down vote
Since you have bash:
tr -d -c 0-9. <<<$variable
(would also work in Zsh).
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
This seems to work:
grep -Eo '[0-9]+(.[0-9]+)?' inputfile
If you're checking the value of a shell variable rather than the contents of a file, you can do this:
echo "$variable" | grep -Eo '[0-9]+(.[0-9]+)?'
it must be read from a variable.
â Andrzej TI
Nov 27 '17 at 19:29
It Rules :) Than You so much :)))
â Andrzej TI
Nov 27 '17 at 19:31
add a comment |Â
up vote
1
down vote
accepted
This seems to work:
grep -Eo '[0-9]+(.[0-9]+)?' inputfile
If you're checking the value of a shell variable rather than the contents of a file, you can do this:
echo "$variable" | grep -Eo '[0-9]+(.[0-9]+)?'
it must be read from a variable.
â Andrzej TI
Nov 27 '17 at 19:29
It Rules :) Than You so much :)))
â Andrzej TI
Nov 27 '17 at 19:31
add a comment |Â
up vote
1
down vote
accepted
up vote
1
down vote
accepted
This seems to work:
grep -Eo '[0-9]+(.[0-9]+)?' inputfile
If you're checking the value of a shell variable rather than the contents of a file, you can do this:
echo "$variable" | grep -Eo '[0-9]+(.[0-9]+)?'
This seems to work:
grep -Eo '[0-9]+(.[0-9]+)?' inputfile
If you're checking the value of a shell variable rather than the contents of a file, you can do this:
echo "$variable" | grep -Eo '[0-9]+(.[0-9]+)?'
edited Nov 27 '17 at 19:30
answered Nov 27 '17 at 19:25
DopeGhoti
40.6k54979
40.6k54979
it must be read from a variable.
â Andrzej TI
Nov 27 '17 at 19:29
It Rules :) Than You so much :)))
â Andrzej TI
Nov 27 '17 at 19:31
add a comment |Â
it must be read from a variable.
â Andrzej TI
Nov 27 '17 at 19:29
It Rules :) Than You so much :)))
â Andrzej TI
Nov 27 '17 at 19:31
it must be read from a variable.
â Andrzej TI
Nov 27 '17 at 19:29
it must be read from a variable.
â Andrzej TI
Nov 27 '17 at 19:29
It Rules :) Than You so much :)))
â Andrzej TI
Nov 27 '17 at 19:31
It Rules :) Than You so much :)))
â Andrzej TI
Nov 27 '17 at 19:31
add a comment |Â
up vote
5
down vote
POSIXly:
n=$variable% bytes # strip the trailing " bytes"
n=$n##*[[:blank:]] # strip the leading part up to the rightmost blank
add a comment |Â
up vote
5
down vote
POSIXly:
n=$variable% bytes # strip the trailing " bytes"
n=$n##*[[:blank:]] # strip the leading part up to the rightmost blank
add a comment |Â
up vote
5
down vote
up vote
5
down vote
POSIXly:
n=$variable% bytes # strip the trailing " bytes"
n=$n##*[[:blank:]] # strip the leading part up to the rightmost blank
POSIXly:
n=$variable% bytes # strip the trailing " bytes"
n=$n##*[[:blank:]] # strip the leading part up to the rightmost blank
answered Nov 27 '17 at 19:40
Stéphane Chazelas
282k53521854
282k53521854
add a comment |Â
add a comment |Â
up vote
0
down vote
Since you have bash:
tr -d -c 0-9. <<<$variable
(would also work in Zsh).
add a comment |Â
up vote
0
down vote
Since you have bash:
tr -d -c 0-9. <<<$variable
(would also work in Zsh).
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Since you have bash:
tr -d -c 0-9. <<<$variable
(would also work in Zsh).
Since you have bash:
tr -d -c 0-9. <<<$variable
(would also work in Zsh).
answered Nov 28 '17 at 6:12
user1934428
36119
36119
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%2f407360%2fhow-to-grep-any-digit-from-sentence%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
it must be read from a variable.
â Andrzej TI
Nov 27 '17 at 19:28