Find the answer to your Linux question:
Results 1 to 6 of 6
I want to search files (basically .cc files) in /xx folder and subfolders. Those files (*.cc files) must contain #include "header.h" AND x() function. Clearly, I wanna list of *.cc ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Dec 2010
    Posts
    3

    how to grep?


    I want to search files (basically .cc files) in /xx folder and subfolders.
    Those files (*.cc files) must contain #include "header.h" AND x() function.

    Clearly,
    I wanna list of *.cc files that have 'header.h' & 'x()'. They must have two strings.

    I tried several ways, and got only files with header.h OR x().
    Some examples given in Internet doesn't give any result in my system.
    I am using Fedora 8.0 in my PC and I have grep 2.5.1 version.

  2. #2
    Linux Newbie
    Join Date
    Apr 2007
    Posts
    119
    Quote Originally Posted by ritika_sharma View Post
    I want to search files (basically .cc files) in /xx folder and subfolders.
    Those files (*.cc files) must contain #include "header.h" AND x() function.

    Clearly,
    I wanna list of *.cc files that have 'header.h' & 'x()'. They must have two strings.

    I tried several ways, and got only files with header.h OR x().
    Some examples given in Internet doesn't give any result in my system.
    I am using Fedora 8.0 in my PC and I have grep 2.5.1 version.
    You can use xargs along with piping into another grep

    Code:
    grep -Rl "header.h" *.cc | xargs  grep "x()"

  3. #3
    Linux User Manko10's Avatar
    Join Date
    Sep 2010
    Posts
    250
    To make it even saver, use the -Z parameter for grep and the -0 parameter for xargs. That will separate the different file names with NULL bytes instead of newlines.
    Refining Linux Advent calendar: 24 Outstanding ZSH Gems

  4. #4
    Just Joined!
    Join Date
    Dec 2010
    Posts
    16
    grep [hH]ello
    is the same as
    grep "[hH]hello"

    and this always confused me...

  5. #5
    Linux User Manko10's Avatar
    Join Date
    Sep 2010
    Posts
    250
    That's clear, since it's a character class.
    But what does this have to do with the topic?
    Refining Linux Advent calendar: 24 Outstanding ZSH Gems

  6. #6
    Just Joined!
    Join Date
    Dec 2010
    Location
    India
    Posts
    45
    grep -iR "x()" `grep -iR "header.h" *.cc | cut -f1 -d:` | cut -f1 -d:

    output will be the file containing both "header.h" and "x()"

Posting Permissions

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