How to search each occurrence in a text file Linux?

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











up vote
1
down vote

favorite
1












I have 23 strings to search, I want it returns those that are in the file.



I got the code below:



users='User1|User2|User3|User4|User5|User6|User7|User8|User9|User10|User11|User12..User23'


Desired output:



User1 is in the file
User2 is not in the file
...
User 23 is in the file


I have no idea how to make it, I was thinking in an array but, I want some tips if is possible. Thanks in advance.










share|improve this question

























    up vote
    1
    down vote

    favorite
    1












    I have 23 strings to search, I want it returns those that are in the file.



    I got the code below:



    users='User1|User2|User3|User4|User5|User6|User7|User8|User9|User10|User11|User12..User23'


    Desired output:



    User1 is in the file
    User2 is not in the file
    ...
    User 23 is in the file


    I have no idea how to make it, I was thinking in an array but, I want some tips if is possible. Thanks in advance.










    share|improve this question























      up vote
      1
      down vote

      favorite
      1









      up vote
      1
      down vote

      favorite
      1






      1





      I have 23 strings to search, I want it returns those that are in the file.



      I got the code below:



      users='User1|User2|User3|User4|User5|User6|User7|User8|User9|User10|User11|User12..User23'


      Desired output:



      User1 is in the file
      User2 is not in the file
      ...
      User 23 is in the file


      I have no idea how to make it, I was thinking in an array but, I want some tips if is possible. Thanks in advance.










      share|improve this question













      I have 23 strings to search, I want it returns those that are in the file.



      I got the code below:



      users='User1|User2|User3|User4|User5|User6|User7|User8|User9|User10|User11|User12..User23'


      Desired output:



      User1 is in the file
      User2 is not in the file
      ...
      User 23 is in the file


      I have no idea how to make it, I was thinking in an array but, I want some tips if is possible. Thanks in advance.







      linux text-processing awk grep






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Aug 21 at 16:54









      Mareyes

      13611




      13611




















          4 Answers
          4






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          Try this,



          users=( User1 User2 User3 User4 )
          for i in "$users[@]"
          do
          grep -qw $i file && echo "$i is in the file" || echo "$i is not in the file"
          done


          From man:




          -q, --quiet, --silent



          Quiet; do not write anything to standard output. Exit immediately with zero status if any match is found, even if an error was detected.







          share|improve this answer


















          • 1




            Change that for loop index from $i to just i, and also quote the array "$users[@]" OTW liable to have issues with variable expansion. Plus the grep can be made more robust by invoking it with -w option to prevent grep from selecting user11 when it really was looking for user1.
            – Rakesh Sharma
            Aug 21 at 19:20

















          up vote
          4
          down vote













          Use an array:



          users=(User1 User2 User3 User4) # et cetera
          for i in "$users[@]"; do
          echo -n "$user is "
          if grep -q "$user" inputfile; then
          echo "present"
          else
          echo "not present"
          fi
          done


          grep -q will execute the search but not return any output, allowing you to use it silently in an if test.



          Alternatively, you can put each user on its on line in a file called Users, and then:



          grep -o -f Users inputfile


          This will output a list of all users seen. If you want to see both present and absent users, you could:



          echo "Users present:"
          grep -o -f Users inputfile
          echo "Users absent:"
          grep -vo -f Users inputfile





          share|improve this answer





























            up vote
            1
            down vote













            A further tweak.



            users=( User1 User2 User3 User4 )
            for i in "$users[@]"
            do
            echo "$i is" $(grep -qw $i file || echo "not") "in the file"
            done





            share|improve this answer



























              up vote
              1
              down vote













              With only a single scan through the file: this is bash



              # the array of user names
              users=( User1..23 )
              # an array of grep options: ( -e User1 -e User2 ...)
              for u in "$users[@]"; do grep_opts+=( -e "$u" ); done
              # scan the input file and save the user names that are present in the file
              readarray -t users_present < <(grep -Fo "$grep_opts[@]" input | sort -u)
              # find the user names absent from the file
              # this assumes there are no spaces in any of the user names.
              for u in "$users[@]"; do
              [[ " $users_present[*] " == *" $u "* ]] || users_absent+=( "$u" )
              done
              # and print out the results
              printf "%s is in the filen" "$users_present[@]"
              printf "%s is NOT in the filen" "$users_absent[@]"





              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%2f463918%2fhow-to-search-each-occurrence-in-a-text-file-linux%23new-answer', 'question_page');

                );

                Post as a guest






























                4 Answers
                4






                active

                oldest

                votes








                4 Answers
                4






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes








                up vote
                2
                down vote



                accepted










                Try this,



                users=( User1 User2 User3 User4 )
                for i in "$users[@]"
                do
                grep -qw $i file && echo "$i is in the file" || echo "$i is not in the file"
                done


                From man:




                -q, --quiet, --silent



                Quiet; do not write anything to standard output. Exit immediately with zero status if any match is found, even if an error was detected.







                share|improve this answer


















                • 1




                  Change that for loop index from $i to just i, and also quote the array "$users[@]" OTW liable to have issues with variable expansion. Plus the grep can be made more robust by invoking it with -w option to prevent grep from selecting user11 when it really was looking for user1.
                  – Rakesh Sharma
                  Aug 21 at 19:20














                up vote
                2
                down vote



                accepted










                Try this,



                users=( User1 User2 User3 User4 )
                for i in "$users[@]"
                do
                grep -qw $i file && echo "$i is in the file" || echo "$i is not in the file"
                done


                From man:




                -q, --quiet, --silent



                Quiet; do not write anything to standard output. Exit immediately with zero status if any match is found, even if an error was detected.







                share|improve this answer


















                • 1




                  Change that for loop index from $i to just i, and also quote the array "$users[@]" OTW liable to have issues with variable expansion. Plus the grep can be made more robust by invoking it with -w option to prevent grep from selecting user11 when it really was looking for user1.
                  – Rakesh Sharma
                  Aug 21 at 19:20












                up vote
                2
                down vote



                accepted







                up vote
                2
                down vote



                accepted






                Try this,



                users=( User1 User2 User3 User4 )
                for i in "$users[@]"
                do
                grep -qw $i file && echo "$i is in the file" || echo "$i is not in the file"
                done


                From man:




                -q, --quiet, --silent



                Quiet; do not write anything to standard output. Exit immediately with zero status if any match is found, even if an error was detected.







                share|improve this answer














                Try this,



                users=( User1 User2 User3 User4 )
                for i in "$users[@]"
                do
                grep -qw $i file && echo "$i is in the file" || echo "$i is not in the file"
                done


                From man:




                -q, --quiet, --silent



                Quiet; do not write anything to standard output. Exit immediately with zero status if any match is found, even if an error was detected.








                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Aug 21 at 19:22

























                answered Aug 21 at 17:01









                msp9011

                3,46643862




                3,46643862







                • 1




                  Change that for loop index from $i to just i, and also quote the array "$users[@]" OTW liable to have issues with variable expansion. Plus the grep can be made more robust by invoking it with -w option to prevent grep from selecting user11 when it really was looking for user1.
                  – Rakesh Sharma
                  Aug 21 at 19:20












                • 1




                  Change that for loop index from $i to just i, and also quote the array "$users[@]" OTW liable to have issues with variable expansion. Plus the grep can be made more robust by invoking it with -w option to prevent grep from selecting user11 when it really was looking for user1.
                  – Rakesh Sharma
                  Aug 21 at 19:20







                1




                1




                Change that for loop index from $i to just i, and also quote the array "$users[@]" OTW liable to have issues with variable expansion. Plus the grep can be made more robust by invoking it with -w option to prevent grep from selecting user11 when it really was looking for user1.
                – Rakesh Sharma
                Aug 21 at 19:20




                Change that for loop index from $i to just i, and also quote the array "$users[@]" OTW liable to have issues with variable expansion. Plus the grep can be made more robust by invoking it with -w option to prevent grep from selecting user11 when it really was looking for user1.
                – Rakesh Sharma
                Aug 21 at 19:20












                up vote
                4
                down vote













                Use an array:



                users=(User1 User2 User3 User4) # et cetera
                for i in "$users[@]"; do
                echo -n "$user is "
                if grep -q "$user" inputfile; then
                echo "present"
                else
                echo "not present"
                fi
                done


                grep -q will execute the search but not return any output, allowing you to use it silently in an if test.



                Alternatively, you can put each user on its on line in a file called Users, and then:



                grep -o -f Users inputfile


                This will output a list of all users seen. If you want to see both present and absent users, you could:



                echo "Users present:"
                grep -o -f Users inputfile
                echo "Users absent:"
                grep -vo -f Users inputfile





                share|improve this answer


























                  up vote
                  4
                  down vote













                  Use an array:



                  users=(User1 User2 User3 User4) # et cetera
                  for i in "$users[@]"; do
                  echo -n "$user is "
                  if grep -q "$user" inputfile; then
                  echo "present"
                  else
                  echo "not present"
                  fi
                  done


                  grep -q will execute the search but not return any output, allowing you to use it silently in an if test.



                  Alternatively, you can put each user on its on line in a file called Users, and then:



                  grep -o -f Users inputfile


                  This will output a list of all users seen. If you want to see both present and absent users, you could:



                  echo "Users present:"
                  grep -o -f Users inputfile
                  echo "Users absent:"
                  grep -vo -f Users inputfile





                  share|improve this answer
























                    up vote
                    4
                    down vote










                    up vote
                    4
                    down vote









                    Use an array:



                    users=(User1 User2 User3 User4) # et cetera
                    for i in "$users[@]"; do
                    echo -n "$user is "
                    if grep -q "$user" inputfile; then
                    echo "present"
                    else
                    echo "not present"
                    fi
                    done


                    grep -q will execute the search but not return any output, allowing you to use it silently in an if test.



                    Alternatively, you can put each user on its on line in a file called Users, and then:



                    grep -o -f Users inputfile


                    This will output a list of all users seen. If you want to see both present and absent users, you could:



                    echo "Users present:"
                    grep -o -f Users inputfile
                    echo "Users absent:"
                    grep -vo -f Users inputfile





                    share|improve this answer














                    Use an array:



                    users=(User1 User2 User3 User4) # et cetera
                    for i in "$users[@]"; do
                    echo -n "$user is "
                    if grep -q "$user" inputfile; then
                    echo "present"
                    else
                    echo "not present"
                    fi
                    done


                    grep -q will execute the search but not return any output, allowing you to use it silently in an if test.



                    Alternatively, you can put each user on its on line in a file called Users, and then:



                    grep -o -f Users inputfile


                    This will output a list of all users seen. If you want to see both present and absent users, you could:



                    echo "Users present:"
                    grep -o -f Users inputfile
                    echo "Users absent:"
                    grep -vo -f Users inputfile






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Aug 21 at 17:53

























                    answered Aug 21 at 17:07









                    DopeGhoti

                    41.1k55080




                    41.1k55080




















                        up vote
                        1
                        down vote













                        A further tweak.



                        users=( User1 User2 User3 User4 )
                        for i in "$users[@]"
                        do
                        echo "$i is" $(grep -qw $i file || echo "not") "in the file"
                        done





                        share|improve this answer
























                          up vote
                          1
                          down vote













                          A further tweak.



                          users=( User1 User2 User3 User4 )
                          for i in "$users[@]"
                          do
                          echo "$i is" $(grep -qw $i file || echo "not") "in the file"
                          done





                          share|improve this answer






















                            up vote
                            1
                            down vote










                            up vote
                            1
                            down vote









                            A further tweak.



                            users=( User1 User2 User3 User4 )
                            for i in "$users[@]"
                            do
                            echo "$i is" $(grep -qw $i file || echo "not") "in the file"
                            done





                            share|improve this answer












                            A further tweak.



                            users=( User1 User2 User3 User4 )
                            for i in "$users[@]"
                            do
                            echo "$i is" $(grep -qw $i file || echo "not") "in the file"
                            done






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Aug 21 at 20:06









                            steve

                            12.9k22149




                            12.9k22149




















                                up vote
                                1
                                down vote













                                With only a single scan through the file: this is bash



                                # the array of user names
                                users=( User1..23 )
                                # an array of grep options: ( -e User1 -e User2 ...)
                                for u in "$users[@]"; do grep_opts+=( -e "$u" ); done
                                # scan the input file and save the user names that are present in the file
                                readarray -t users_present < <(grep -Fo "$grep_opts[@]" input | sort -u)
                                # find the user names absent from the file
                                # this assumes there are no spaces in any of the user names.
                                for u in "$users[@]"; do
                                [[ " $users_present[*] " == *" $u "* ]] || users_absent+=( "$u" )
                                done
                                # and print out the results
                                printf "%s is in the filen" "$users_present[@]"
                                printf "%s is NOT in the filen" "$users_absent[@]"





                                share|improve this answer
























                                  up vote
                                  1
                                  down vote













                                  With only a single scan through the file: this is bash



                                  # the array of user names
                                  users=( User1..23 )
                                  # an array of grep options: ( -e User1 -e User2 ...)
                                  for u in "$users[@]"; do grep_opts+=( -e "$u" ); done
                                  # scan the input file and save the user names that are present in the file
                                  readarray -t users_present < <(grep -Fo "$grep_opts[@]" input | sort -u)
                                  # find the user names absent from the file
                                  # this assumes there are no spaces in any of the user names.
                                  for u in "$users[@]"; do
                                  [[ " $users_present[*] " == *" $u "* ]] || users_absent+=( "$u" )
                                  done
                                  # and print out the results
                                  printf "%s is in the filen" "$users_present[@]"
                                  printf "%s is NOT in the filen" "$users_absent[@]"





                                  share|improve this answer






















                                    up vote
                                    1
                                    down vote










                                    up vote
                                    1
                                    down vote









                                    With only a single scan through the file: this is bash



                                    # the array of user names
                                    users=( User1..23 )
                                    # an array of grep options: ( -e User1 -e User2 ...)
                                    for u in "$users[@]"; do grep_opts+=( -e "$u" ); done
                                    # scan the input file and save the user names that are present in the file
                                    readarray -t users_present < <(grep -Fo "$grep_opts[@]" input | sort -u)
                                    # find the user names absent from the file
                                    # this assumes there are no spaces in any of the user names.
                                    for u in "$users[@]"; do
                                    [[ " $users_present[*] " == *" $u "* ]] || users_absent+=( "$u" )
                                    done
                                    # and print out the results
                                    printf "%s is in the filen" "$users_present[@]"
                                    printf "%s is NOT in the filen" "$users_absent[@]"





                                    share|improve this answer












                                    With only a single scan through the file: this is bash



                                    # the array of user names
                                    users=( User1..23 )
                                    # an array of grep options: ( -e User1 -e User2 ...)
                                    for u in "$users[@]"; do grep_opts+=( -e "$u" ); done
                                    # scan the input file and save the user names that are present in the file
                                    readarray -t users_present < <(grep -Fo "$grep_opts[@]" input | sort -u)
                                    # find the user names absent from the file
                                    # this assumes there are no spaces in any of the user names.
                                    for u in "$users[@]"; do
                                    [[ " $users_present[*] " == *" $u "* ]] || users_absent+=( "$u" )
                                    done
                                    # and print out the results
                                    printf "%s is in the filen" "$users_present[@]"
                                    printf "%s is NOT in the filen" "$users_absent[@]"






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Aug 21 at 20:31









                                    glenn jackman

                                    47.8k365105




                                    47.8k365105



























                                         

                                        draft saved


                                        draft discarded















































                                         


                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function ()
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f463918%2fhow-to-search-each-occurrence-in-a-text-file-linux%23new-answer', 'question_page');

                                        );

                                        Post as a guest













































































                                        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