1

In a Makefile, I can get the full path string by $(CURDIR). The result is like /home/jones/prj/platform/Application_UBUNTU/build_os. How do I extract the UBUNTU from the string?

I use subst to replace '/' as space.

DIR = $(subst /, " ", $(CURDIR))

I get result as home jones prj platform Application_UBUNTU build_os.

Then I try to use filter command but I cannot use % or wildcard to match Application_UBUNTU out. Thanks for help in advance.

Jones
  • 307
  • 1
  • 4
  • 11

1 Answers1

1

Use the penultimateword macro from my answer here.

penultimateword = $(wordlist $(words $1),$(words $1), x $1)

BUILD_OS=$(call penultimateword,$(subst /, ,$(CURDIR)))
BUILD_OS=$(subst _, ,$(BUILD_OS))
BUILD_OS=$(word 2,$(BUILD_OS))

This is obviously sensitive to extra underscores in the path/etc.

Community
  • 1
  • 1
Etan Reisner
  • 77,877
  • 8
  • 106
  • 148
  • It's amazing and works well. However, I don't know how it works. If I want to extract the second word with underscore '_', in my example, it's `os` from the `build_os`, where should I modify? Thanks. – Jones Dec 02 '14 at 04:14
  • 1
    I got it. I can use the `lastword` macro to get `build_os` part. `lastword = $(word $(words $1), $1)` – Jones Dec 02 '14 at 04:20