-1

I have a command that I execute that gives me a list of relative paths to files under a directory. Think in terms of using find my_dir, although the command itself is different.

For each file in the list, I want to pipe it into a command that will filter the list down depending on contents of the file itself. I'm looking for semantics identical or similar to egrep -v but it needs to open & read the file to run the regex on the file contents instead of the path itself.

How can I accomplish this using a simple piped command stream? Example:

find some_dir/ | egrep_contents -v 'Exclude pattern' | xargs -n1 do_something_with_filtered_files

In this case, egrep_contents is ideally the command I'm asking for in this question. It would run the specified exclude pattern on each line in the file until it finds a match. If a match is found, it prevents that file path from being passed down to the next piped command.

For context, I'm running these commands in Git Bash included with Git for Windows (MINGW).

1

1 Answer 1

0

The grep option --files-without-match or -L may be what you're looking for. It tells grep to print out file paths whose corresponding file content does not match the given pattern. For example,

$ echo "bar" > foo
$ echo "quux" > baz

$ ls
baz foo

$ find . -type f | xargs -n1 grep -EL bar | xargs -n1 cat
quux

Here, "quux" is printed because it's the result of cat being run on the files whose contents do not contain "bar".

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .