Page 1 of 3 123 LastLast
Results 1 to 10 of 25

Thread: Recursively looks for text files

  1. #1
    Join Date
    Aug 2006
    Beans
    445

    Recursively looks for text files

    I have been trying to cobble together a script that does the following:

    asks me to input 2 separate strings (2 single words if that's the technical limit)

    recursively looks for text files (purposely excluding binaries) that contain both the words

    displays each file that contains the words consecutively

    and

    keeps each file on the screen until I do something (hit "Enter" perhaps or TAB)

    I have played around with grep, -egrep, cat etc. and basically gotten nowhere.

    Can anyone help? Please,

    Thanks in advance.

  2. #2
    Join Date
    Apr 2012
    Beans
    7,256

    Re: Well this too is beyond my ken ,..

    AFAIK there's no easy way to logically AND matches in grep - you basically need to grep each file that contains pattern1 again to see if it contains pattern2, e.g. something like

    Code:
    while read -rd $'\0' file; do grep -qIw 'pattern1' "$file" && grep -qIw 'pattern2' "$file" && echo "$file"; done < <(find . -type f -print0)
    Note that the bash && logical operator is a 'short-circuit' evaluator i.e. it only executes the second grep if the first one returns true - so it's not as inefficient as all that (and it only echos the filename if both greps return true, which completes the test)

  3. #3
    Join Date
    Aug 2006
    Beans
    445

    Re: Well this too is beyond my ken ,..

    Thanks so much for your very complete and very prompt response.

    I guess what I wrote may have not been as precise as it should have been,

    What I want the script to do is show me (the content of) the file. The code you have so kindly provided lists the filename(s).

    That said, I think it "does the job" but it would be much easier to verify that if I saw the file(s).

    And that said, I really wanted an interrogative ... or perhaps you're saying that's not possible.

    Finally the complexity of the code fully explains why my pea brain wasn't able to get anywhere.

    Thanks again.

  4. #4
    Join Date
    Apr 2012
    Beans
    7,256

    Re: Recursively looks for text files

    You can replace the echo "$file" command with any other command you want to perform on the matching "$file" e.g. to cat the contents

    Code:
    while read -rd $'\0' file; do grep -qIw 'pattern1' "$file"  && grep -qIw 'pattern2' "$file" && cat  "$file"; done < <(find . -type f -print0)
    or to echo the name and then cat the contents - note the use of a subshell ( ... )

    Code:
    while read -rd $'\0' file; do grep -qIw 'pattern1' "$file" && grep -qIw 'pattern2' "$file" && (echo "$file: "; cat "$file"); done < <(find . -type f -print0)
    If your pattern1 and pattern2 are variables you will need to use double quotes not single quotes, like "$pattern1", "$pattern2" for example

    Code:
    read -p "Please enter the first pattern: " pattern1
    
    read -p "Please enter the second pattern: " pattern2
    
    while read -rd $'\0' file; do 
        grep -qIw "$pattern1" "$file" && grep -qIw "$pattern2" "$file" && (echo "$file: "; cat "$file")
    done < <(find . -type f -print0)
    Last edited by steeldriver; September 23rd, 2013 at 02:50 PM. Reason: added interactive prompts for the patterns

  5. #5
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Recursively looks for text files

    you can skip the find part entirely as grep is able to perform recursive searches and generate list files
    Code:
    while read -rd $'\0' file
    do
      grep -qIw "$pattern2" "$file" && (echo "$file: "; cat "$file")
    done < <( grep -ZlrIw "$pattern1" . )
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  6. #6
    Join Date
    Aug 2006
    Beans
    445

    Re: Recursively looks for text files

    steeldriver - I have tried all 3 of your suggestions and I think they ALL display all the files that they are supposed to.

    Thank you so VERY much.

    Why do I "think" they do? Well, because there is no pause/freeze between files - just one continuous outpouring.

    Is this fixable do you think?

  7. #7
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Recursively looks for text files

    few changes:
    Code:
    while read -r -u 3 -d $'\0' file
    do
      ...
      read -p "Press Enter to continue..."
    done 3< <( ... )
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  8. #8
    Join Date
    Aug 2006
    Beans
    445

    Re: Recursively looks for text files

    Vaphell - I tried your code.

    Dunno why but I get the following error:

    Code:
    emailfind2.sh: 6: Syntax error: redirection unexpected

  9. #9
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Recursively looks for text files

    Code:
    $ echo $pattern1 $pattern2
    echo bash
    $ while read -r -u 3 -d $'\0' file; do   echo "=== $file ========================" && grep -P "$pattern1|$pattern2|$" "$file"; read -p "Press Enter to continue..."; done 3< <( grep -ZlrIw "$pattern1" . 2>/dev/null | xargs -0 grep -Zlw "$pattern2" )
    === ./syntax.sh ========================
    #!/bin/bash
    
    if [ -f file ]
    then
      echo line1
      echo line2
    else
      echo line3
    fi
    Press Enter to continue...
    grep -P inside the loop is only used to colorize the output
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  10. #10
    Join Date
    Aug 2006
    Beans
    445

    Re: Recursively looks for text files

    Well I still don't know why I got that error. But I DO know how to get rid of it.

    I did some playing around and found that using

    Code:
    bash emailfind2.sh
    works just fine. It seems to me I've been "caught" with that before. Sorry about that.

    Just missing that elusive "in between" pause and we're good to go.

Page 1 of 3 123 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •