tyrande
tyrande

Reputation: 27

Exclude wildcard containing string 'x'

Is there a way to modify egrep -rha 'part1.*part2' to add something like: "if .* contains (not necessarily equals) string_x then pattern is not a match"? The problem is that string_x is present in every line so I can't -v it. It's okay to have this string before or after pattern, just not in the middle of it.

I'm assuming double .* with not string_x between them will get the job done, but it'll take a lot of time, plus I sometimes use .{n,m} wildcard, and in this case it would double the desired wildcard length. Maybe some sort of search termination every time it encounters string_x before part2?

Upvotes: 0

Views: 715

Answers (1)

Ed Morton
Ed Morton

Reputation: 203712

Forget you ever heard about -r or any other option to let grep find files. There's a perfectly good tool for finding files with an extremely obvious name - find. Keep grep for what it's good at which is doing g/re/p. I can't imagine what the GNU guys were smoking when they decided to give grep options to find files, but hopefully they aren't now plotting to add options to sort files or pull content from web sites or print process data or do anything else that existing tools do perfectly well!

In this case you're looking for more than just g/re/p though so you should use awk:

awk '/part1.*part2/ && !/part1.*string_x.*part2/'

So the full script would be something like (untested since no sample input/output provided):

find . -type f -exec awk '/part1.*part2/ && !/part1.*string_x.*part2/' {} +

Upvotes: 2

Related Questions