How to grep all result such that the sub-pattern may or may not containing in the target pattern?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












0














Suppose I have search the string which give like the following result



anything1.knownKeyWord
anything2.knownKeyWord
anything3[1].knownKeyWord


How I can write generic syntax for grep such it match all 3 string.
I have done like this



^.*w+d[?[0]?[]]?.knownKeyWord.*$ 


But I think for indexing eg [1] is not written in good way, how can I achieve so that even i replace [1] with [2342jdsjf], I don't have to change the syntax much.










share|improve this question




























    0














    Suppose I have search the string which give like the following result



    anything1.knownKeyWord
    anything2.knownKeyWord
    anything3[1].knownKeyWord


    How I can write generic syntax for grep such it match all 3 string.
    I have done like this



    ^.*w+d[?[0]?[]]?.knownKeyWord.*$ 


    But I think for indexing eg [1] is not written in good way, how can I achieve so that even i replace [1] with [2342jdsjf], I don't have to change the syntax much.










    share|improve this question


























      0












      0








      0







      Suppose I have search the string which give like the following result



      anything1.knownKeyWord
      anything2.knownKeyWord
      anything3[1].knownKeyWord


      How I can write generic syntax for grep such it match all 3 string.
      I have done like this



      ^.*w+d[?[0]?[]]?.knownKeyWord.*$ 


      But I think for indexing eg [1] is not written in good way, how can I achieve so that even i replace [1] with [2342jdsjf], I don't have to change the syntax much.










      share|improve this question















      Suppose I have search the string which give like the following result



      anything1.knownKeyWord
      anything2.knownKeyWord
      anything3[1].knownKeyWord


      How I can write generic syntax for grep such it match all 3 string.
      I have done like this



      ^.*w+d[?[0]?[]]?.knownKeyWord.*$ 


      But I think for indexing eg [1] is not written in good way, how can I achieve so that even i replace [1] with [2342jdsjf], I don't have to change the syntax much.







      grep






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 17 at 12:09

























      asked Dec 17 at 12:04









      Vinay Sah

      32




      32




















          2 Answers
          2






          active

          oldest

          votes


















          0














          Using an extended regular expression:



          $ grep -E '[[:alnum:]_]+[[:digit:]]+([[^]]+])?.knownKeyWord' <file
          anything1.knownKeyWord
          anything2.knownKeyWord
          anything3[1].knownKeyWord


          This would extract any line containing a string on the format



          XXXNNN[YYY].knownKeyWord


          or



          XXXNNN.knownKeyWord


          where XXX is any non-empty alphanumeric string (that may also include _), NNN is any string of (one or more) digits, and YYY is anything not including a ].



          Use grep with -x if matches are to be complete lines. Use -w if matches are supposed to be complete words (i.e. not as a substring of something else).




          Just using sed to show what each part of the regular expression is matching:



          $ sed -E 's/([[:alnum:]_]+)([[:digit:]]+)([[^]]+])?(.knownKeyWord)/<1><2><3><4>/' <file
          <anything><1><><.knownKeyWord>
          <anything><2><><.knownKeyWord>
          <anything><3><[1]><.knownKeyWord>





          share|improve this answer




























            0














            Try this,



            grep -w 'knownKeyWord$' file.txt


            From man




            -w, --word-regexp



             Select only those lines containing matches that form whole words. The test is that the matching substring must either be at the beginning of the line, or
            preceded by a non-word constituent character. Similarly, it must be either at the end of the line or followed by a non-word constituent character. Word-
            constituent characters are letters, digits, and the underscore.






            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',
              autoActivateHeartbeat: false,
              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%2f489459%2fhow-to-grep-all-result-such-that-the-sub-pattern-may-or-may-not-containing-in-th%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









              0














              Using an extended regular expression:



              $ grep -E '[[:alnum:]_]+[[:digit:]]+([[^]]+])?.knownKeyWord' <file
              anything1.knownKeyWord
              anything2.knownKeyWord
              anything3[1].knownKeyWord


              This would extract any line containing a string on the format



              XXXNNN[YYY].knownKeyWord


              or



              XXXNNN.knownKeyWord


              where XXX is any non-empty alphanumeric string (that may also include _), NNN is any string of (one or more) digits, and YYY is anything not including a ].



              Use grep with -x if matches are to be complete lines. Use -w if matches are supposed to be complete words (i.e. not as a substring of something else).




              Just using sed to show what each part of the regular expression is matching:



              $ sed -E 's/([[:alnum:]_]+)([[:digit:]]+)([[^]]+])?(.knownKeyWord)/<1><2><3><4>/' <file
              <anything><1><><.knownKeyWord>
              <anything><2><><.knownKeyWord>
              <anything><3><[1]><.knownKeyWord>





              share|improve this answer

























                0














                Using an extended regular expression:



                $ grep -E '[[:alnum:]_]+[[:digit:]]+([[^]]+])?.knownKeyWord' <file
                anything1.knownKeyWord
                anything2.knownKeyWord
                anything3[1].knownKeyWord


                This would extract any line containing a string on the format



                XXXNNN[YYY].knownKeyWord


                or



                XXXNNN.knownKeyWord


                where XXX is any non-empty alphanumeric string (that may also include _), NNN is any string of (one or more) digits, and YYY is anything not including a ].



                Use grep with -x if matches are to be complete lines. Use -w if matches are supposed to be complete words (i.e. not as a substring of something else).




                Just using sed to show what each part of the regular expression is matching:



                $ sed -E 's/([[:alnum:]_]+)([[:digit:]]+)([[^]]+])?(.knownKeyWord)/<1><2><3><4>/' <file
                <anything><1><><.knownKeyWord>
                <anything><2><><.knownKeyWord>
                <anything><3><[1]><.knownKeyWord>





                share|improve this answer























                  0












                  0








                  0






                  Using an extended regular expression:



                  $ grep -E '[[:alnum:]_]+[[:digit:]]+([[^]]+])?.knownKeyWord' <file
                  anything1.knownKeyWord
                  anything2.knownKeyWord
                  anything3[1].knownKeyWord


                  This would extract any line containing a string on the format



                  XXXNNN[YYY].knownKeyWord


                  or



                  XXXNNN.knownKeyWord


                  where XXX is any non-empty alphanumeric string (that may also include _), NNN is any string of (one or more) digits, and YYY is anything not including a ].



                  Use grep with -x if matches are to be complete lines. Use -w if matches are supposed to be complete words (i.e. not as a substring of something else).




                  Just using sed to show what each part of the regular expression is matching:



                  $ sed -E 's/([[:alnum:]_]+)([[:digit:]]+)([[^]]+])?(.knownKeyWord)/<1><2><3><4>/' <file
                  <anything><1><><.knownKeyWord>
                  <anything><2><><.knownKeyWord>
                  <anything><3><[1]><.knownKeyWord>





                  share|improve this answer












                  Using an extended regular expression:



                  $ grep -E '[[:alnum:]_]+[[:digit:]]+([[^]]+])?.knownKeyWord' <file
                  anything1.knownKeyWord
                  anything2.knownKeyWord
                  anything3[1].knownKeyWord


                  This would extract any line containing a string on the format



                  XXXNNN[YYY].knownKeyWord


                  or



                  XXXNNN.knownKeyWord


                  where XXX is any non-empty alphanumeric string (that may also include _), NNN is any string of (one or more) digits, and YYY is anything not including a ].



                  Use grep with -x if matches are to be complete lines. Use -w if matches are supposed to be complete words (i.e. not as a substring of something else).




                  Just using sed to show what each part of the regular expression is matching:



                  $ sed -E 's/([[:alnum:]_]+)([[:digit:]]+)([[^]]+])?(.knownKeyWord)/<1><2><3><4>/' <file
                  <anything><1><><.knownKeyWord>
                  <anything><2><><.knownKeyWord>
                  <anything><3><[1]><.knownKeyWord>






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 17 at 13:01









                  Kusalananda

                  121k16229372




                  121k16229372























                      0














                      Try this,



                      grep -w 'knownKeyWord$' file.txt


                      From man




                      -w, --word-regexp



                       Select only those lines containing matches that form whole words. The test is that the matching substring must either be at the beginning of the line, or
                      preceded by a non-word constituent character. Similarly, it must be either at the end of the line or followed by a non-word constituent character. Word-
                      constituent characters are letters, digits, and the underscore.






                      share|improve this answer



























                        0














                        Try this,



                        grep -w 'knownKeyWord$' file.txt


                        From man




                        -w, --word-regexp



                         Select only those lines containing matches that form whole words. The test is that the matching substring must either be at the beginning of the line, or
                        preceded by a non-word constituent character. Similarly, it must be either at the end of the line or followed by a non-word constituent character. Word-
                        constituent characters are letters, digits, and the underscore.






                        share|improve this answer

























                          0












                          0








                          0






                          Try this,



                          grep -w 'knownKeyWord$' file.txt


                          From man




                          -w, --word-regexp



                           Select only those lines containing matches that form whole words. The test is that the matching substring must either be at the beginning of the line, or
                          preceded by a non-word constituent character. Similarly, it must be either at the end of the line or followed by a non-word constituent character. Word-
                          constituent characters are letters, digits, and the underscore.






                          share|improve this answer














                          Try this,



                          grep -w 'knownKeyWord$' file.txt


                          From man




                          -w, --word-regexp



                           Select only those lines containing matches that form whole words. The test is that the matching substring must either be at the beginning of the line, or
                          preceded by a non-word constituent character. Similarly, it must be either at the end of the line or followed by a non-word constituent character. Word-
                          constituent characters are letters, digits, and the underscore.







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Dec 17 at 13:11

























                          answered Dec 17 at 13:05









                          msp9011

                          3,74343863




                          3,74343863



























                              draft saved

                              draft discarded
















































                              Thanks for contributing an answer to Unix & Linux Stack Exchange!


                              • 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.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • 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%2funix.stackexchange.com%2fquestions%2f489459%2fhow-to-grep-all-result-such-that-the-sub-pattern-may-or-may-not-containing-in-th%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

                              How to check contact read email or not when send email to Individual?

                              Bahrain

                              Postfix configuration issue with fips on centos 7; mailgun relay