[Makefile] String Replacement Example


First example: replace . with -
i.e.,
mn.004.contrast-reading.html => mn-004-contrast-reading-html


Second example: replace ending .html with %zh.rst
i.e.,
mn.004.contrast-reading.html => mn.004.contrast-reading%zh.rst

Source code:

Makefile | repository | view raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
FILENAME:= mn.004.contrast-reading.html

DOT:= .
DASH:= -
# replace . with -
RESULT= $(subst $(DOT),$(DASH),$(FILENAME))

HTML_SUFFIX:= .html
RST_SUFFIX:= \%zh.rst
# replace .html with %zh.rst
RESULT2= $(subst $(HTML_SUFFIX),$(RST_SUFFIX),$(FILENAME))


default:
	@echo $(RESULT)
	@echo $(RESULT2)

Output:

mn-004-contrast-reading-html
mn.004.contrast-reading%zh.rst

Tested on Ubuntu Linux 15.10, GNU make 4.0-8.2.


References:

[1]

shell replace character in string - Google search

shell replace character in string - DuckDuckGo search

shell replace character in string - Bing search

shell replace character in string - Yahoo search

shell replace character in string - Baidu search

shell replace character in string - Yandex search

[2]

makefile replace string in variable - Google search

makefile replace string in variable - DuckDuckGo search

makefile replace string in variable - Bing search

makefile replace string in variable - Yahoo search

makefile replace string in variable - Baidu search

makefile replace string in variable - Yandex search

GNU make - Functions for Transforming Text

[3]makefile file name manipulation · twnanda/twnanda@0323a19 · GitHub