How to find a fixed strings with grep?

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











up vote
0
down vote

favorite












How to find a fixed strings with grep if the required output is not shown with grep -F? For example:



egrep -r 'string1|string2|string3' /var/www/http


with this what should we use?







share|improve this question





















  • What output with which input do you expect?
    – mnille
    Apr 19 at 7:11










  • Are you just trying to find three different strings?
    – Nasir Riley
    Apr 19 at 7:18










  • i expect the oputput of three strings but if anything like hash(#) present in the starting and ending of the string then is shows nothing otherwise the output will be those three strings
    – Gurpreet kaur
    Apr 19 at 7:31










  • Please show some sample input and desired output.
    – agc
    Apr 19 at 7:32











  • path="/home/local/abc" grep -E 'Allow from 8.8.2.5|Allow from 192.3.0.4|DenyAll'* $path if [ $? -eq 0 ]; then echo hostname echo "ftp lock (not accessable outside)" exit 0 else echo hostname echo "ftp unlock ( accessable from outside)" exit 1 fi
    – Gurpreet kaur
    Apr 19 at 7:36














up vote
0
down vote

favorite












How to find a fixed strings with grep if the required output is not shown with grep -F? For example:



egrep -r 'string1|string2|string3' /var/www/http


with this what should we use?







share|improve this question





















  • What output with which input do you expect?
    – mnille
    Apr 19 at 7:11










  • Are you just trying to find three different strings?
    – Nasir Riley
    Apr 19 at 7:18










  • i expect the oputput of three strings but if anything like hash(#) present in the starting and ending of the string then is shows nothing otherwise the output will be those three strings
    – Gurpreet kaur
    Apr 19 at 7:31










  • Please show some sample input and desired output.
    – agc
    Apr 19 at 7:32











  • path="/home/local/abc" grep -E 'Allow from 8.8.2.5|Allow from 192.3.0.4|DenyAll'* $path if [ $? -eq 0 ]; then echo hostname echo "ftp lock (not accessable outside)" exit 0 else echo hostname echo "ftp unlock ( accessable from outside)" exit 1 fi
    – Gurpreet kaur
    Apr 19 at 7:36












up vote
0
down vote

favorite









up vote
0
down vote

favorite











How to find a fixed strings with grep if the required output is not shown with grep -F? For example:



egrep -r 'string1|string2|string3' /var/www/http


with this what should we use?







share|improve this question













How to find a fixed strings with grep if the required output is not shown with grep -F? For example:



egrep -r 'string1|string2|string3' /var/www/http


with this what should we use?









share|improve this question












share|improve this question




share|improve this question








edited Apr 19 at 7:13









Kusalananda

102k13199315




102k13199315









asked Apr 19 at 6:58









Gurpreet kaur

61




61











  • What output with which input do you expect?
    – mnille
    Apr 19 at 7:11










  • Are you just trying to find three different strings?
    – Nasir Riley
    Apr 19 at 7:18










  • i expect the oputput of three strings but if anything like hash(#) present in the starting and ending of the string then is shows nothing otherwise the output will be those three strings
    – Gurpreet kaur
    Apr 19 at 7:31










  • Please show some sample input and desired output.
    – agc
    Apr 19 at 7:32











  • path="/home/local/abc" grep -E 'Allow from 8.8.2.5|Allow from 192.3.0.4|DenyAll'* $path if [ $? -eq 0 ]; then echo hostname echo "ftp lock (not accessable outside)" exit 0 else echo hostname echo "ftp unlock ( accessable from outside)" exit 1 fi
    – Gurpreet kaur
    Apr 19 at 7:36
















  • What output with which input do you expect?
    – mnille
    Apr 19 at 7:11










  • Are you just trying to find three different strings?
    – Nasir Riley
    Apr 19 at 7:18










  • i expect the oputput of three strings but if anything like hash(#) present in the starting and ending of the string then is shows nothing otherwise the output will be those three strings
    – Gurpreet kaur
    Apr 19 at 7:31










  • Please show some sample input and desired output.
    – agc
    Apr 19 at 7:32











  • path="/home/local/abc" grep -E 'Allow from 8.8.2.5|Allow from 192.3.0.4|DenyAll'* $path if [ $? -eq 0 ]; then echo hostname echo "ftp lock (not accessable outside)" exit 0 else echo hostname echo "ftp unlock ( accessable from outside)" exit 1 fi
    – Gurpreet kaur
    Apr 19 at 7:36















What output with which input do you expect?
– mnille
Apr 19 at 7:11




What output with which input do you expect?
– mnille
Apr 19 at 7:11












Are you just trying to find three different strings?
– Nasir Riley
Apr 19 at 7:18




Are you just trying to find three different strings?
– Nasir Riley
Apr 19 at 7:18












i expect the oputput of three strings but if anything like hash(#) present in the starting and ending of the string then is shows nothing otherwise the output will be those three strings
– Gurpreet kaur
Apr 19 at 7:31




i expect the oputput of three strings but if anything like hash(#) present in the starting and ending of the string then is shows nothing otherwise the output will be those three strings
– Gurpreet kaur
Apr 19 at 7:31












Please show some sample input and desired output.
– agc
Apr 19 at 7:32





Please show some sample input and desired output.
– agc
Apr 19 at 7:32













path="/home/local/abc" grep -E 'Allow from 8.8.2.5|Allow from 192.3.0.4|DenyAll'* $path if [ $? -eq 0 ]; then echo hostname echo "ftp lock (not accessable outside)" exit 0 else echo hostname echo "ftp unlock ( accessable from outside)" exit 1 fi
– Gurpreet kaur
Apr 19 at 7:36




path="/home/local/abc" grep -E 'Allow from 8.8.2.5|Allow from 192.3.0.4|DenyAll'* $path if [ $? -eq 0 ]; then echo hostname echo "ftp lock (not accessable outside)" exit 0 else echo hostname echo "ftp unlock ( accessable from outside)" exit 1 fi
– Gurpreet kaur
Apr 19 at 7:36










3 Answers
3






active

oldest

votes

















up vote
2
down vote













Either use grep -E (the modern form for egrep) so | be treated as an extended regular expression alternation operator, but then you'd need to escape every other regular expression operator (like ., ?, *, ^, $, , [, (, ), , ) in your fixed strings.



Or use -F and pass the strings one per line or with several -es:



grep -Fe string1 -e string2 -e string3


Or:



grep -F 'string1
string2
string3'


Add the -x option if those fixed strings have to make up the entire matched line (as opposed to be found anywhere within the line without it). Most grep implementations also have a -w option for word match where for instance, string1 would match inside foo string1-2, but not inside foostring12.



For your specific example in comments, that would be:



grep -wE 'Allow from (8.8.2.5|192.3.0.4)|DenyAll'


Or:



grep -we 'Allow from 8.8.2.5' -e 'Allow from 192.3.0.4' -e 'DenyAll'


You'd want either -w or -x (assuming that's the whole lines, not even spaces around) here as otherwise it would also match on lines like Allow from 8.8.2.51.



You can also write it:



grep -xE '[[:space:]]*(Allow from (8.8.2.5|192.3.0.4)|DenyAll)[[:space:]]*'


To match on the full line (-x) but allowing leading and trailing spacing characters (here using [[:space:]] instead of [[:blank:]] to also allow the CR characters found at the end of lines coming from the MS-DOS world).



To look for those fixed strings anywhere in the line but not inside comments, you'd need to ensure that the part of the line leading to those fixed strings doesn't contain # characters. So something like:



grep -wE '^[^#]*(Allow from (8.8.2.5|192.3.0.4)|DenyAll)'


Note that the -w there doesn't apply to Allow as it applies to the whole matched string. So it would match on GAllow from for instance. Some grep implementations support <, > to match word boundaries explicitly (also known as b in some):



grep -E '^[^#]*<(Allow from (8.8.2.5|192.3.0.4)|DenyAll)>'


Also note that in apache2, configuration directives are case insensitive and any number of blanks are allowed between words. Also, I suspect you meant Deny from all instead of DenyAll, so maybe you'd want:



grep -iE '^[^#]*<(allow[[:blank:]]+from[[:blank:]]+(8.8.2.5|192.3.0.4)|deny[[:blank:]]+from[[:blank:]]+all)>'





share|improve this answer






























    up vote
    0
    down vote













    The pipes | need to be escaped | like so:



    grep 'string1|string2|string3' /var/www/http





    share|improve this answer





















    • The issue is that if one string contains a special character such as *, this will not work without escaping that/those characters. You would have to insert an extra step to escape all special characters in the strings.
      – Kusalananda
      Apr 19 at 7:37






    • 1




      Note that | is a GNU extension, unspecified per POSIX, not supported by all grep implementations.
      – Stéphane Chazelas
      Apr 19 at 8:41

















    up vote
    0
    down vote













    The issue is that



    grep -F -r 'string1|string2|string3' /var/www/http


    would look for the literal string string1|string2|string3 in the files. Note, since you are looking for fixed strings, there is no reason to use egrep (or grep -E).



    What you could do is to use



    printf 'string1nstring2nstring3n' | grep -f /dev/stdin -F -r /var/www/http


    This would make grep read the fixed strings to match against from standard input (provided by printf) and use them to get all lines that matched them in any file under the given directory.



    Anything that generates a text document with the patterns on separate lines may replace the printf above, including a simple text file (which could be read directly by grep -f textfile -F ...).




    Example from your comments:



    Look for any of the strings Allow from 8.8.2.5, Allow from 192.3.0.4 and DenyAll in the files in $dirpath:



    printf 'Allow from 8.8.2.5nAllow from 192.3.0.4nDenyAlln' |
    grep -f /dev/stdin -F -r "$dirpath"





    share|improve this answer























    • it is not working..Is there any alternative way to do this ?
      – Gurpreet kaur
      Apr 19 at 7:51










    • @Gurpreetkaur To be able to debug your issue, I would need to know what the exact command you tried and what the result was (including error messages etc.).
      – Kusalananda
      Apr 19 at 8:27










    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%2f438657%2fhow-to-find-a-fixed-strings-with-grep%23new-answer', 'question_page');

    );

    Post as a guest






























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote













    Either use grep -E (the modern form for egrep) so | be treated as an extended regular expression alternation operator, but then you'd need to escape every other regular expression operator (like ., ?, *, ^, $, , [, (, ), , ) in your fixed strings.



    Or use -F and pass the strings one per line or with several -es:



    grep -Fe string1 -e string2 -e string3


    Or:



    grep -F 'string1
    string2
    string3'


    Add the -x option if those fixed strings have to make up the entire matched line (as opposed to be found anywhere within the line without it). Most grep implementations also have a -w option for word match where for instance, string1 would match inside foo string1-2, but not inside foostring12.



    For your specific example in comments, that would be:



    grep -wE 'Allow from (8.8.2.5|192.3.0.4)|DenyAll'


    Or:



    grep -we 'Allow from 8.8.2.5' -e 'Allow from 192.3.0.4' -e 'DenyAll'


    You'd want either -w or -x (assuming that's the whole lines, not even spaces around) here as otherwise it would also match on lines like Allow from 8.8.2.51.



    You can also write it:



    grep -xE '[[:space:]]*(Allow from (8.8.2.5|192.3.0.4)|DenyAll)[[:space:]]*'


    To match on the full line (-x) but allowing leading and trailing spacing characters (here using [[:space:]] instead of [[:blank:]] to also allow the CR characters found at the end of lines coming from the MS-DOS world).



    To look for those fixed strings anywhere in the line but not inside comments, you'd need to ensure that the part of the line leading to those fixed strings doesn't contain # characters. So something like:



    grep -wE '^[^#]*(Allow from (8.8.2.5|192.3.0.4)|DenyAll)'


    Note that the -w there doesn't apply to Allow as it applies to the whole matched string. So it would match on GAllow from for instance. Some grep implementations support <, > to match word boundaries explicitly (also known as b in some):



    grep -E '^[^#]*<(Allow from (8.8.2.5|192.3.0.4)|DenyAll)>'


    Also note that in apache2, configuration directives are case insensitive and any number of blanks are allowed between words. Also, I suspect you meant Deny from all instead of DenyAll, so maybe you'd want:



    grep -iE '^[^#]*<(allow[[:blank:]]+from[[:blank:]]+(8.8.2.5|192.3.0.4)|deny[[:blank:]]+from[[:blank:]]+all)>'





    share|improve this answer



























      up vote
      2
      down vote













      Either use grep -E (the modern form for egrep) so | be treated as an extended regular expression alternation operator, but then you'd need to escape every other regular expression operator (like ., ?, *, ^, $, , [, (, ), , ) in your fixed strings.



      Or use -F and pass the strings one per line or with several -es:



      grep -Fe string1 -e string2 -e string3


      Or:



      grep -F 'string1
      string2
      string3'


      Add the -x option if those fixed strings have to make up the entire matched line (as opposed to be found anywhere within the line without it). Most grep implementations also have a -w option for word match where for instance, string1 would match inside foo string1-2, but not inside foostring12.



      For your specific example in comments, that would be:



      grep -wE 'Allow from (8.8.2.5|192.3.0.4)|DenyAll'


      Or:



      grep -we 'Allow from 8.8.2.5' -e 'Allow from 192.3.0.4' -e 'DenyAll'


      You'd want either -w or -x (assuming that's the whole lines, not even spaces around) here as otherwise it would also match on lines like Allow from 8.8.2.51.



      You can also write it:



      grep -xE '[[:space:]]*(Allow from (8.8.2.5|192.3.0.4)|DenyAll)[[:space:]]*'


      To match on the full line (-x) but allowing leading and trailing spacing characters (here using [[:space:]] instead of [[:blank:]] to also allow the CR characters found at the end of lines coming from the MS-DOS world).



      To look for those fixed strings anywhere in the line but not inside comments, you'd need to ensure that the part of the line leading to those fixed strings doesn't contain # characters. So something like:



      grep -wE '^[^#]*(Allow from (8.8.2.5|192.3.0.4)|DenyAll)'


      Note that the -w there doesn't apply to Allow as it applies to the whole matched string. So it would match on GAllow from for instance. Some grep implementations support <, > to match word boundaries explicitly (also known as b in some):



      grep -E '^[^#]*<(Allow from (8.8.2.5|192.3.0.4)|DenyAll)>'


      Also note that in apache2, configuration directives are case insensitive and any number of blanks are allowed between words. Also, I suspect you meant Deny from all instead of DenyAll, so maybe you'd want:



      grep -iE '^[^#]*<(allow[[:blank:]]+from[[:blank:]]+(8.8.2.5|192.3.0.4)|deny[[:blank:]]+from[[:blank:]]+all)>'





      share|improve this answer

























        up vote
        2
        down vote










        up vote
        2
        down vote









        Either use grep -E (the modern form for egrep) so | be treated as an extended regular expression alternation operator, but then you'd need to escape every other regular expression operator (like ., ?, *, ^, $, , [, (, ), , ) in your fixed strings.



        Or use -F and pass the strings one per line or with several -es:



        grep -Fe string1 -e string2 -e string3


        Or:



        grep -F 'string1
        string2
        string3'


        Add the -x option if those fixed strings have to make up the entire matched line (as opposed to be found anywhere within the line without it). Most grep implementations also have a -w option for word match where for instance, string1 would match inside foo string1-2, but not inside foostring12.



        For your specific example in comments, that would be:



        grep -wE 'Allow from (8.8.2.5|192.3.0.4)|DenyAll'


        Or:



        grep -we 'Allow from 8.8.2.5' -e 'Allow from 192.3.0.4' -e 'DenyAll'


        You'd want either -w or -x (assuming that's the whole lines, not even spaces around) here as otherwise it would also match on lines like Allow from 8.8.2.51.



        You can also write it:



        grep -xE '[[:space:]]*(Allow from (8.8.2.5|192.3.0.4)|DenyAll)[[:space:]]*'


        To match on the full line (-x) but allowing leading and trailing spacing characters (here using [[:space:]] instead of [[:blank:]] to also allow the CR characters found at the end of lines coming from the MS-DOS world).



        To look for those fixed strings anywhere in the line but not inside comments, you'd need to ensure that the part of the line leading to those fixed strings doesn't contain # characters. So something like:



        grep -wE '^[^#]*(Allow from (8.8.2.5|192.3.0.4)|DenyAll)'


        Note that the -w there doesn't apply to Allow as it applies to the whole matched string. So it would match on GAllow from for instance. Some grep implementations support <, > to match word boundaries explicitly (also known as b in some):



        grep -E '^[^#]*<(Allow from (8.8.2.5|192.3.0.4)|DenyAll)>'


        Also note that in apache2, configuration directives are case insensitive and any number of blanks are allowed between words. Also, I suspect you meant Deny from all instead of DenyAll, so maybe you'd want:



        grep -iE '^[^#]*<(allow[[:blank:]]+from[[:blank:]]+(8.8.2.5|192.3.0.4)|deny[[:blank:]]+from[[:blank:]]+all)>'





        share|improve this answer















        Either use grep -E (the modern form for egrep) so | be treated as an extended regular expression alternation operator, but then you'd need to escape every other regular expression operator (like ., ?, *, ^, $, , [, (, ), , ) in your fixed strings.



        Or use -F and pass the strings one per line or with several -es:



        grep -Fe string1 -e string2 -e string3


        Or:



        grep -F 'string1
        string2
        string3'


        Add the -x option if those fixed strings have to make up the entire matched line (as opposed to be found anywhere within the line without it). Most grep implementations also have a -w option for word match where for instance, string1 would match inside foo string1-2, but not inside foostring12.



        For your specific example in comments, that would be:



        grep -wE 'Allow from (8.8.2.5|192.3.0.4)|DenyAll'


        Or:



        grep -we 'Allow from 8.8.2.5' -e 'Allow from 192.3.0.4' -e 'DenyAll'


        You'd want either -w or -x (assuming that's the whole lines, not even spaces around) here as otherwise it would also match on lines like Allow from 8.8.2.51.



        You can also write it:



        grep -xE '[[:space:]]*(Allow from (8.8.2.5|192.3.0.4)|DenyAll)[[:space:]]*'


        To match on the full line (-x) but allowing leading and trailing spacing characters (here using [[:space:]] instead of [[:blank:]] to also allow the CR characters found at the end of lines coming from the MS-DOS world).



        To look for those fixed strings anywhere in the line but not inside comments, you'd need to ensure that the part of the line leading to those fixed strings doesn't contain # characters. So something like:



        grep -wE '^[^#]*(Allow from (8.8.2.5|192.3.0.4)|DenyAll)'


        Note that the -w there doesn't apply to Allow as it applies to the whole matched string. So it would match on GAllow from for instance. Some grep implementations support <, > to match word boundaries explicitly (also known as b in some):



        grep -E '^[^#]*<(Allow from (8.8.2.5|192.3.0.4)|DenyAll)>'


        Also note that in apache2, configuration directives are case insensitive and any number of blanks are allowed between words. Also, I suspect you meant Deny from all instead of DenyAll, so maybe you'd want:



        grep -iE '^[^#]*<(allow[[:blank:]]+from[[:blank:]]+(8.8.2.5|192.3.0.4)|deny[[:blank:]]+from[[:blank:]]+all)>'






        share|improve this answer















        share|improve this answer



        share|improve this answer








        edited Apr 19 at 9:19


























        answered Apr 19 at 8:36









        Stéphane Chazelas

        279k53514846




        279k53514846






















            up vote
            0
            down vote













            The pipes | need to be escaped | like so:



            grep 'string1|string2|string3' /var/www/http





            share|improve this answer





















            • The issue is that if one string contains a special character such as *, this will not work without escaping that/those characters. You would have to insert an extra step to escape all special characters in the strings.
              – Kusalananda
              Apr 19 at 7:37






            • 1




              Note that | is a GNU extension, unspecified per POSIX, not supported by all grep implementations.
              – Stéphane Chazelas
              Apr 19 at 8:41














            up vote
            0
            down vote













            The pipes | need to be escaped | like so:



            grep 'string1|string2|string3' /var/www/http





            share|improve this answer





















            • The issue is that if one string contains a special character such as *, this will not work without escaping that/those characters. You would have to insert an extra step to escape all special characters in the strings.
              – Kusalananda
              Apr 19 at 7:37






            • 1




              Note that | is a GNU extension, unspecified per POSIX, not supported by all grep implementations.
              – Stéphane Chazelas
              Apr 19 at 8:41












            up vote
            0
            down vote










            up vote
            0
            down vote









            The pipes | need to be escaped | like so:



            grep 'string1|string2|string3' /var/www/http





            share|improve this answer













            The pipes | need to be escaped | like so:



            grep 'string1|string2|string3' /var/www/http






            share|improve this answer













            share|improve this answer



            share|improve this answer











            answered Apr 19 at 7:28









            agc

            4,0091935




            4,0091935











            • The issue is that if one string contains a special character such as *, this will not work without escaping that/those characters. You would have to insert an extra step to escape all special characters in the strings.
              – Kusalananda
              Apr 19 at 7:37






            • 1




              Note that | is a GNU extension, unspecified per POSIX, not supported by all grep implementations.
              – Stéphane Chazelas
              Apr 19 at 8:41
















            • The issue is that if one string contains a special character such as *, this will not work without escaping that/those characters. You would have to insert an extra step to escape all special characters in the strings.
              – Kusalananda
              Apr 19 at 7:37






            • 1




              Note that | is a GNU extension, unspecified per POSIX, not supported by all grep implementations.
              – Stéphane Chazelas
              Apr 19 at 8:41















            The issue is that if one string contains a special character such as *, this will not work without escaping that/those characters. You would have to insert an extra step to escape all special characters in the strings.
            – Kusalananda
            Apr 19 at 7:37




            The issue is that if one string contains a special character such as *, this will not work without escaping that/those characters. You would have to insert an extra step to escape all special characters in the strings.
            – Kusalananda
            Apr 19 at 7:37




            1




            1




            Note that | is a GNU extension, unspecified per POSIX, not supported by all grep implementations.
            – Stéphane Chazelas
            Apr 19 at 8:41




            Note that | is a GNU extension, unspecified per POSIX, not supported by all grep implementations.
            – Stéphane Chazelas
            Apr 19 at 8:41










            up vote
            0
            down vote













            The issue is that



            grep -F -r 'string1|string2|string3' /var/www/http


            would look for the literal string string1|string2|string3 in the files. Note, since you are looking for fixed strings, there is no reason to use egrep (or grep -E).



            What you could do is to use



            printf 'string1nstring2nstring3n' | grep -f /dev/stdin -F -r /var/www/http


            This would make grep read the fixed strings to match against from standard input (provided by printf) and use them to get all lines that matched them in any file under the given directory.



            Anything that generates a text document with the patterns on separate lines may replace the printf above, including a simple text file (which could be read directly by grep -f textfile -F ...).




            Example from your comments:



            Look for any of the strings Allow from 8.8.2.5, Allow from 192.3.0.4 and DenyAll in the files in $dirpath:



            printf 'Allow from 8.8.2.5nAllow from 192.3.0.4nDenyAlln' |
            grep -f /dev/stdin -F -r "$dirpath"





            share|improve this answer























            • it is not working..Is there any alternative way to do this ?
              – Gurpreet kaur
              Apr 19 at 7:51










            • @Gurpreetkaur To be able to debug your issue, I would need to know what the exact command you tried and what the result was (including error messages etc.).
              – Kusalananda
              Apr 19 at 8:27














            up vote
            0
            down vote













            The issue is that



            grep -F -r 'string1|string2|string3' /var/www/http


            would look for the literal string string1|string2|string3 in the files. Note, since you are looking for fixed strings, there is no reason to use egrep (or grep -E).



            What you could do is to use



            printf 'string1nstring2nstring3n' | grep -f /dev/stdin -F -r /var/www/http


            This would make grep read the fixed strings to match against from standard input (provided by printf) and use them to get all lines that matched them in any file under the given directory.



            Anything that generates a text document with the patterns on separate lines may replace the printf above, including a simple text file (which could be read directly by grep -f textfile -F ...).




            Example from your comments:



            Look for any of the strings Allow from 8.8.2.5, Allow from 192.3.0.4 and DenyAll in the files in $dirpath:



            printf 'Allow from 8.8.2.5nAllow from 192.3.0.4nDenyAlln' |
            grep -f /dev/stdin -F -r "$dirpath"





            share|improve this answer























            • it is not working..Is there any alternative way to do this ?
              – Gurpreet kaur
              Apr 19 at 7:51










            • @Gurpreetkaur To be able to debug your issue, I would need to know what the exact command you tried and what the result was (including error messages etc.).
              – Kusalananda
              Apr 19 at 8:27












            up vote
            0
            down vote










            up vote
            0
            down vote









            The issue is that



            grep -F -r 'string1|string2|string3' /var/www/http


            would look for the literal string string1|string2|string3 in the files. Note, since you are looking for fixed strings, there is no reason to use egrep (or grep -E).



            What you could do is to use



            printf 'string1nstring2nstring3n' | grep -f /dev/stdin -F -r /var/www/http


            This would make grep read the fixed strings to match against from standard input (provided by printf) and use them to get all lines that matched them in any file under the given directory.



            Anything that generates a text document with the patterns on separate lines may replace the printf above, including a simple text file (which could be read directly by grep -f textfile -F ...).




            Example from your comments:



            Look for any of the strings Allow from 8.8.2.5, Allow from 192.3.0.4 and DenyAll in the files in $dirpath:



            printf 'Allow from 8.8.2.5nAllow from 192.3.0.4nDenyAlln' |
            grep -f /dev/stdin -F -r "$dirpath"





            share|improve this answer















            The issue is that



            grep -F -r 'string1|string2|string3' /var/www/http


            would look for the literal string string1|string2|string3 in the files. Note, since you are looking for fixed strings, there is no reason to use egrep (or grep -E).



            What you could do is to use



            printf 'string1nstring2nstring3n' | grep -f /dev/stdin -F -r /var/www/http


            This would make grep read the fixed strings to match against from standard input (provided by printf) and use them to get all lines that matched them in any file under the given directory.



            Anything that generates a text document with the patterns on separate lines may replace the printf above, including a simple text file (which could be read directly by grep -f textfile -F ...).




            Example from your comments:



            Look for any of the strings Allow from 8.8.2.5, Allow from 192.3.0.4 and DenyAll in the files in $dirpath:



            printf 'Allow from 8.8.2.5nAllow from 192.3.0.4nDenyAlln' |
            grep -f /dev/stdin -F -r "$dirpath"






            share|improve this answer















            share|improve this answer



            share|improve this answer








            edited Apr 19 at 8:30


























            answered Apr 19 at 7:24









            Kusalananda

            102k13199315




            102k13199315











            • it is not working..Is there any alternative way to do this ?
              – Gurpreet kaur
              Apr 19 at 7:51










            • @Gurpreetkaur To be able to debug your issue, I would need to know what the exact command you tried and what the result was (including error messages etc.).
              – Kusalananda
              Apr 19 at 8:27
















            • it is not working..Is there any alternative way to do this ?
              – Gurpreet kaur
              Apr 19 at 7:51










            • @Gurpreetkaur To be able to debug your issue, I would need to know what the exact command you tried and what the result was (including error messages etc.).
              – Kusalananda
              Apr 19 at 8:27















            it is not working..Is there any alternative way to do this ?
            – Gurpreet kaur
            Apr 19 at 7:51




            it is not working..Is there any alternative way to do this ?
            – Gurpreet kaur
            Apr 19 at 7:51












            @Gurpreetkaur To be able to debug your issue, I would need to know what the exact command you tried and what the result was (including error messages etc.).
            – Kusalananda
            Apr 19 at 8:27




            @Gurpreetkaur To be able to debug your issue, I would need to know what the exact command you tried and what the result was (including error messages etc.).
            – Kusalananda
            Apr 19 at 8:27












             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f438657%2fhow-to-find-a-fixed-strings-with-grep%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