30

I'm trying to filter out strings that contain a particular character, but it doesn't work. I guess make does not support multiple % patterns?

.PHONY: test
test:
    echo $(filter-out %g%, seven eight nine ten)

Gives:

$ make test
echo seven eight nine ten
seven eight nine ten

It doesn't filter out "eight"? Actually what I want to do is filter out from a list of filenames those containing "$". (In a Java context.)

Any hope, or do I have to use $(shell)?

Thanks.

Steve
  • 8,153
  • 9
  • 44
  • 91
  • I can't see any way of doing it in make itself. –  May 26 '11 at 21:16
  • 5
    As the documentation says, only the first `%` character is a wildcard -- subsequent `%` characters match literal % characters in whatever you are matching. So your command filters out names that end in `g%` – Chris Dodd May 26 '11 at 22:26

2 Answers2

44

Does the following function meet the purpose?

FILTER_OUT = $(foreach v,$(2),$(if $(findstring $(1),$(v)),,$(v)))
$(call FILTER_OUT,g, seven eight nine ten)
Ise Wisteria
  • 11,259
  • 2
  • 43
  • 26
17

I recently did something similar using two wildcards functions for excluding some files

.PHONY: test
test:
    echo $(filter-out $(wildcard *g*.c),$(wildcard *.c))
bltavares
  • 319
  • 2
  • 6
  • 1
    Doesn't `wildcard` just find files in your current directory, though? The other solution is a bit more robust, I think. – Keith M Nov 06 '19 at 02:56
  • @keith-m the following will work with any directory. It also ensures the excluded file pattern applies only to the file name and not on the folder path itself. ```$(filter-out $(wildcard my/folder/*g*.c),$(wildcard my/folder/*.c))``` – Lila Viollette Mar 12 '21 at 16:23
  • 1
    @RomainVIOLLETTE What I meant is that even with your snippet, the files must all be in the same folder. No? – Keith M Jun 25 '21 at 01:59