a question about GREP

Ben Scott dragonhawk at gmail.com
Fri Mar 23 18:12:05 EDT 2007


On 3/23/07, Jerry <greenmt at gmail.com> wrote:
> For the --include or --exclude option, what is "file matching PATTERN"
> supposed to mean?

  Typically, it's a shell glob.  My testing appears to confirm that.

> I supposed it means "file name" match PATTERN, not "file
> content" match patten, am I right?

  Yah.

> Find out all plain text files whose file names contain "out" and whose
> contents containing "zip" (in the form of whole word),  and then output
> these files names to a file called zip.txt.

I think this should work:

	grep -lwir --include=\*out\* . > zip.txt

Using long options:

	grep  --files-with-matches --word-regexp --ignore-case \
		--recursive --include=\*out\* . > zip.txt

  The backslashes before the stars (\*out\*) are needed because
otherwise the shell will try to expand them, which may prevent grep
from seing them.

> grep -Hwli -r --include=out "zip" *  > zip.txt

  The biggest problem there is that the include PATTERN is just "out",
which means the filename would have to be just "out".  Not "without"
or "outside".  By putting the stars around it, as I did, it will match
anything (including nothing) on either side, as well.

  The * you give for the file name will be expanded by the shell,
which may or may not give you what you want.  I used just ".", which
is the current directory.  Let grep handle getting the file list from
the current directory, since you're using a recursive file search (-r)
anyway.

  Also, a few minor superfluous things: -l implies -H, so you don't
need to specify both.  And you don't need to quote "zip", since "zip"
does not contain any shell meta-characters.

-- 
"One day I feel I'm ahead of the wheel / And the next it's rolling over me"
                                                  -- Rush, "Far Cry"


More information about the gnhlug-discuss mailing list