Find 1st occurance of string pattern and extract a substring from it











up vote
-1
down vote

favorite












I need to search for 1st occurrence of string pattern "EPMAT-" in a log file and extract the numeric part from it. EPMAT- will be followed by some number. I would like to extract 20 from EPMAT-20 and print it.



Ex file:



This is a test  
test EPMAT-20 ......
....
EPMAT.33 test
end of test.









share|improve this question
























  • Please decide whether you want EPMAT searched or EMPAT...
    – user218374
    Mar 26 '17 at 17:31










  • sorry my bad, it is EPMAT
    – naren b
    Mar 27 '17 at 18:17















up vote
-1
down vote

favorite












I need to search for 1st occurrence of string pattern "EPMAT-" in a log file and extract the numeric part from it. EPMAT- will be followed by some number. I would like to extract 20 from EPMAT-20 and print it.



Ex file:



This is a test  
test EPMAT-20 ......
....
EPMAT.33 test
end of test.









share|improve this question
























  • Please decide whether you want EPMAT searched or EMPAT...
    – user218374
    Mar 26 '17 at 17:31










  • sorry my bad, it is EPMAT
    – naren b
    Mar 27 '17 at 18:17













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I need to search for 1st occurrence of string pattern "EPMAT-" in a log file and extract the numeric part from it. EPMAT- will be followed by some number. I would like to extract 20 from EPMAT-20 and print it.



Ex file:



This is a test  
test EPMAT-20 ......
....
EPMAT.33 test
end of test.









share|improve this question















I need to search for 1st occurrence of string pattern "EPMAT-" in a log file and extract the numeric part from it. EPMAT- will be followed by some number. I would like to extract 20 from EPMAT-20 and print it.



Ex file:



This is a test  
test EPMAT-20 ......
....
EPMAT.33 test
end of test.






sed






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 22:48









Rui F Ribeiro

38.2k1475125




38.2k1475125










asked Mar 26 '17 at 16:58









naren b

11




11












  • Please decide whether you want EPMAT searched or EMPAT...
    – user218374
    Mar 26 '17 at 17:31










  • sorry my bad, it is EPMAT
    – naren b
    Mar 27 '17 at 18:17


















  • Please decide whether you want EPMAT searched or EMPAT...
    – user218374
    Mar 26 '17 at 17:31










  • sorry my bad, it is EPMAT
    – naren b
    Mar 27 '17 at 18:17
















Please decide whether you want EPMAT searched or EMPAT...
– user218374
Mar 26 '17 at 17:31




Please decide whether you want EPMAT searched or EMPAT...
– user218374
Mar 26 '17 at 17:31












sorry my bad, it is EPMAT
– naren b
Mar 27 '17 at 18:17




sorry my bad, it is EPMAT
– naren b
Mar 27 '17 at 18:17










2 Answers
2






active

oldest

votes

















up vote
1
down vote













With sed:



sed -n '/EPMAT/{ s/.*EPMAT-//; s/[^0-9].*//; p; q; }' file





share|improve this answer




























    up vote
    1
    down vote













    grep -m1 -oP 'bEPMAT-Kd+' yourfile


    -m1 will just look at the first match in the whole of file



    -P will enable the Perl regex engine.



    -o will show only the matching portion



    perl -lne 'print,exit for /bEPMAT-Kd+/g' yourfile

    sed -ne '
    /<EPMAT-[0-9][0-9]*/{
    s//&
    /
    s/.*-([0-9]*n)/1/
    P;q
    }
    ' yourfile





    share|improve this answer























      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "106"
      };
      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',
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      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%2funix.stackexchange.com%2fquestions%2f353978%2ffind-1st-occurance-of-string-pattern-and-extract-a-substring-from-it%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








      up vote
      1
      down vote













      With sed:



      sed -n '/EPMAT/{ s/.*EPMAT-//; s/[^0-9].*//; p; q; }' file





      share|improve this answer

























        up vote
        1
        down vote













        With sed:



        sed -n '/EPMAT/{ s/.*EPMAT-//; s/[^0-9].*//; p; q; }' file





        share|improve this answer























          up vote
          1
          down vote










          up vote
          1
          down vote









          With sed:



          sed -n '/EPMAT/{ s/.*EPMAT-//; s/[^0-9].*//; p; q; }' file





          share|improve this answer












          With sed:



          sed -n '/EPMAT/{ s/.*EPMAT-//; s/[^0-9].*//; p; q; }' file






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 26 '17 at 17:22









          Satō Katsura

          10.9k11534




          10.9k11534
























              up vote
              1
              down vote













              grep -m1 -oP 'bEPMAT-Kd+' yourfile


              -m1 will just look at the first match in the whole of file



              -P will enable the Perl regex engine.



              -o will show only the matching portion



              perl -lne 'print,exit for /bEPMAT-Kd+/g' yourfile

              sed -ne '
              /<EPMAT-[0-9][0-9]*/{
              s//&
              /
              s/.*-([0-9]*n)/1/
              P;q
              }
              ' yourfile





              share|improve this answer



























                up vote
                1
                down vote













                grep -m1 -oP 'bEPMAT-Kd+' yourfile


                -m1 will just look at the first match in the whole of file



                -P will enable the Perl regex engine.



                -o will show only the matching portion



                perl -lne 'print,exit for /bEPMAT-Kd+/g' yourfile

                sed -ne '
                /<EPMAT-[0-9][0-9]*/{
                s//&
                /
                s/.*-([0-9]*n)/1/
                P;q
                }
                ' yourfile





                share|improve this answer

























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  grep -m1 -oP 'bEPMAT-Kd+' yourfile


                  -m1 will just look at the first match in the whole of file



                  -P will enable the Perl regex engine.



                  -o will show only the matching portion



                  perl -lne 'print,exit for /bEPMAT-Kd+/g' yourfile

                  sed -ne '
                  /<EPMAT-[0-9][0-9]*/{
                  s//&
                  /
                  s/.*-([0-9]*n)/1/
                  P;q
                  }
                  ' yourfile





                  share|improve this answer














                  grep -m1 -oP 'bEPMAT-Kd+' yourfile


                  -m1 will just look at the first match in the whole of file



                  -P will enable the Perl regex engine.



                  -o will show only the matching portion



                  perl -lne 'print,exit for /bEPMAT-Kd+/g' yourfile

                  sed -ne '
                  /<EPMAT-[0-9][0-9]*/{
                  s//&
                  /
                  s/.*-([0-9]*n)/1/
                  P;q
                  }
                  ' yourfile






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Mar 26 '17 at 17:30

























                  answered Mar 26 '17 at 17:14







                  user218374





































                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f353978%2ffind-1st-occurance-of-string-pattern-and-extract-a-substring-from-it%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

                      Morgemoulin

                      Scott Moir

                      Souastre