Alex Ivanov
Alex Ivanov

Reputation: 473

How do I extract value from a text file in Makefile?

Here's the code snippet from my Makefile:

%/foo: %/bar.yaml
    $(BINARY) generate -g go \
        --package-name {COOL_VALUE}
# COOL_VALUE should be the value for a unique `x-cool-value` key from `bar.yaml`.

E.g., here's bar.yaml:

abc:
  xyz: Hi StackOverflow!
  x-cool-value: v1

and I'd like to extract v1 value.

E.g.,

make foo1/foo2/foo
> ./binary generate -g go \
   --package-name v1
# since foo1/foo2/foo/bar.yaml had a `v1` value for `abc.x-cool-value` key.

Upvotes: 0

Views: 588

Answers (1)

MadScientist
MadScientist

Reputation: 100846

You can't do things like that using makefile syntax (at least it's not worth it). Figure out how to do it using a shell command, then put that shell command in your makefile recipe.

For example:

$ sed -n 's/^ *x-cool-value: *//p' foo1/foo2/foo/bar.yaml
v1

So, in your makefile:

%/foo: %/bar.yaml
        $(BINARY) generate -g go \
            --package-name $$(sed -n 's/^ *x-cool-value: *//p' $<)

Or you can try to find some command-line tool that parses YAML and lets you query things; there probably is one.

Upvotes: 1

Related Questions