a question about GREP

Bill Freeman f at ke1g.mv.com
Sat Mar 24 09:34:24 EDT 2007


Ben Scott writes:
 > grep -lwir --include=\*out\* . > zip.txt

Close.  You've left out what he's searching for:

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

Of course, this doesn't have the subtilty that Steven W. Orr addedd to
limit it to text files, as Jerry mentioned, but didn't seem to be
trying to do.

Note that all the versions using "find" and "xargs" have issues in
modern times when it is highly likely that there are files whose names
include spaces.  "find"'s "-print0" option combined with "xargs"'s
"-0" (zero, not a capitol letter) option take care of this.

Steven's "awk" script would need further work to extract filenames
containing spaces.  Or, you can use "find"'s "-exec":

   find . -name '*out*' -exec file '{}' '|' grep -q ASCII ';' -print0 \
     | xargs -0 grep -wli zip > zip.txt

[ -exec treats everything up to the next simicolon (which must be
quoted so that the shell will pass it to find) as a command (pipe) to
run, except that an argument consisting of a matched pair of curly
braces (which must be quoted agains shell interpretation) is replaced,
in the command run, by the name of the file under consideration.
Things in the command, like the pipe symbol, that are special to the
shell, must be quoted.  ("find" never sees the quotes, so they're not
quoted in the sub-shell running the command.)  If the command fails
(returns non-zero status) -exec moves on to the next filename, so this
one doesn't get printed. ]

Bill



More information about the gnhlug-discuss mailing list