Newsgroup: comp.unix.programmer


Date: Wed, 12 Apr 2006 15:31:06 +0300
From: Diomidis Spinellis <dds@aueb.gr>
Organization: Athens University of Economics and Business
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.1) Gecko/20060130 SeaMonkey/1.0
MIME-Version: 1.0
Newsgroups: comp.unix.programmer
Subject: Re: problem with grep
References: <1144828524.313367.59970@z34g2000cwc.googlegroups.com> <1144829148.761905.296850@v46g2000cwv.googlegroups.com>
In-Reply-To: <1144829148.761905.296850@v46g2000cwv.googlegroups.com>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Maxim Yegorushkin wrote:
> jeniffer wrote:
>> >From a set of files of extension c ,i need to find a pattern string and
>> the output must also contain the filename of the file tht contains the
>> match.
>> i did
>> $ cat `find . -name '*.c'`|grep -H mystring
>> but its giving the location as standard input.
>> I need the name of the C file.
> 
> try something like:
> 
> find . -name "*.c" -exec grep -q mystring {} \; -print
> 

More efficiently:

find . -name '*.c' -print | xargs grep mystring /dev/null

(The /dev/null argument is needed to print a filename even if you find a 
  single .c file.)

On some systems the sequence

find . -name '*.c' -print0 | xargs -0 grep mystring /dev/null

will work even with pathnames containing spaces.


-- 
Diomidis Spinellis
Code Quality: The Open Source Perspective (Addison-Wesley 2006)
http://www.spinellis.gr/codequality



Newsgroup comp.unix.programmer contents
Newsgroup list
Diomidis Spinellis home page

Creative Commons License Unless otherwise expressly stated, all original material on this page created by Diomidis Spinellis is licensed under a Creative Commons Attribution-Share Alike 3.0 Greece License.