3

I hava a Makefile which needs to extract some information from a textfile `someText.txt by using awk. Here is an example:

Executing awk on my textfile manually in the console works pretty fine:

$ cat someText.txt | awk '/Total number:/ {print $NF}'
$ 42

But using it in my makefile does not work:

MYVAR=$(shell cat someText.txt | awk '/Total number:/ {print $NF}')
all:
    @echo Count: $(MYVAR)

The output is: Count:

But I would suggest the output is Count: 42

eDeviser
  • 1,605
  • 2
  • 17
  • 44
  • Possible duplicate of [How do I properly escape data for a Makefile?](http://stackoverflow.com/questions/7654386/how-do-i-properly-escape-data-for-a-makefile) – tripleee Mar 03 '17 at 06:52
  • 2
    As an aside, you also want to avoid the [useless use of `cat`](http://www.iki.fi/era/unix/award.html) – tripleee Mar 03 '17 at 06:53
  • 4
    TL;DR you need to double every `$$` which should be passed through to the shell. – tripleee Mar 03 '17 at 06:54
  • 1
    triple is alluding to this... `awk '/Total number:/ {print $NF}' someText.txt` – Mark Setchell Mar 03 '17 at 08:09
  • 2
    Yes, thats it. I overlooked the dollar within the curly brackets. Using `{print $$NF}` works pretty fine. – eDeviser Mar 03 '17 at 08:36

1 Answers1

5

MYVAR=$(shell cat someText.txt | awk '/Total number:/ {print $$NF}')

Daniel Casserly
  • 3,552
  • 2
  • 29
  • 60
田咖啡
  • 728
  • 8
  • 10