How to get a string untill second occurence of “-” and number
An example: prod2-03_dl-httpd-prod-8080_access_referer_log.20181111-050000
I still need value till prod2-03_dl-httpd-prod
So basically we need value till second occurrence of '-' and number.
We tried following options:-
echo "prod2-03_dl-httpd-prod-8080_access_referer_log.20181111-050000" | sed -r 's/([^-][:digit:]+[^-][:digit:]).*/1/'
regex unix
add a comment |
An example: prod2-03_dl-httpd-prod-8080_access_referer_log.20181111-050000
I still need value till prod2-03_dl-httpd-prod
So basically we need value till second occurrence of '-' and number.
We tried following options:-
echo "prod2-03_dl-httpd-prod-8080_access_referer_log.20181111-050000" | sed -r 's/([^-][:digit:]+[^-][:digit:]).*/1/'
regex unix
You should probably read this: stackoverflow.com/help/someone-answers
– James Brown
Nov 22 '18 at 9:42
See myawk
approach, it might turn out more robust thatgrep
here.
– Wiktor Stribiżew
Nov 22 '18 at 10:16
add a comment |
An example: prod2-03_dl-httpd-prod-8080_access_referer_log.20181111-050000
I still need value till prod2-03_dl-httpd-prod
So basically we need value till second occurrence of '-' and number.
We tried following options:-
echo "prod2-03_dl-httpd-prod-8080_access_referer_log.20181111-050000" | sed -r 's/([^-][:digit:]+[^-][:digit:]).*/1/'
regex unix
An example: prod2-03_dl-httpd-prod-8080_access_referer_log.20181111-050000
I still need value till prod2-03_dl-httpd-prod
So basically we need value till second occurrence of '-' and number.
We tried following options:-
echo "prod2-03_dl-httpd-prod-8080_access_referer_log.20181111-050000" | sed -r 's/([^-][:digit:]+[^-][:digit:]).*/1/'
regex unix
regex unix
edited Nov 22 '18 at 9:20
tukan
5,5641828
5,5641828
asked Nov 22 '18 at 9:18
Prashant RanePrashant Rane
134
134
You should probably read this: stackoverflow.com/help/someone-answers
– James Brown
Nov 22 '18 at 9:42
See myawk
approach, it might turn out more robust thatgrep
here.
– Wiktor Stribiżew
Nov 22 '18 at 10:16
add a comment |
You should probably read this: stackoverflow.com/help/someone-answers
– James Brown
Nov 22 '18 at 9:42
See myawk
approach, it might turn out more robust thatgrep
here.
– Wiktor Stribiżew
Nov 22 '18 at 10:16
You should probably read this: stackoverflow.com/help/someone-answers
– James Brown
Nov 22 '18 at 9:42
You should probably read this: stackoverflow.com/help/someone-answers
– James Brown
Nov 22 '18 at 9:42
See my
awk
approach, it might turn out more robust that grep
here.– Wiktor Stribiżew
Nov 22 '18 at 10:16
See my
awk
approach, it might turn out more robust that grep
here.– Wiktor Stribiżew
Nov 22 '18 at 10:16
add a comment |
2 Answers
2
active
oldest
votes
Using pcregrep
and positive lookahead:
$ echo "prod2-03_dl-httpd-prod-8080_access_referer_log.20181111-050000" |
grep -Po "^[^-]*-.*?(?=-[0-9])"
prod2-03_dl-httpd-prod
Explained some:
grep -P
: using PCRE
^
from the beginning of the string
[^-]*
all non-dashes
-
followed by a dash
.*?
followed by anything non-greedily
(?=-[0-9])
positive lookahead for a dash and a number
Thanks James, Need one more help - can you please tell how get 8080 from same string.
– Prashant Rane
Nov 22 '18 at 9:59
Be more specific on the requirement, please.grep -o 8080 file
otherwise. ;D
– James Brown
Nov 22 '18 at 16:02
add a comment |
It seems the string can be parsed as a delimited string, use awk
:
s="prod2-03_dl-httpd-prod-8080_access_referer_log.20181111-050000"
awk -F'-' '{print $1 "-" $2 "-" $3}' <<< "$s"
# => prod2-03_dl-httpd
awk -F'-' '{sub(/_.*/, "", $5); print $5}' <<< "$s"
# => 8080
awk -F'[-_]' '{print $6}' <<< "$s"
# => 8080
See online awk
demo
Here,
-F'-'
sets field separator to-
{print $1 "-" $2 "-" $3}
prints Fields from 1 to 3 with the separator chars in between
sub(/_.*/, "", $5)
removes all text beginning with the first_
in Field 5 and then prints it- If the number of
-
and_
is constant before8080
,[-_]
separator can be used and then{print $6}
is enough.
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%2f53427468%2fhow-to-get-a-string-untill-second-occurence-of-and-number%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
Using pcregrep
and positive lookahead:
$ echo "prod2-03_dl-httpd-prod-8080_access_referer_log.20181111-050000" |
grep -Po "^[^-]*-.*?(?=-[0-9])"
prod2-03_dl-httpd-prod
Explained some:
grep -P
: using PCRE
^
from the beginning of the string
[^-]*
all non-dashes
-
followed by a dash
.*?
followed by anything non-greedily
(?=-[0-9])
positive lookahead for a dash and a number
Thanks James, Need one more help - can you please tell how get 8080 from same string.
– Prashant Rane
Nov 22 '18 at 9:59
Be more specific on the requirement, please.grep -o 8080 file
otherwise. ;D
– James Brown
Nov 22 '18 at 16:02
add a comment |
Using pcregrep
and positive lookahead:
$ echo "prod2-03_dl-httpd-prod-8080_access_referer_log.20181111-050000" |
grep -Po "^[^-]*-.*?(?=-[0-9])"
prod2-03_dl-httpd-prod
Explained some:
grep -P
: using PCRE
^
from the beginning of the string
[^-]*
all non-dashes
-
followed by a dash
.*?
followed by anything non-greedily
(?=-[0-9])
positive lookahead for a dash and a number
Thanks James, Need one more help - can you please tell how get 8080 from same string.
– Prashant Rane
Nov 22 '18 at 9:59
Be more specific on the requirement, please.grep -o 8080 file
otherwise. ;D
– James Brown
Nov 22 '18 at 16:02
add a comment |
Using pcregrep
and positive lookahead:
$ echo "prod2-03_dl-httpd-prod-8080_access_referer_log.20181111-050000" |
grep -Po "^[^-]*-.*?(?=-[0-9])"
prod2-03_dl-httpd-prod
Explained some:
grep -P
: using PCRE
^
from the beginning of the string
[^-]*
all non-dashes
-
followed by a dash
.*?
followed by anything non-greedily
(?=-[0-9])
positive lookahead for a dash and a number
Using pcregrep
and positive lookahead:
$ echo "prod2-03_dl-httpd-prod-8080_access_referer_log.20181111-050000" |
grep -Po "^[^-]*-.*?(?=-[0-9])"
prod2-03_dl-httpd-prod
Explained some:
grep -P
: using PCRE
^
from the beginning of the string
[^-]*
all non-dashes
-
followed by a dash
.*?
followed by anything non-greedily
(?=-[0-9])
positive lookahead for a dash and a number
edited Nov 22 '18 at 9:38
answered Nov 22 '18 at 9:32
James BrownJames Brown
18.4k31635
18.4k31635
Thanks James, Need one more help - can you please tell how get 8080 from same string.
– Prashant Rane
Nov 22 '18 at 9:59
Be more specific on the requirement, please.grep -o 8080 file
otherwise. ;D
– James Brown
Nov 22 '18 at 16:02
add a comment |
Thanks James, Need one more help - can you please tell how get 8080 from same string.
– Prashant Rane
Nov 22 '18 at 9:59
Be more specific on the requirement, please.grep -o 8080 file
otherwise. ;D
– James Brown
Nov 22 '18 at 16:02
Thanks James, Need one more help - can you please tell how get 8080 from same string.
– Prashant Rane
Nov 22 '18 at 9:59
Thanks James, Need one more help - can you please tell how get 8080 from same string.
– Prashant Rane
Nov 22 '18 at 9:59
Be more specific on the requirement, please.
grep -o 8080 file
otherwise. ;D– James Brown
Nov 22 '18 at 16:02
Be more specific on the requirement, please.
grep -o 8080 file
otherwise. ;D– James Brown
Nov 22 '18 at 16:02
add a comment |
It seems the string can be parsed as a delimited string, use awk
:
s="prod2-03_dl-httpd-prod-8080_access_referer_log.20181111-050000"
awk -F'-' '{print $1 "-" $2 "-" $3}' <<< "$s"
# => prod2-03_dl-httpd
awk -F'-' '{sub(/_.*/, "", $5); print $5}' <<< "$s"
# => 8080
awk -F'[-_]' '{print $6}' <<< "$s"
# => 8080
See online awk
demo
Here,
-F'-'
sets field separator to-
{print $1 "-" $2 "-" $3}
prints Fields from 1 to 3 with the separator chars in between
sub(/_.*/, "", $5)
removes all text beginning with the first_
in Field 5 and then prints it- If the number of
-
and_
is constant before8080
,[-_]
separator can be used and then{print $6}
is enough.
add a comment |
It seems the string can be parsed as a delimited string, use awk
:
s="prod2-03_dl-httpd-prod-8080_access_referer_log.20181111-050000"
awk -F'-' '{print $1 "-" $2 "-" $3}' <<< "$s"
# => prod2-03_dl-httpd
awk -F'-' '{sub(/_.*/, "", $5); print $5}' <<< "$s"
# => 8080
awk -F'[-_]' '{print $6}' <<< "$s"
# => 8080
See online awk
demo
Here,
-F'-'
sets field separator to-
{print $1 "-" $2 "-" $3}
prints Fields from 1 to 3 with the separator chars in between
sub(/_.*/, "", $5)
removes all text beginning with the first_
in Field 5 and then prints it- If the number of
-
and_
is constant before8080
,[-_]
separator can be used and then{print $6}
is enough.
add a comment |
It seems the string can be parsed as a delimited string, use awk
:
s="prod2-03_dl-httpd-prod-8080_access_referer_log.20181111-050000"
awk -F'-' '{print $1 "-" $2 "-" $3}' <<< "$s"
# => prod2-03_dl-httpd
awk -F'-' '{sub(/_.*/, "", $5); print $5}' <<< "$s"
# => 8080
awk -F'[-_]' '{print $6}' <<< "$s"
# => 8080
See online awk
demo
Here,
-F'-'
sets field separator to-
{print $1 "-" $2 "-" $3}
prints Fields from 1 to 3 with the separator chars in between
sub(/_.*/, "", $5)
removes all text beginning with the first_
in Field 5 and then prints it- If the number of
-
and_
is constant before8080
,[-_]
separator can be used and then{print $6}
is enough.
It seems the string can be parsed as a delimited string, use awk
:
s="prod2-03_dl-httpd-prod-8080_access_referer_log.20181111-050000"
awk -F'-' '{print $1 "-" $2 "-" $3}' <<< "$s"
# => prod2-03_dl-httpd
awk -F'-' '{sub(/_.*/, "", $5); print $5}' <<< "$s"
# => 8080
awk -F'[-_]' '{print $6}' <<< "$s"
# => 8080
See online awk
demo
Here,
-F'-'
sets field separator to-
{print $1 "-" $2 "-" $3}
prints Fields from 1 to 3 with the separator chars in between
sub(/_.*/, "", $5)
removes all text beginning with the first_
in Field 5 and then prints it- If the number of
-
and_
is constant before8080
,[-_]
separator can be used and then{print $6}
is enough.
answered Nov 22 '18 at 10:13
Wiktor StribiżewWiktor Stribiżew
313k16133207
313k16133207
add a comment |
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%2f53427468%2fhow-to-get-a-string-untill-second-occurence-of-and-number%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
You should probably read this: stackoverflow.com/help/someone-answers
– James Brown
Nov 22 '18 at 9:42
See my
awk
approach, it might turn out more robust thatgrep
here.– Wiktor Stribiżew
Nov 22 '18 at 10:16