I want to search something in a file knowing only few chars

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












0















I have a problem. I'm doing a script which one will get as a parameter $1 some charters. I have file "drinks.txt" where there have written few lines like this:



Cocacola
juice
CocaPepsi


I'm going to pass as a parameter "co" for example, and this should return me Cocacola and CocaPepsi. I'm failing with IF condition. Here is my code:



searchingParameter=$1
for drink in `cat drinks.txt`;do
if [ "$drink" == "$1*" ]; then
echo "$drink"
fi
done


So this should print every drink that found with $1 plus *.
What's the correct way to do the IF for this?



Thanks in advance.










share|improve this question


























    0















    I have a problem. I'm doing a script which one will get as a parameter $1 some charters. I have file "drinks.txt" where there have written few lines like this:



    Cocacola
    juice
    CocaPepsi


    I'm going to pass as a parameter "co" for example, and this should return me Cocacola and CocaPepsi. I'm failing with IF condition. Here is my code:



    searchingParameter=$1
    for drink in `cat drinks.txt`;do
    if [ "$drink" == "$1*" ]; then
    echo "$drink"
    fi
    done


    So this should print every drink that found with $1 plus *.
    What's the correct way to do the IF for this?



    Thanks in advance.










    share|improve this question
























      0












      0








      0








      I have a problem. I'm doing a script which one will get as a parameter $1 some charters. I have file "drinks.txt" where there have written few lines like this:



      Cocacola
      juice
      CocaPepsi


      I'm going to pass as a parameter "co" for example, and this should return me Cocacola and CocaPepsi. I'm failing with IF condition. Here is my code:



      searchingParameter=$1
      for drink in `cat drinks.txt`;do
      if [ "$drink" == "$1*" ]; then
      echo "$drink"
      fi
      done


      So this should print every drink that found with $1 plus *.
      What's the correct way to do the IF for this?



      Thanks in advance.










      share|improve this question














      I have a problem. I'm doing a script which one will get as a parameter $1 some charters. I have file "drinks.txt" where there have written few lines like this:



      Cocacola
      juice
      CocaPepsi


      I'm going to pass as a parameter "co" for example, and this should return me Cocacola and CocaPepsi. I'm failing with IF condition. Here is my code:



      searchingParameter=$1
      for drink in `cat drinks.txt`;do
      if [ "$drink" == "$1*" ]; then
      echo "$drink"
      fi
      done


      So this should print every drink that found with $1 plus *.
      What's the correct way to do the IF for this?



      Thanks in advance.







      shell-script






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Feb 7 at 19:21









      Multi17Multi17

      31




      31




















          1 Answer
          1






          active

          oldest

          votes


















          1














          Doing this with a shell loop is awkward and error prone. If your drinks.txt file contains a * on a line by itself (or surrounded by spaces), it would expand to the names of the files in the current directory, for example.



          Instead, just use grep:



          grep -iF 'co' drinks.txt


          Here, we use grep with -i to do a case-insensitive match. The -F option means that grep should use the pattern as a string rather than as a regular expression (this does not matter here, but would matter if your pattern contained characters like * or . or other special regular expression symbols).



          The command would return each complete line that contained the string co, Co, cO or CO.



          As part of a script:



          #!/bin/sh

          grep -iF -e "$1" drinks.txt


          I'm using -e here to say "the next argument is a pattern". If I didn't do that and $1 started with a dash, it would be taken as an option to grep.



          To force a match at the start of a word, you would use



          grep -i -e 'b'"$1" drinks.txt


          or



          grep -i -e '<'"$1" drinks.txt


          Where b and < both would match at a word boundary. Note that we had to remove the -F option here, because our pattern is now a regular expression. This also means that any regular expression characters in $1 will be special.






          share|improve this answer

























          • Thanks @Kusalananda! grep -i -e '<'"$1" drinks.txt helped me to solve the issue. Thanks again

            – Multi17
            Feb 7 at 19:43











          • b or < or [[:<:]] are not standard. Most portable of the three probably < (and even then, there are variations as to what constitutes a word character). POSIXly, you can do grep -i "^(.*[^[:alnum:]_])0,1$1"

            – Stéphane Chazelas
            Feb 7 at 20:18










          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%2f499344%2fi-want-to-search-something-in-a-file-knowing-only-few-chars%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Doing this with a shell loop is awkward and error prone. If your drinks.txt file contains a * on a line by itself (or surrounded by spaces), it would expand to the names of the files in the current directory, for example.



          Instead, just use grep:



          grep -iF 'co' drinks.txt


          Here, we use grep with -i to do a case-insensitive match. The -F option means that grep should use the pattern as a string rather than as a regular expression (this does not matter here, but would matter if your pattern contained characters like * or . or other special regular expression symbols).



          The command would return each complete line that contained the string co, Co, cO or CO.



          As part of a script:



          #!/bin/sh

          grep -iF -e "$1" drinks.txt


          I'm using -e here to say "the next argument is a pattern". If I didn't do that and $1 started with a dash, it would be taken as an option to grep.



          To force a match at the start of a word, you would use



          grep -i -e 'b'"$1" drinks.txt


          or



          grep -i -e '<'"$1" drinks.txt


          Where b and < both would match at a word boundary. Note that we had to remove the -F option here, because our pattern is now a regular expression. This also means that any regular expression characters in $1 will be special.






          share|improve this answer

























          • Thanks @Kusalananda! grep -i -e '<'"$1" drinks.txt helped me to solve the issue. Thanks again

            – Multi17
            Feb 7 at 19:43











          • b or < or [[:<:]] are not standard. Most portable of the three probably < (and even then, there are variations as to what constitutes a word character). POSIXly, you can do grep -i "^(.*[^[:alnum:]_])0,1$1"

            – Stéphane Chazelas
            Feb 7 at 20:18















          1














          Doing this with a shell loop is awkward and error prone. If your drinks.txt file contains a * on a line by itself (or surrounded by spaces), it would expand to the names of the files in the current directory, for example.



          Instead, just use grep:



          grep -iF 'co' drinks.txt


          Here, we use grep with -i to do a case-insensitive match. The -F option means that grep should use the pattern as a string rather than as a regular expression (this does not matter here, but would matter if your pattern contained characters like * or . or other special regular expression symbols).



          The command would return each complete line that contained the string co, Co, cO or CO.



          As part of a script:



          #!/bin/sh

          grep -iF -e "$1" drinks.txt


          I'm using -e here to say "the next argument is a pattern". If I didn't do that and $1 started with a dash, it would be taken as an option to grep.



          To force a match at the start of a word, you would use



          grep -i -e 'b'"$1" drinks.txt


          or



          grep -i -e '<'"$1" drinks.txt


          Where b and < both would match at a word boundary. Note that we had to remove the -F option here, because our pattern is now a regular expression. This also means that any regular expression characters in $1 will be special.






          share|improve this answer

























          • Thanks @Kusalananda! grep -i -e '<'"$1" drinks.txt helped me to solve the issue. Thanks again

            – Multi17
            Feb 7 at 19:43











          • b or < or [[:<:]] are not standard. Most portable of the three probably < (and even then, there are variations as to what constitutes a word character). POSIXly, you can do grep -i "^(.*[^[:alnum:]_])0,1$1"

            – Stéphane Chazelas
            Feb 7 at 20:18













          1












          1








          1







          Doing this with a shell loop is awkward and error prone. If your drinks.txt file contains a * on a line by itself (or surrounded by spaces), it would expand to the names of the files in the current directory, for example.



          Instead, just use grep:



          grep -iF 'co' drinks.txt


          Here, we use grep with -i to do a case-insensitive match. The -F option means that grep should use the pattern as a string rather than as a regular expression (this does not matter here, but would matter if your pattern contained characters like * or . or other special regular expression symbols).



          The command would return each complete line that contained the string co, Co, cO or CO.



          As part of a script:



          #!/bin/sh

          grep -iF -e "$1" drinks.txt


          I'm using -e here to say "the next argument is a pattern". If I didn't do that and $1 started with a dash, it would be taken as an option to grep.



          To force a match at the start of a word, you would use



          grep -i -e 'b'"$1" drinks.txt


          or



          grep -i -e '<'"$1" drinks.txt


          Where b and < both would match at a word boundary. Note that we had to remove the -F option here, because our pattern is now a regular expression. This also means that any regular expression characters in $1 will be special.






          share|improve this answer















          Doing this with a shell loop is awkward and error prone. If your drinks.txt file contains a * on a line by itself (or surrounded by spaces), it would expand to the names of the files in the current directory, for example.



          Instead, just use grep:



          grep -iF 'co' drinks.txt


          Here, we use grep with -i to do a case-insensitive match. The -F option means that grep should use the pattern as a string rather than as a regular expression (this does not matter here, but would matter if your pattern contained characters like * or . or other special regular expression symbols).



          The command would return each complete line that contained the string co, Co, cO or CO.



          As part of a script:



          #!/bin/sh

          grep -iF -e "$1" drinks.txt


          I'm using -e here to say "the next argument is a pattern". If I didn't do that and $1 started with a dash, it would be taken as an option to grep.



          To force a match at the start of a word, you would use



          grep -i -e 'b'"$1" drinks.txt


          or



          grep -i -e '<'"$1" drinks.txt


          Where b and < both would match at a word boundary. Note that we had to remove the -F option here, because our pattern is now a regular expression. This also means that any regular expression characters in $1 will be special.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Feb 7 at 19:39

























          answered Feb 7 at 19:28









          KusalanandaKusalananda

          133k17254417




          133k17254417












          • Thanks @Kusalananda! grep -i -e '<'"$1" drinks.txt helped me to solve the issue. Thanks again

            – Multi17
            Feb 7 at 19:43











          • b or < or [[:<:]] are not standard. Most portable of the three probably < (and even then, there are variations as to what constitutes a word character). POSIXly, you can do grep -i "^(.*[^[:alnum:]_])0,1$1"

            – Stéphane Chazelas
            Feb 7 at 20:18

















          • Thanks @Kusalananda! grep -i -e '<'"$1" drinks.txt helped me to solve the issue. Thanks again

            – Multi17
            Feb 7 at 19:43











          • b or < or [[:<:]] are not standard. Most portable of the three probably < (and even then, there are variations as to what constitutes a word character). POSIXly, you can do grep -i "^(.*[^[:alnum:]_])0,1$1"

            – Stéphane Chazelas
            Feb 7 at 20:18
















          Thanks @Kusalananda! grep -i -e '<'"$1" drinks.txt helped me to solve the issue. Thanks again

          – Multi17
          Feb 7 at 19:43





          Thanks @Kusalananda! grep -i -e '<'"$1" drinks.txt helped me to solve the issue. Thanks again

          – Multi17
          Feb 7 at 19:43













          b or < or [[:<:]] are not standard. Most portable of the three probably < (and even then, there are variations as to what constitutes a word character). POSIXly, you can do grep -i "^(.*[^[:alnum:]_])0,1$1"

          – Stéphane Chazelas
          Feb 7 at 20:18





          b or < or [[:<:]] are not standard. Most portable of the three probably < (and even then, there are variations as to what constitutes a word character). POSIXly, you can do grep -i "^(.*[^[:alnum:]_])0,1$1"

          – Stéphane Chazelas
          Feb 7 at 20:18

















          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.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f499344%2fi-want-to-search-something-in-a-file-knowing-only-few-chars%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?

          Displaying single band from multi-band raster using QGIS

          How many registers does an x86_64 CPU actually have?