How can I output the number on the nth position in a string?
Using bash, lets say i have the following string
string="Same bought 5 bananas, 12 apples, 2 peaches and 16 oranges"
How can I trim everything except the nth number. In this case I want to output 12 which is the second number in the string.
How can I do that with bash, grep or sed?
bash sed grep text-manipulation
|
show 3 more comments
Using bash, lets say i have the following string
string="Same bought 5 bananas, 12 apples, 2 peaches and 16 oranges"
How can I trim everything except the nth number. In this case I want to output 12 which is the second number in the string.
How can I do that with bash, grep or sed?
bash sed grep text-manipulation
If X is unknown, how do you establish that it's X?
– tripleee
Dec 12 '18 at 11:32
@tripleee im representing the unknown with X.. X is actually an interger
– Bret Joseph
Dec 12 '18 at 11:34
What I am trying to say is that your question is unclear. What would an acceptable answer look like if we don't know what X is, or how we can find out?
– tripleee
Dec 12 '18 at 11:39
Obviously if you know that X is 1, the answer is trivial;sed 's/.*1.*/1/
but then what do you need the variable for? The result after the substitution is clearly identical to the input string, so there is no point in performing any substitution. Or are you trying to find out if the string contains 1?case $string in *1*) echo true;; esac
– tripleee
Dec 12 '18 at 11:40
the problem is I don't want to use the character1
since X it not always 1
– Bret Joseph
Dec 12 '18 at 11:44
|
show 3 more comments
Using bash, lets say i have the following string
string="Same bought 5 bananas, 12 apples, 2 peaches and 16 oranges"
How can I trim everything except the nth number. In this case I want to output 12 which is the second number in the string.
How can I do that with bash, grep or sed?
bash sed grep text-manipulation
Using bash, lets say i have the following string
string="Same bought 5 bananas, 12 apples, 2 peaches and 16 oranges"
How can I trim everything except the nth number. In this case I want to output 12 which is the second number in the string.
How can I do that with bash, grep or sed?
bash sed grep text-manipulation
bash sed grep text-manipulation
edited Jan 16 at 12:12
Bret Joseph
asked Dec 12 '18 at 11:28


Bret JosephBret Joseph
8410
8410
If X is unknown, how do you establish that it's X?
– tripleee
Dec 12 '18 at 11:32
@tripleee im representing the unknown with X.. X is actually an interger
– Bret Joseph
Dec 12 '18 at 11:34
What I am trying to say is that your question is unclear. What would an acceptable answer look like if we don't know what X is, or how we can find out?
– tripleee
Dec 12 '18 at 11:39
Obviously if you know that X is 1, the answer is trivial;sed 's/.*1.*/1/
but then what do you need the variable for? The result after the substitution is clearly identical to the input string, so there is no point in performing any substitution. Or are you trying to find out if the string contains 1?case $string in *1*) echo true;; esac
– tripleee
Dec 12 '18 at 11:40
the problem is I don't want to use the character1
since X it not always 1
– Bret Joseph
Dec 12 '18 at 11:44
|
show 3 more comments
If X is unknown, how do you establish that it's X?
– tripleee
Dec 12 '18 at 11:32
@tripleee im representing the unknown with X.. X is actually an interger
– Bret Joseph
Dec 12 '18 at 11:34
What I am trying to say is that your question is unclear. What would an acceptable answer look like if we don't know what X is, or how we can find out?
– tripleee
Dec 12 '18 at 11:39
Obviously if you know that X is 1, the answer is trivial;sed 's/.*1.*/1/
but then what do you need the variable for? The result after the substitution is clearly identical to the input string, so there is no point in performing any substitution. Or are you trying to find out if the string contains 1?case $string in *1*) echo true;; esac
– tripleee
Dec 12 '18 at 11:40
the problem is I don't want to use the character1
since X it not always 1
– Bret Joseph
Dec 12 '18 at 11:44
If X is unknown, how do you establish that it's X?
– tripleee
Dec 12 '18 at 11:32
If X is unknown, how do you establish that it's X?
– tripleee
Dec 12 '18 at 11:32
@tripleee im representing the unknown with X.. X is actually an interger
– Bret Joseph
Dec 12 '18 at 11:34
@tripleee im representing the unknown with X.. X is actually an interger
– Bret Joseph
Dec 12 '18 at 11:34
What I am trying to say is that your question is unclear. What would an acceptable answer look like if we don't know what X is, or how we can find out?
– tripleee
Dec 12 '18 at 11:39
What I am trying to say is that your question is unclear. What would an acceptable answer look like if we don't know what X is, or how we can find out?
– tripleee
Dec 12 '18 at 11:39
Obviously if you know that X is 1, the answer is trivial;
sed 's/.*1.*/1/
but then what do you need the variable for? The result after the substitution is clearly identical to the input string, so there is no point in performing any substitution. Or are you trying to find out if the string contains 1? case $string in *1*) echo true;; esac
– tripleee
Dec 12 '18 at 11:40
Obviously if you know that X is 1, the answer is trivial;
sed 's/.*1.*/1/
but then what do you need the variable for? The result after the substitution is clearly identical to the input string, so there is no point in performing any substitution. Or are you trying to find out if the string contains 1? case $string in *1*) echo true;; esac
– tripleee
Dec 12 '18 at 11:40
the problem is I don't want to use the character
1
since X it not always 1– Bret Joseph
Dec 12 '18 at 11:44
the problem is I don't want to use the character
1
since X it not always 1– Bret Joseph
Dec 12 '18 at 11:44
|
show 3 more comments
2 Answers
2
active
oldest
votes
This might work for you (GNU sed):
sed 's/^([^0-9]*([0-9]*)){2}.*/2/;/^$/d' file
This replaces the current line by the second occurrence of a group of numbers. The line is deleted unless a number is output.
thanks for the effort, what do you think of my own solution as well
– Bret Joseph
Jan 1 at 5:16
add a comment |
Solution Using sed
According to Paul Hodges and Triplee
grep -Eo '[0-9]+' <<<"$string"| sed 'nq;d'
Where n is the position of the number
sed 'NUMq;d'
NUM
is the lines to print.2q
says quit on the second line.d
will delete every other line except the last
3
'2q'
says quit on the second line, but won't stop the line from being printed.'d'
says delete every line you come to, which does prevent those lines from being printed. It checks them in order though, so'2q;d'
will quit (and flush-print) on line 2 before it hits the delete, but not on line one, so thed
prevents printing of line 1 but not line 2.
– Paul Hodges
Dec 12 '18 at 15:07
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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%2fstackoverflow.com%2fquestions%2f53742072%2fhow-can-i-output-the-number-on-the-nth-position-in-a-string%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
This might work for you (GNU sed):
sed 's/^([^0-9]*([0-9]*)){2}.*/2/;/^$/d' file
This replaces the current line by the second occurrence of a group of numbers. The line is deleted unless a number is output.
thanks for the effort, what do you think of my own solution as well
– Bret Joseph
Jan 1 at 5:16
add a comment |
This might work for you (GNU sed):
sed 's/^([^0-9]*([0-9]*)){2}.*/2/;/^$/d' file
This replaces the current line by the second occurrence of a group of numbers. The line is deleted unless a number is output.
thanks for the effort, what do you think of my own solution as well
– Bret Joseph
Jan 1 at 5:16
add a comment |
This might work for you (GNU sed):
sed 's/^([^0-9]*([0-9]*)){2}.*/2/;/^$/d' file
This replaces the current line by the second occurrence of a group of numbers. The line is deleted unless a number is output.
This might work for you (GNU sed):
sed 's/^([^0-9]*([0-9]*)){2}.*/2/;/^$/d' file
This replaces the current line by the second occurrence of a group of numbers. The line is deleted unless a number is output.
answered Dec 12 '18 at 14:33
potongpotong
36k43062
36k43062
thanks for the effort, what do you think of my own solution as well
– Bret Joseph
Jan 1 at 5:16
add a comment |
thanks for the effort, what do you think of my own solution as well
– Bret Joseph
Jan 1 at 5:16
thanks for the effort, what do you think of my own solution as well
– Bret Joseph
Jan 1 at 5:16
thanks for the effort, what do you think of my own solution as well
– Bret Joseph
Jan 1 at 5:16
add a comment |
Solution Using sed
According to Paul Hodges and Triplee
grep -Eo '[0-9]+' <<<"$string"| sed 'nq;d'
Where n is the position of the number
sed 'NUMq;d'
NUM
is the lines to print.2q
says quit on the second line.d
will delete every other line except the last
3
'2q'
says quit on the second line, but won't stop the line from being printed.'d'
says delete every line you come to, which does prevent those lines from being printed. It checks them in order though, so'2q;d'
will quit (and flush-print) on line 2 before it hits the delete, but not on line one, so thed
prevents printing of line 1 but not line 2.
– Paul Hodges
Dec 12 '18 at 15:07
add a comment |
Solution Using sed
According to Paul Hodges and Triplee
grep -Eo '[0-9]+' <<<"$string"| sed 'nq;d'
Where n is the position of the number
sed 'NUMq;d'
NUM
is the lines to print.2q
says quit on the second line.d
will delete every other line except the last
3
'2q'
says quit on the second line, but won't stop the line from being printed.'d'
says delete every line you come to, which does prevent those lines from being printed. It checks them in order though, so'2q;d'
will quit (and flush-print) on line 2 before it hits the delete, but not on line one, so thed
prevents printing of line 1 but not line 2.
– Paul Hodges
Dec 12 '18 at 15:07
add a comment |
Solution Using sed
According to Paul Hodges and Triplee
grep -Eo '[0-9]+' <<<"$string"| sed 'nq;d'
Where n is the position of the number
sed 'NUMq;d'
NUM
is the lines to print.2q
says quit on the second line.d
will delete every other line except the last
Solution Using sed
According to Paul Hodges and Triplee
grep -Eo '[0-9]+' <<<"$string"| sed 'nq;d'
Where n is the position of the number
sed 'NUMq;d'
NUM
is the lines to print.2q
says quit on the second line.d
will delete every other line except the last
edited Jan 2 at 17:14
answered Dec 12 '18 at 12:25


Bret JosephBret Joseph
8410
8410
3
'2q'
says quit on the second line, but won't stop the line from being printed.'d'
says delete every line you come to, which does prevent those lines from being printed. It checks them in order though, so'2q;d'
will quit (and flush-print) on line 2 before it hits the delete, but not on line one, so thed
prevents printing of line 1 but not line 2.
– Paul Hodges
Dec 12 '18 at 15:07
add a comment |
3
'2q'
says quit on the second line, but won't stop the line from being printed.'d'
says delete every line you come to, which does prevent those lines from being printed. It checks them in order though, so'2q;d'
will quit (and flush-print) on line 2 before it hits the delete, but not on line one, so thed
prevents printing of line 1 but not line 2.
– Paul Hodges
Dec 12 '18 at 15:07
3
3
'2q'
says quit on the second line, but won't stop the line from being printed. 'd'
says delete every line you come to, which does prevent those lines from being printed. It checks them in order though, so '2q;d'
will quit (and flush-print) on line 2 before it hits the delete, but not on line one, so the d
prevents printing of line 1 but not line 2.– Paul Hodges
Dec 12 '18 at 15:07
'2q'
says quit on the second line, but won't stop the line from being printed. 'd'
says delete every line you come to, which does prevent those lines from being printed. It checks them in order though, so '2q;d'
will quit (and flush-print) on line 2 before it hits the delete, but not on line one, so the d
prevents printing of line 1 but not line 2.– Paul Hodges
Dec 12 '18 at 15:07
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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%2fstackoverflow.com%2fquestions%2f53742072%2fhow-can-i-output-the-number-on-the-nth-position-in-a-string%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
If X is unknown, how do you establish that it's X?
– tripleee
Dec 12 '18 at 11:32
@tripleee im representing the unknown with X.. X is actually an interger
– Bret Joseph
Dec 12 '18 at 11:34
What I am trying to say is that your question is unclear. What would an acceptable answer look like if we don't know what X is, or how we can find out?
– tripleee
Dec 12 '18 at 11:39
Obviously if you know that X is 1, the answer is trivial;
sed 's/.*1.*/1/
but then what do you need the variable for? The result after the substitution is clearly identical to the input string, so there is no point in performing any substitution. Or are you trying to find out if the string contains 1?case $string in *1*) echo true;; esac
– tripleee
Dec 12 '18 at 11:40
the problem is I don't want to use the character
1
since X it not always 1– Bret Joseph
Dec 12 '18 at 11:44