How to find files containing set of words on linux [closed]

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











up vote
-1
down vote

favorite












I have number of text files line fruit1, fruit2, fruit3, fruit4 and so on in one directory. Now I want to find the files which contains all three words "Appale" , "Banana" & "Orange".



All three must be present in single file.










share|improve this question













closed as unclear what you're asking by Jeff Schaller, Rui F Ribeiro, schily, msp9011, RalfFriedl Dec 8 at 17:05


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.










  • 1




    What about Apple?
    – Rui F Ribeiro
    Dec 7 at 11:28






  • 4




    Why is the AIX tag included?
    – Jeff Schaller
    Dec 7 at 11:35






  • 1




    What have your tried and what errors do you encounter?
    – kemotep
    Dec 7 at 11:47










  • How to run grep with multiple AND patterns?
    – steeldriver
    Dec 7 at 13:40














up vote
-1
down vote

favorite












I have number of text files line fruit1, fruit2, fruit3, fruit4 and so on in one directory. Now I want to find the files which contains all three words "Appale" , "Banana" & "Orange".



All three must be present in single file.










share|improve this question













closed as unclear what you're asking by Jeff Schaller, Rui F Ribeiro, schily, msp9011, RalfFriedl Dec 8 at 17:05


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.










  • 1




    What about Apple?
    – Rui F Ribeiro
    Dec 7 at 11:28






  • 4




    Why is the AIX tag included?
    – Jeff Schaller
    Dec 7 at 11:35






  • 1




    What have your tried and what errors do you encounter?
    – kemotep
    Dec 7 at 11:47










  • How to run grep with multiple AND patterns?
    – steeldriver
    Dec 7 at 13:40












up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I have number of text files line fruit1, fruit2, fruit3, fruit4 and so on in one directory. Now I want to find the files which contains all three words "Appale" , "Banana" & "Orange".



All three must be present in single file.










share|improve this question













I have number of text files line fruit1, fruit2, fruit3, fruit4 and so on in one directory. Now I want to find the files which contains all three words "Appale" , "Banana" & "Orange".



All three must be present in single file.







linux aix






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 7 at 11:26









Nitin H

6




6




closed as unclear what you're asking by Jeff Schaller, Rui F Ribeiro, schily, msp9011, RalfFriedl Dec 8 at 17:05


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.






closed as unclear what you're asking by Jeff Schaller, Rui F Ribeiro, schily, msp9011, RalfFriedl Dec 8 at 17:05


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.









  • 1




    What about Apple?
    – Rui F Ribeiro
    Dec 7 at 11:28






  • 4




    Why is the AIX tag included?
    – Jeff Schaller
    Dec 7 at 11:35






  • 1




    What have your tried and what errors do you encounter?
    – kemotep
    Dec 7 at 11:47










  • How to run grep with multiple AND patterns?
    – steeldriver
    Dec 7 at 13:40












  • 1




    What about Apple?
    – Rui F Ribeiro
    Dec 7 at 11:28






  • 4




    Why is the AIX tag included?
    – Jeff Schaller
    Dec 7 at 11:35






  • 1




    What have your tried and what errors do you encounter?
    – kemotep
    Dec 7 at 11:47










  • How to run grep with multiple AND patterns?
    – steeldriver
    Dec 7 at 13:40







1




1




What about Apple?
– Rui F Ribeiro
Dec 7 at 11:28




What about Apple?
– Rui F Ribeiro
Dec 7 at 11:28




4




4




Why is the AIX tag included?
– Jeff Schaller
Dec 7 at 11:35




Why is the AIX tag included?
– Jeff Schaller
Dec 7 at 11:35




1




1




What have your tried and what errors do you encounter?
– kemotep
Dec 7 at 11:47




What have your tried and what errors do you encounter?
– kemotep
Dec 7 at 11:47












How to run grep with multiple AND patterns?
– steeldriver
Dec 7 at 13:40




How to run grep with multiple AND patterns?
– steeldriver
Dec 7 at 13:40










2 Answers
2






active

oldest

votes

















up vote
1
down vote













for filename in *; do
if [ -f "$filename" ] &&
grep -qF Appale -- "$filename" &&
grep -qF Banana -- "$filename" &&
grep -qF Orange -- "$filename"
then
printf '%s contains all three stringsn' "$filename"
fi
done


This would report any regular file (or symbolic link to a regular file) in the current directory that contains the all the given strings (not necessarily on the same line). If your grep supports -w, then you may use this as well to stop substrings from being found (such as the substring Banana in the word Bananas).



The -F makes grep do string matching rather than full regular expression matching, and the -q makes it quiet (we are only interested in the exit status of the utility).




A solution that generalises the above to any set of words:



set -- Appale Banana Orange Bumblebee Botswana

for filename in *; do
if [ ! -f "$filename" ] && continue

found=1
for string do
if ! grep -qF -e "$string" -- "$filename"; then
found=0
break
fi
done

if [ "$found" -eq 1 ]; then
printf '%s contains all stringsn' "$filename"
fi
done





share|improve this answer





























    up vote
    0
    down vote













    How about



    grep -E "Appale|Banana|Orange" fi* | cut -d: -f1 | uniq -c | grep "^ *3"
    3 file2
    3 file3


    Should patterns occur multiple times in a file, i.e. a count > 3, we need a different approach.






    share|improve this answer



























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      1
      down vote













      for filename in *; do
      if [ -f "$filename" ] &&
      grep -qF Appale -- "$filename" &&
      grep -qF Banana -- "$filename" &&
      grep -qF Orange -- "$filename"
      then
      printf '%s contains all three stringsn' "$filename"
      fi
      done


      This would report any regular file (or symbolic link to a regular file) in the current directory that contains the all the given strings (not necessarily on the same line). If your grep supports -w, then you may use this as well to stop substrings from being found (such as the substring Banana in the word Bananas).



      The -F makes grep do string matching rather than full regular expression matching, and the -q makes it quiet (we are only interested in the exit status of the utility).




      A solution that generalises the above to any set of words:



      set -- Appale Banana Orange Bumblebee Botswana

      for filename in *; do
      if [ ! -f "$filename" ] && continue

      found=1
      for string do
      if ! grep -qF -e "$string" -- "$filename"; then
      found=0
      break
      fi
      done

      if [ "$found" -eq 1 ]; then
      printf '%s contains all stringsn' "$filename"
      fi
      done





      share|improve this answer


























        up vote
        1
        down vote













        for filename in *; do
        if [ -f "$filename" ] &&
        grep -qF Appale -- "$filename" &&
        grep -qF Banana -- "$filename" &&
        grep -qF Orange -- "$filename"
        then
        printf '%s contains all three stringsn' "$filename"
        fi
        done


        This would report any regular file (or symbolic link to a regular file) in the current directory that contains the all the given strings (not necessarily on the same line). If your grep supports -w, then you may use this as well to stop substrings from being found (such as the substring Banana in the word Bananas).



        The -F makes grep do string matching rather than full regular expression matching, and the -q makes it quiet (we are only interested in the exit status of the utility).




        A solution that generalises the above to any set of words:



        set -- Appale Banana Orange Bumblebee Botswana

        for filename in *; do
        if [ ! -f "$filename" ] && continue

        found=1
        for string do
        if ! grep -qF -e "$string" -- "$filename"; then
        found=0
        break
        fi
        done

        if [ "$found" -eq 1 ]; then
        printf '%s contains all stringsn' "$filename"
        fi
        done





        share|improve this answer
























          up vote
          1
          down vote










          up vote
          1
          down vote









          for filename in *; do
          if [ -f "$filename" ] &&
          grep -qF Appale -- "$filename" &&
          grep -qF Banana -- "$filename" &&
          grep -qF Orange -- "$filename"
          then
          printf '%s contains all three stringsn' "$filename"
          fi
          done


          This would report any regular file (or symbolic link to a regular file) in the current directory that contains the all the given strings (not necessarily on the same line). If your grep supports -w, then you may use this as well to stop substrings from being found (such as the substring Banana in the word Bananas).



          The -F makes grep do string matching rather than full regular expression matching, and the -q makes it quiet (we are only interested in the exit status of the utility).




          A solution that generalises the above to any set of words:



          set -- Appale Banana Orange Bumblebee Botswana

          for filename in *; do
          if [ ! -f "$filename" ] && continue

          found=1
          for string do
          if ! grep -qF -e "$string" -- "$filename"; then
          found=0
          break
          fi
          done

          if [ "$found" -eq 1 ]; then
          printf '%s contains all stringsn' "$filename"
          fi
          done





          share|improve this answer














          for filename in *; do
          if [ -f "$filename" ] &&
          grep -qF Appale -- "$filename" &&
          grep -qF Banana -- "$filename" &&
          grep -qF Orange -- "$filename"
          then
          printf '%s contains all three stringsn' "$filename"
          fi
          done


          This would report any regular file (or symbolic link to a regular file) in the current directory that contains the all the given strings (not necessarily on the same line). If your grep supports -w, then you may use this as well to stop substrings from being found (such as the substring Banana in the word Bananas).



          The -F makes grep do string matching rather than full regular expression matching, and the -q makes it quiet (we are only interested in the exit status of the utility).




          A solution that generalises the above to any set of words:



          set -- Appale Banana Orange Bumblebee Botswana

          for filename in *; do
          if [ ! -f "$filename" ] && continue

          found=1
          for string do
          if ! grep -qF -e "$string" -- "$filename"; then
          found=0
          break
          fi
          done

          if [ "$found" -eq 1 ]; then
          printf '%s contains all stringsn' "$filename"
          fi
          done






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 7 at 13:10

























          answered Dec 7 at 12:58









          Kusalananda

          120k16225369




          120k16225369






















              up vote
              0
              down vote













              How about



              grep -E "Appale|Banana|Orange" fi* | cut -d: -f1 | uniq -c | grep "^ *3"
              3 file2
              3 file3


              Should patterns occur multiple times in a file, i.e. a count > 3, we need a different approach.






              share|improve this answer
























                up vote
                0
                down vote













                How about



                grep -E "Appale|Banana|Orange" fi* | cut -d: -f1 | uniq -c | grep "^ *3"
                3 file2
                3 file3


                Should patterns occur multiple times in a file, i.e. a count > 3, we need a different approach.






                share|improve this answer






















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  How about



                  grep -E "Appale|Banana|Orange" fi* | cut -d: -f1 | uniq -c | grep "^ *3"
                  3 file2
                  3 file3


                  Should patterns occur multiple times in a file, i.e. a count > 3, we need a different approach.






                  share|improve this answer












                  How about



                  grep -E "Appale|Banana|Orange" fi* | cut -d: -f1 | uniq -c | grep "^ *3"
                  3 file2
                  3 file3


                  Should patterns occur multiple times in a file, i.e. a count > 3, we need a different approach.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 7 at 12:34









                  RudiC

                  3,9391312




                  3,9391312












                      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?