Posted By

michaelaskew on 01/07/10


Tagged

search replace space whitespace filter substitute


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

michaelaskew


Search For or Replace Spaces


 / Published in: Makefile
 

URL: http://braindumpery.com

Make (and GMake) ignore spaces in many constructs, making it difficult to use a single space as a search string or a replacement string, for example. This trick works around the problem by assigning the space to a variable named SPACE and using that instead. So given this makefile:

NOTHING:=
SPACE:=$(NOTHING) $(NOTHING)
NAME_WITH_UNDERSCORES:=$(subst $(SPACE),_,$(NAME))
print : ; @echo $(NAME_WITH_UNDERSCORES)

The command

gmake NAME="Professor Hubert Farnsworth"

would print

Professor_Hubert_Farnsworth
  1. NOTHING:=
  2. SPACE:=$(NOTHING) $(NOTHING)

Report this snippet  

You need to login to post a comment.