RegEx - How to extract second matches using grep
How to extract second numbers in this text using grep command in ubuntu.
filename : Results.html?owner=12345
14219,98.2,31-s,19225
39219,92.7,31-s,29225
ouput desired :
12345:98.2
12345:92.7
Thanks
grep regex
New contributor
add a comment |
How to extract second numbers in this text using grep command in ubuntu.
filename : Results.html?owner=12345
14219,98.2,31-s,19225
39219,92.7,31-s,29225
ouput desired :
12345:98.2
12345:92.7
Thanks
grep regex
New contributor
Do you really need a regex for this? something likecut -d, -f2 file
would be simpler
– steeldriver
7 hours ago
yes im really need a regex in grep, because i have multiple files and by grep its a good for me because i get ouput with file name like this :file1:98.2
– H.Otmane
7 hours ago
add a comment |
How to extract second numbers in this text using grep command in ubuntu.
filename : Results.html?owner=12345
14219,98.2,31-s,19225
39219,92.7,31-s,29225
ouput desired :
12345:98.2
12345:92.7
Thanks
grep regex
New contributor
How to extract second numbers in this text using grep command in ubuntu.
filename : Results.html?owner=12345
14219,98.2,31-s,19225
39219,92.7,31-s,29225
ouput desired :
12345:98.2
12345:92.7
Thanks
grep regex
grep regex
New contributor
New contributor
edited 6 hours ago
H.Otmane
New contributor
asked 7 hours ago
H.OtmaneH.Otmane
52
52
New contributor
New contributor
Do you really need a regex for this? something likecut -d, -f2 file
would be simpler
– steeldriver
7 hours ago
yes im really need a regex in grep, because i have multiple files and by grep its a good for me because i get ouput with file name like this :file1:98.2
– H.Otmane
7 hours ago
add a comment |
Do you really need a regex for this? something likecut -d, -f2 file
would be simpler
– steeldriver
7 hours ago
yes im really need a regex in grep, because i have multiple files and by grep its a good for me because i get ouput with file name like this :file1:98.2
– H.Otmane
7 hours ago
Do you really need a regex for this? something like
cut -d, -f2 file
would be simpler– steeldriver
7 hours ago
Do you really need a regex for this? something like
cut -d, -f2 file
would be simpler– steeldriver
7 hours ago
yes im really need a regex in grep, because i have multiple files and by grep its a good for me because i get ouput with file name like this :
file1:98.2
– H.Otmane
7 hours ago
yes im really need a regex in grep, because i have multiple files and by grep its a good for me because i get ouput with file name like this :
file1:98.2
– H.Otmane
7 hours ago
add a comment |
2 Answers
2
active
oldest
votes
With GNU grep
, you can switch to PCRE mode where you can use lookarounds to match a number surrounded by commas:
$ grep -HPo '(?<=,)[0-9.]*(?=,)' file
file:98.2
file:92.7
OTOH if your data are comma-delimited a better option might be Awk:
$ awk -F, '{print FILENAME ":" $2}' file
file:98.2
file:92.7
or equivalently in perl perl -F, -nlE '{say "$ARGV:$F[1]"}' file
To split the filename in Awk you can use
$ awk -F, '{split(FILENAME,a,/=/); print a[2] ":" $2}' Results.html?owner=12345
12345:98.2
12345:92.7
or (slightly more efficiently) if you have GNU Awk
gawk -F, 'BEGINFILE{split(FILENAME,a,/=/)} {print a[2] ":" $2}' Results.html?owner=12345
Thanks mr @steeldriver its working fine
– H.Otmane
7 hours ago
thanks awk its best, please my filename like this :Results.html?owner=12345
i want get ouput like this :12345:98.2
– H.Otmane
7 hours ago
Edited :) thanks for your answer
– H.Otmane
6 hours ago
OMG i think isnt possible, but its possible with you mr steeldriver :) Thanks a lot
– H.Otmane
6 hours ago
add a comment |
According to this thread, there is not a right way to achieve that using RegEx
and grep
.
In your case you can retrieve the desired second column of the input data you provided, using awk
or sed
. An example using awk
is presented bellow:
#assuming test.dat contains your data
awk -F"," '{ print $2 }'
#assuming you want to read data from stdout
echo "14219,98.2,31-s,19225" | awk -F"," '{ print $2 }'
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
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
});
}
});
H.Otmane is a new contributor. Be nice, and check out our Code of Conduct.
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%2faskubuntu.com%2fquestions%2f1125160%2fregex-how-to-extract-second-matches-using-grep%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
With GNU grep
, you can switch to PCRE mode where you can use lookarounds to match a number surrounded by commas:
$ grep -HPo '(?<=,)[0-9.]*(?=,)' file
file:98.2
file:92.7
OTOH if your data are comma-delimited a better option might be Awk:
$ awk -F, '{print FILENAME ":" $2}' file
file:98.2
file:92.7
or equivalently in perl perl -F, -nlE '{say "$ARGV:$F[1]"}' file
To split the filename in Awk you can use
$ awk -F, '{split(FILENAME,a,/=/); print a[2] ":" $2}' Results.html?owner=12345
12345:98.2
12345:92.7
or (slightly more efficiently) if you have GNU Awk
gawk -F, 'BEGINFILE{split(FILENAME,a,/=/)} {print a[2] ":" $2}' Results.html?owner=12345
Thanks mr @steeldriver its working fine
– H.Otmane
7 hours ago
thanks awk its best, please my filename like this :Results.html?owner=12345
i want get ouput like this :12345:98.2
– H.Otmane
7 hours ago
Edited :) thanks for your answer
– H.Otmane
6 hours ago
OMG i think isnt possible, but its possible with you mr steeldriver :) Thanks a lot
– H.Otmane
6 hours ago
add a comment |
With GNU grep
, you can switch to PCRE mode where you can use lookarounds to match a number surrounded by commas:
$ grep -HPo '(?<=,)[0-9.]*(?=,)' file
file:98.2
file:92.7
OTOH if your data are comma-delimited a better option might be Awk:
$ awk -F, '{print FILENAME ":" $2}' file
file:98.2
file:92.7
or equivalently in perl perl -F, -nlE '{say "$ARGV:$F[1]"}' file
To split the filename in Awk you can use
$ awk -F, '{split(FILENAME,a,/=/); print a[2] ":" $2}' Results.html?owner=12345
12345:98.2
12345:92.7
or (slightly more efficiently) if you have GNU Awk
gawk -F, 'BEGINFILE{split(FILENAME,a,/=/)} {print a[2] ":" $2}' Results.html?owner=12345
Thanks mr @steeldriver its working fine
– H.Otmane
7 hours ago
thanks awk its best, please my filename like this :Results.html?owner=12345
i want get ouput like this :12345:98.2
– H.Otmane
7 hours ago
Edited :) thanks for your answer
– H.Otmane
6 hours ago
OMG i think isnt possible, but its possible with you mr steeldriver :) Thanks a lot
– H.Otmane
6 hours ago
add a comment |
With GNU grep
, you can switch to PCRE mode where you can use lookarounds to match a number surrounded by commas:
$ grep -HPo '(?<=,)[0-9.]*(?=,)' file
file:98.2
file:92.7
OTOH if your data are comma-delimited a better option might be Awk:
$ awk -F, '{print FILENAME ":" $2}' file
file:98.2
file:92.7
or equivalently in perl perl -F, -nlE '{say "$ARGV:$F[1]"}' file
To split the filename in Awk you can use
$ awk -F, '{split(FILENAME,a,/=/); print a[2] ":" $2}' Results.html?owner=12345
12345:98.2
12345:92.7
or (slightly more efficiently) if you have GNU Awk
gawk -F, 'BEGINFILE{split(FILENAME,a,/=/)} {print a[2] ":" $2}' Results.html?owner=12345
With GNU grep
, you can switch to PCRE mode where you can use lookarounds to match a number surrounded by commas:
$ grep -HPo '(?<=,)[0-9.]*(?=,)' file
file:98.2
file:92.7
OTOH if your data are comma-delimited a better option might be Awk:
$ awk -F, '{print FILENAME ":" $2}' file
file:98.2
file:92.7
or equivalently in perl perl -F, -nlE '{say "$ARGV:$F[1]"}' file
To split the filename in Awk you can use
$ awk -F, '{split(FILENAME,a,/=/); print a[2] ":" $2}' Results.html?owner=12345
12345:98.2
12345:92.7
or (slightly more efficiently) if you have GNU Awk
gawk -F, 'BEGINFILE{split(FILENAME,a,/=/)} {print a[2] ":" $2}' Results.html?owner=12345
edited 6 hours ago
answered 7 hours ago
steeldriversteeldriver
68.9k11113184
68.9k11113184
Thanks mr @steeldriver its working fine
– H.Otmane
7 hours ago
thanks awk its best, please my filename like this :Results.html?owner=12345
i want get ouput like this :12345:98.2
– H.Otmane
7 hours ago
Edited :) thanks for your answer
– H.Otmane
6 hours ago
OMG i think isnt possible, but its possible with you mr steeldriver :) Thanks a lot
– H.Otmane
6 hours ago
add a comment |
Thanks mr @steeldriver its working fine
– H.Otmane
7 hours ago
thanks awk its best, please my filename like this :Results.html?owner=12345
i want get ouput like this :12345:98.2
– H.Otmane
7 hours ago
Edited :) thanks for your answer
– H.Otmane
6 hours ago
OMG i think isnt possible, but its possible with you mr steeldriver :) Thanks a lot
– H.Otmane
6 hours ago
Thanks mr @steeldriver its working fine
– H.Otmane
7 hours ago
Thanks mr @steeldriver its working fine
– H.Otmane
7 hours ago
thanks awk its best, please my filename like this :
Results.html?owner=12345
i want get ouput like this : 12345:98.2
– H.Otmane
7 hours ago
thanks awk its best, please my filename like this :
Results.html?owner=12345
i want get ouput like this : 12345:98.2
– H.Otmane
7 hours ago
Edited :) thanks for your answer
– H.Otmane
6 hours ago
Edited :) thanks for your answer
– H.Otmane
6 hours ago
OMG i think isnt possible, but its possible with you mr steeldriver :) Thanks a lot
– H.Otmane
6 hours ago
OMG i think isnt possible, but its possible with you mr steeldriver :) Thanks a lot
– H.Otmane
6 hours ago
add a comment |
According to this thread, there is not a right way to achieve that using RegEx
and grep
.
In your case you can retrieve the desired second column of the input data you provided, using awk
or sed
. An example using awk
is presented bellow:
#assuming test.dat contains your data
awk -F"," '{ print $2 }'
#assuming you want to read data from stdout
echo "14219,98.2,31-s,19225" | awk -F"," '{ print $2 }'
add a comment |
According to this thread, there is not a right way to achieve that using RegEx
and grep
.
In your case you can retrieve the desired second column of the input data you provided, using awk
or sed
. An example using awk
is presented bellow:
#assuming test.dat contains your data
awk -F"," '{ print $2 }'
#assuming you want to read data from stdout
echo "14219,98.2,31-s,19225" | awk -F"," '{ print $2 }'
add a comment |
According to this thread, there is not a right way to achieve that using RegEx
and grep
.
In your case you can retrieve the desired second column of the input data you provided, using awk
or sed
. An example using awk
is presented bellow:
#assuming test.dat contains your data
awk -F"," '{ print $2 }'
#assuming you want to read data from stdout
echo "14219,98.2,31-s,19225" | awk -F"," '{ print $2 }'
According to this thread, there is not a right way to achieve that using RegEx
and grep
.
In your case you can retrieve the desired second column of the input data you provided, using awk
or sed
. An example using awk
is presented bellow:
#assuming test.dat contains your data
awk -F"," '{ print $2 }'
#assuming you want to read data from stdout
echo "14219,98.2,31-s,19225" | awk -F"," '{ print $2 }'
answered 6 hours ago
SarrimanSarriman
7317
7317
add a comment |
add a comment |
H.Otmane is a new contributor. Be nice, and check out our Code of Conduct.
H.Otmane is a new contributor. Be nice, and check out our Code of Conduct.
H.Otmane is a new contributor. Be nice, and check out our Code of Conduct.
H.Otmane is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Ask Ubuntu!
- 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%2faskubuntu.com%2fquestions%2f1125160%2fregex-how-to-extract-second-matches-using-grep%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
Do you really need a regex for this? something like
cut -d, -f2 file
would be simpler– steeldriver
7 hours ago
yes im really need a regex in grep, because i have multiple files and by grep its a good for me because i get ouput with file name like this :
file1:98.2
– H.Otmane
7 hours ago