grep : find bigger string in small string

Multi tool use
Multi tool use

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











up vote
1
down vote

favorite












I am writing a small shell script to receive an input and match against a text file.



However, the input would be a bigger string than the string in the file, against which I want a match.



User input:



abc-de-ef-gh1


Text in file:



This is test,-de-ef-gh,This is test


I want a positive result if I grep user-input with existing file.



How can I achieve this? I couldn't do it with grep.










share|improve this question



















  • 2




    how about echo "user input" | grep -f pattern_file
    – Kamaraj
    Aug 31 at 9:09










  • Kamaraj - it doesn't work.
    – Novice
    Aug 31 at 9:15






  • 1




    If the user input and the pattern are as in the question, then the comment of @Kamaraj will work. So your problem is not reproducible. Please give exact input and patterns. "It doesn't work" will not help us to help you.
    – chaos
    Aug 31 at 9:37










  • Apologies as I was unclear. Edited the question.
    – Novice
    Aug 31 at 9:54






  • 1




    This question is unclear. I sort-of understand it, but not clearly enough for it to be answerable.  @Novice: Do you want to match a line that contains abc-d-ef-gh1? How about f-gh? How about -gh1? How about gh or 1?  Describe the processes by which you would answer those questions; i.e., describe the rules that determine whether a string should match a line. Please do not respond in comments; edit your question to make it clearer and more complete.
    – G-Man
    Sep 2 at 20:59














up vote
1
down vote

favorite












I am writing a small shell script to receive an input and match against a text file.



However, the input would be a bigger string than the string in the file, against which I want a match.



User input:



abc-de-ef-gh1


Text in file:



This is test,-de-ef-gh,This is test


I want a positive result if I grep user-input with existing file.



How can I achieve this? I couldn't do it with grep.










share|improve this question



















  • 2




    how about echo "user input" | grep -f pattern_file
    – Kamaraj
    Aug 31 at 9:09










  • Kamaraj - it doesn't work.
    – Novice
    Aug 31 at 9:15






  • 1




    If the user input and the pattern are as in the question, then the comment of @Kamaraj will work. So your problem is not reproducible. Please give exact input and patterns. "It doesn't work" will not help us to help you.
    – chaos
    Aug 31 at 9:37










  • Apologies as I was unclear. Edited the question.
    – Novice
    Aug 31 at 9:54






  • 1




    This question is unclear. I sort-of understand it, but not clearly enough for it to be answerable.  @Novice: Do you want to match a line that contains abc-d-ef-gh1? How about f-gh? How about -gh1? How about gh or 1?  Describe the processes by which you would answer those questions; i.e., describe the rules that determine whether a string should match a line. Please do not respond in comments; edit your question to make it clearer and more complete.
    – G-Man
    Sep 2 at 20:59












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I am writing a small shell script to receive an input and match against a text file.



However, the input would be a bigger string than the string in the file, against which I want a match.



User input:



abc-de-ef-gh1


Text in file:



This is test,-de-ef-gh,This is test


I want a positive result if I grep user-input with existing file.



How can I achieve this? I couldn't do it with grep.










share|improve this question















I am writing a small shell script to receive an input and match against a text file.



However, the input would be a bigger string than the string in the file, against which I want a match.



User input:



abc-de-ef-gh1


Text in file:



This is test,-de-ef-gh,This is test


I want a positive result if I grep user-input with existing file.



How can I achieve this? I couldn't do it with grep.







linux shell grep regular-expression






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 31 at 9:51

























asked Aug 31 at 9:07









Novice

92




92







  • 2




    how about echo "user input" | grep -f pattern_file
    – Kamaraj
    Aug 31 at 9:09










  • Kamaraj - it doesn't work.
    – Novice
    Aug 31 at 9:15






  • 1




    If the user input and the pattern are as in the question, then the comment of @Kamaraj will work. So your problem is not reproducible. Please give exact input and patterns. "It doesn't work" will not help us to help you.
    – chaos
    Aug 31 at 9:37










  • Apologies as I was unclear. Edited the question.
    – Novice
    Aug 31 at 9:54






  • 1




    This question is unclear. I sort-of understand it, but not clearly enough for it to be answerable.  @Novice: Do you want to match a line that contains abc-d-ef-gh1? How about f-gh? How about -gh1? How about gh or 1?  Describe the processes by which you would answer those questions; i.e., describe the rules that determine whether a string should match a line. Please do not respond in comments; edit your question to make it clearer and more complete.
    – G-Man
    Sep 2 at 20:59












  • 2




    how about echo "user input" | grep -f pattern_file
    – Kamaraj
    Aug 31 at 9:09










  • Kamaraj - it doesn't work.
    – Novice
    Aug 31 at 9:15






  • 1




    If the user input and the pattern are as in the question, then the comment of @Kamaraj will work. So your problem is not reproducible. Please give exact input and patterns. "It doesn't work" will not help us to help you.
    – chaos
    Aug 31 at 9:37










  • Apologies as I was unclear. Edited the question.
    – Novice
    Aug 31 at 9:54






  • 1




    This question is unclear. I sort-of understand it, but not clearly enough for it to be answerable.  @Novice: Do you want to match a line that contains abc-d-ef-gh1? How about f-gh? How about -gh1? How about gh or 1?  Describe the processes by which you would answer those questions; i.e., describe the rules that determine whether a string should match a line. Please do not respond in comments; edit your question to make it clearer and more complete.
    – G-Man
    Sep 2 at 20:59







2




2




how about echo "user input" | grep -f pattern_file
– Kamaraj
Aug 31 at 9:09




how about echo "user input" | grep -f pattern_file
– Kamaraj
Aug 31 at 9:09












Kamaraj - it doesn't work.
– Novice
Aug 31 at 9:15




Kamaraj - it doesn't work.
– Novice
Aug 31 at 9:15




1




1




If the user input and the pattern are as in the question, then the comment of @Kamaraj will work. So your problem is not reproducible. Please give exact input and patterns. "It doesn't work" will not help us to help you.
– chaos
Aug 31 at 9:37




If the user input and the pattern are as in the question, then the comment of @Kamaraj will work. So your problem is not reproducible. Please give exact input and patterns. "It doesn't work" will not help us to help you.
– chaos
Aug 31 at 9:37












Apologies as I was unclear. Edited the question.
– Novice
Aug 31 at 9:54




Apologies as I was unclear. Edited the question.
– Novice
Aug 31 at 9:54




1




1




This question is unclear. I sort-of understand it, but not clearly enough for it to be answerable.  @Novice: Do you want to match a line that contains abc-d-ef-gh1? How about f-gh? How about -gh1? How about gh or 1?  Describe the processes by which you would answer those questions; i.e., describe the rules that determine whether a string should match a line. Please do not respond in comments; edit your question to make it clearer and more complete.
– G-Man
Sep 2 at 20:59




This question is unclear. I sort-of understand it, but not clearly enough for it to be answerable.  @Novice: Do you want to match a line that contains abc-d-ef-gh1? How about f-gh? How about -gh1? How about gh or 1?  Describe the processes by which you would answer those questions; i.e., describe the rules that determine whether a string should match a line. Please do not respond in comments; edit your question to make it clearer and more complete.
– G-Man
Sep 2 at 20:59










2 Answers
2






active

oldest

votes

















up vote
1
down vote













You have a file with patterns. grep can read the patterns with its -f option. If you want to check whether a string, $input, matches a pattern in patterns.txt, you may do



printf '%sn' "$input" |
if grep -q -f patterns.txt; then
echo 'matches'
else
echo 'does not match'
fi


This would run grep over the contents of the string with the patterns in patterns.txt. In bash you could instead use a here-string:



if grep -q -f patterns.txt <<<"$input"; then
echo 'matches'
else
echo 'does not match'
fi


The -q stops grep from producing output (we're only interested in the exit status).



If the patterns are fixed strings (not regular expressions), use -F with grep in addition to the other flags (grep -qF -f ...).






share|improve this answer



























    up vote
    0
    down vote













    Using bash, cut and grep:



    read -p "Hey user, input something: " $n
    printf "Match "
    grep -m 1 -q -f <(cut -d, -f2 file) <<< "$n" || printf "not "
    printf "found.n"


    Notes:



    • It's impossible to search for a longer string in a shorter, but this is really about finding whether shorter strings in file match a longer string.


    • cut is used to extract the middle field from file


    • grep searches for that middle field in the user input string.


    • printf provides some feedback.





    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: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      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%2f465963%2fgrep-find-bigger-string-in-small-string%23new-answer', 'question_page');

      );

      Post as a guest






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      1
      down vote













      You have a file with patterns. grep can read the patterns with its -f option. If you want to check whether a string, $input, matches a pattern in patterns.txt, you may do



      printf '%sn' "$input" |
      if grep -q -f patterns.txt; then
      echo 'matches'
      else
      echo 'does not match'
      fi


      This would run grep over the contents of the string with the patterns in patterns.txt. In bash you could instead use a here-string:



      if grep -q -f patterns.txt <<<"$input"; then
      echo 'matches'
      else
      echo 'does not match'
      fi


      The -q stops grep from producing output (we're only interested in the exit status).



      If the patterns are fixed strings (not regular expressions), use -F with grep in addition to the other flags (grep -qF -f ...).






      share|improve this answer
























        up vote
        1
        down vote













        You have a file with patterns. grep can read the patterns with its -f option. If you want to check whether a string, $input, matches a pattern in patterns.txt, you may do



        printf '%sn' "$input" |
        if grep -q -f patterns.txt; then
        echo 'matches'
        else
        echo 'does not match'
        fi


        This would run grep over the contents of the string with the patterns in patterns.txt. In bash you could instead use a here-string:



        if grep -q -f patterns.txt <<<"$input"; then
        echo 'matches'
        else
        echo 'does not match'
        fi


        The -q stops grep from producing output (we're only interested in the exit status).



        If the patterns are fixed strings (not regular expressions), use -F with grep in addition to the other flags (grep -qF -f ...).






        share|improve this answer






















          up vote
          1
          down vote










          up vote
          1
          down vote









          You have a file with patterns. grep can read the patterns with its -f option. If you want to check whether a string, $input, matches a pattern in patterns.txt, you may do



          printf '%sn' "$input" |
          if grep -q -f patterns.txt; then
          echo 'matches'
          else
          echo 'does not match'
          fi


          This would run grep over the contents of the string with the patterns in patterns.txt. In bash you could instead use a here-string:



          if grep -q -f patterns.txt <<<"$input"; then
          echo 'matches'
          else
          echo 'does not match'
          fi


          The -q stops grep from producing output (we're only interested in the exit status).



          If the patterns are fixed strings (not regular expressions), use -F with grep in addition to the other flags (grep -qF -f ...).






          share|improve this answer












          You have a file with patterns. grep can read the patterns with its -f option. If you want to check whether a string, $input, matches a pattern in patterns.txt, you may do



          printf '%sn' "$input" |
          if grep -q -f patterns.txt; then
          echo 'matches'
          else
          echo 'does not match'
          fi


          This would run grep over the contents of the string with the patterns in patterns.txt. In bash you could instead use a here-string:



          if grep -q -f patterns.txt <<<"$input"; then
          echo 'matches'
          else
          echo 'does not match'
          fi


          The -q stops grep from producing output (we're only interested in the exit status).



          If the patterns are fixed strings (not regular expressions), use -F with grep in addition to the other flags (grep -qF -f ...).







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Aug 31 at 9:37









          Kusalananda

          107k14209331




          107k14209331






















              up vote
              0
              down vote













              Using bash, cut and grep:



              read -p "Hey user, input something: " $n
              printf "Match "
              grep -m 1 -q -f <(cut -d, -f2 file) <<< "$n" || printf "not "
              printf "found.n"


              Notes:



              • It's impossible to search for a longer string in a shorter, but this is really about finding whether shorter strings in file match a longer string.


              • cut is used to extract the middle field from file


              • grep searches for that middle field in the user input string.


              • printf provides some feedback.





              share|improve this answer
























                up vote
                0
                down vote













                Using bash, cut and grep:



                read -p "Hey user, input something: " $n
                printf "Match "
                grep -m 1 -q -f <(cut -d, -f2 file) <<< "$n" || printf "not "
                printf "found.n"


                Notes:



                • It's impossible to search for a longer string in a shorter, but this is really about finding whether shorter strings in file match a longer string.


                • cut is used to extract the middle field from file


                • grep searches for that middle field in the user input string.


                • printf provides some feedback.





                share|improve this answer






















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  Using bash, cut and grep:



                  read -p "Hey user, input something: " $n
                  printf "Match "
                  grep -m 1 -q -f <(cut -d, -f2 file) <<< "$n" || printf "not "
                  printf "found.n"


                  Notes:



                  • It's impossible to search for a longer string in a shorter, but this is really about finding whether shorter strings in file match a longer string.


                  • cut is used to extract the middle field from file


                  • grep searches for that middle field in the user input string.


                  • printf provides some feedback.





                  share|improve this answer












                  Using bash, cut and grep:



                  read -p "Hey user, input something: " $n
                  printf "Match "
                  grep -m 1 -q -f <(cut -d, -f2 file) <<< "$n" || printf "not "
                  printf "found.n"


                  Notes:



                  • It's impossible to search for a longer string in a shorter, but this is really about finding whether shorter strings in file match a longer string.


                  • cut is used to extract the middle field from file


                  • grep searches for that middle field in the user input string.


                  • printf provides some feedback.






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Sep 2 at 5:27









                  agc

                  4,2651935




                  4,2651935



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f465963%2fgrep-find-bigger-string-in-small-string%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      u FoqUBfY 5GatfBEMtVkujxI,awgonAEqgxLoBY0t,JEgyTbl8 SfEKU3XApDBrjdovcXk HD1Mh V5Q lRAi4xnD,tUC9rVgf,b17,ownLA
                      iewewOAF6MSGMXrGsdlSp24xLxQptV3I KDlgrx8PklZiW urXfXc

                      Popular posts from this blog

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

                      How many registers does an x86_64 CPU actually have?

                      Displaying single band from multi-band raster using QGIS