How to get a string untill second occurence of “-” and number












1















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/'









share|improve this question

























  • 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
















1















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/'









share|improve this question

























  • 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














1












1








1








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/'









share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 my awk approach, it might turn out more robust that grep 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











  • See my awk approach, it might turn out more robust that grep 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












2 Answers
2






active

oldest

votes


















2














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






share|improve this answer


























  • 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





















0














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 before 8080, [-_] separator can be used and then {print $6} is enough.






share|improve this answer























    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    2














    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






    share|improve this answer


























    • 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


















    2














    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






    share|improve this answer


























    • 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
















    2












    2








    2







    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






    share|improve this answer















    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







    share|improve this answer














    share|improve this answer



    share|improve this answer








    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





















    • 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















    0














    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 before 8080, [-_] separator can be used and then {print $6} is enough.






    share|improve this answer




























      0














      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 before 8080, [-_] separator can be used and then {print $6} is enough.






      share|improve this answer


























        0












        0








        0







        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 before 8080, [-_] separator can be used and then {print $6} is enough.






        share|improve this answer













        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 before 8080, [-_] separator can be used and then {print $6} is enough.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 '18 at 10:13









        Wiktor StribiżewWiktor Stribiżew

        313k16133207




        313k16133207






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Costa Masnaga

            Fotorealismo

            Sidney Franklin