[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: grep -R "include" *.c --- not working
From: |
Stephane Chazelas |
Subject: |
Re: grep -R "include" *.c --- not working |
Date: |
Tue, 5 Oct 2004 08:58:07 +0100 |
User-agent: |
Mutt/1.5.6i |
2004-10-4, 10:17(-06), Bob Proulx:
> Stepan Kasal wrote:
>> since you already use two GNU-specific features (-r and -0 in xargs), you
>> could as well adopt another one:
>>
>> find . -name '*.c' -print0 | xargs -r0 grep -H include
>> or
>> find . -name '*.c' -print0 | xargs -r0 grep --with-filename include
>
> Yes, I think your way here is best. Since I am using the short -r
> option the short -H follows naturally. You have converted me.
>
> I am always wanting to say the following. This will work way back on
> virtually any system. This is old classic way of doing it.
>
> find . -name '*.c' -print | xargs grep 'some_pattern' /dev/null
>
> That works fine for the unix user who would never create files with
> spaces or newlines in them.
[...]
Not only spaces, also single and double quotes and backslashes.
find output is simply not suited for xargs input.
The POSIX version is:
find . -name '*.c' -exec grep 'some_pattern' {} +
Unfortunately, GNU find is not POSIX conformant on that one
(also note that GNU find won't report a file named ".c" or
".foo.c" although they match the pattern).
So, to be POSIX conformant and compatible with GNU find (and
some other old find implementations), one has to do:
find .//. \( -name '*.c' -o -name '.*.c' -o -name .c \) -print |
sed 's/./\\&/g' | awk '
{
if (NR > 1) {
printf "%s", line
if ($0 !~ /\\\/\\\//) printf "\\"
print ""
}
line = $0
}
END { print line }' | xargs grep /dev/null
Also not AT&T tw (http://www.research.att.com/~gsf/tw/tw.html)
(untested).
--
Stephane
______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email
______________________________________________________________________