Find the answer to your Linux question:
Results 1 to 4 of 4

Thread: find and grep

Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    find and grep


    Hi,

    I would like to search for a text in a file using find and grep command. For that I used following command

    find ./ -name *.C -exec grep "using namespace cem" {} \; -print

    and it worked correctly.

    Just wondering why following command fails to do the same any pointers would be appreciated..

    find ./ -name *.C | gerp "using namespace cem"

    shishir

  2. #2
    Linux Guru
    Join Date
    Mar 2003
    Location
    Wisconsin
    Posts
    1,907
    1. Please don't double post.
    2. It didn't work because you have gerp instead of grep.

    Jeremy
    Registered Linux user #346571
    "All The Dude ever wanted was his rug back" - The Dude

  3. #3
    Linux User
    Join Date
    Jul 2004
    Location
    Poland
    Posts
    368
    Because *.C gets replaced by all occurences of .C files in the current directory by the shell (so in fact the first version will not work unless you have a single file in directory). Enclose it in single quotes like in the following line:
    Code:
    find ./ -name '*.C' | grep "using namespace cem"
    By the way if you look for something in current directory you can omit ./
    "I don't know what I'm running from
    And I don't know where I'm running to
    There's something deep and strange inside of me I see"

  4. $spacer_open
    $spacer_close
  5. #4
    Linux User
    Join Date
    Jul 2004
    Location
    Poland
    Posts
    368
    Ahh, and another pointer. If you redirect find's output to grep what the latter is supposed to search in? Of course in the output of the former, i.e. in the names of files find generates. It's should be obvious that it is not the same as those files' contents. To do that you have to transform a program's input to the program's arguments. To do this use a command called xargs:
    Code:
    find ./ -name '*.C' | xargs grep "using namespace cem"
    "I don't know what I'm running from
    And I don't know where I'm running to
    There's something deep and strange inside of me I see"

Posting Permissions

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