Find the answer to your Linux question:
Results 1 to 6 of 6
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    Exclamation help required to find multiple strings in multiple files


    Hi,

    i have 3 files:- file1,file2,file3
    file1 contains the data:- one
    file2 contains the data:- two
    file3 contains the data:- one two

    can anyone help me how to find the file having the data 'one two' only using unix command

  2. #2
    for FILE in $(ls file*) ; do [ $(grep -c "one two" $FILE) -ne 0 ] && echo $FILE ; done

  3. #3
    @theMickey

    can u provide a generic command or script for my question?? for ex i have many files in a directory and i need to find the files having the strings given by the user, which may or may not be in the same line within a files

  4. $spacer_open
    $spacer_close
  5. #4
    Hm. Let's see, should be something like this:

    Code:
    #!/bin/sh
    
    # Search Strings
    S1="one"
    S2="two"
    
    # Directory where to search files
    DIR=.
    
    # Search subdirectories?
    SUBDIR=false
    
    # Do the real stuff
    $SUBDIR && MAXDEPTH="" || MAXDEPTH="-maxdepth 1"
    
    for FILE in $(find $DIR $MAXDEPTH -type f -print) ; do
      [ $(grep -c $S1 $FILE) -ne 0 -a $(grep -c $S2 $FILE) -ne 0 ] && echo $FILE
    done
    Hope that helps.

  6. #5
    @theMickey

    Thanks a ton..that was really helpful

  7. #6
    You're welcome.

Posting Permissions

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