tags:

views:

158

answers:

2

I have some search patterns that fits poorly as part of a file name for the result. I therefore split the regular expression and the corresponding file name part into two different variables like below. How can I automate the following so that I do not have to manually list files for ALL and also do not have to manually enter the rules running grep?

SEARCH_RE   = test  a/b/c  a.*b
SEARCH_FILE = test  abc    ab

ALL = result.test result.abc result.ab

all: $(ALL)

result.test:
        grep test  inputfile > result.test

result.abc:
        grep a/b/c inputfile > result.abc

result.ab
        grep a.*b  inputfile > result.ab
+1  A: 

I don't know how to create the rules, but the ALL target is easy enough:

ALL = $(patsubst %,result.%,$(SEARCH_FILE))
Douglas Leeder
Good. I also found that "ALL = $(foreach file,$(SEARCH_FILE),result.$(file))" works.
hlovdal
+1  A: 

I recommend

ALL = $(addprefix result.,$(SEARCH_FILE))

As for writing the rules, the kind of lookup I think you want can be done in Make, but really shouldn't be-- it would be a horrible kludge. I'd suggest doing it this way:

result.test: TARG = test
result.abc: TARG = a/b/c
result.ab: TARG = a.*b

$(ALL):
    grep $(TARG) inputfile > $@
Beta
While not as fully automated as I hoped for, this is at least much better than having duplicated rules. Thank you.
hlovdal