Help us understand the problem. What is going on with this article?

Makeのsubstで半角スペースを除去する

More than 1 year has passed since last update.

半角スペースを除去する方法が分からなかったので調べた。

参考

http://blog.jgc.org/2007/06/escaping-comma-and-space-in-gnu-make.html

結論

FOO = 1 2  3
BAR = $(subst $() ,,$(FOO))  # BAR = 123

解説

  • 普通のsubstの使い方はこう。
FOO = 1a2aa3
BAR = $(subst a,x,$(FOO))  # 1x2xx3
  • 2文字目以降の半角スペースは同じように置換できる。
FOO = 1a 2a a 3
BAR = $(subst a ,x,$(FOO))  # 1x2xx3
  • でも先頭が半角スペースだと、トークンを区切る半角スペースと区別できない。
FOO = 1 a2 a3
BAR = $(subst  a,x,$(FOO))  # 1 x2 x3
  • なので、空の文字列が入った変数を先頭においてやる。
EMPTY =

FOO = 1 a2 a a3
BAR = $(subst $(EMPTY) a,x,$(FOO))  # 1x2xx3
  • $()に値を与えるのは(おそらく)不可能なので、常に空文字列が入っていることになる。これを上記の$(EMPTY)として使う。
FOO = 1 a2 a a3
BAR = $(subst $() a,x,$(FOO))  # 1x2xx3
Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away