Find the answer to your Linux question:
Results 1 to 6 of 6
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 ...
  1. #1
    Just Joined!
    Join Date
    Aug 2011
    Posts
    3

    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
    Just Joined!
    Join Date
    Aug 2011
    Posts
    11
    for FILE in $(ls file*) ; do [ $(grep -c "one two" $FILE) -ne 0 ] && echo $FILE ; done

  3. #3
    Just Joined!
    Join Date
    Aug 2011
    Posts
    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. #4
    Just Joined!
    Join Date
    Aug 2011
    Posts
    11
    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.

  5. #5
    Just Joined!
    Join Date
    Aug 2011
    Posts
    3
    @theMickey

    Thanks a ton..that was really helpful

  6. #6
    Just Joined!
    Join Date
    Aug 2011
    Posts
    11
    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
  •  
...