Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

One Makefile target is supposed to be in charge of:

  • grabbing the output of $ perl Markdown.pl src/index.md (markdown to html)
  • using that string to replace one line, something like CONTENT originating in the file src/template.html. There's no other line with that row in template.html.

What's an elegant way to achieve this?

share|improve this question

2 Answers 2

up vote 3 down vote accepted

Like arcege said, don't modify the actual source file. Anyway, this is much easier with awk:

target:
    awk '{if($$0=="CONTENT"){system("perl Markdown.pl src/index.md")}else{print}}' src/template.html > build/template.html

You can put that awk code into a script if you want to make the line look a bit cleaner. (If you do, change $$0 to $0, it's necessary because otherwise the makefile interprets it)

target:
    awk -f convert.awk src/template.html > build/template.html
share|improve this answer
    
Perfect. Cheers –  chelmertz Jan 29 '12 at 20:35

One rule is that you do not modify source files in a build, so no in-place modifications. Use a 'build' directory or something similar.

template_markdown:
    @mkdir -p build
    cp -p src/template.html build
    (echo /CONTENT/c; perl Markdown.pl src/index.md; echo .; echo w) | ed - build/template.html

This should take care of any situation where perl Markdown.pl does not emit a period ('.') by itself.

share|improve this answer
    
I think the escaping is off: make template_markdown outputs sed "/CONTENT/s/.*//" src/template.html > build/template.html and build/template.html is the result of that sed: the row containing "CONTENT" is now just \n. –  chelmertz Jan 29 '12 at 19:43
    
Yup, I forgot the 'shell' directive in the variable substitution. Edited the answer. –  Arcege Jan 29 '12 at 19:53
    
I've SHELL := /bin/bash on top, if that matters. Output from second tip: $ make template_markdown sed "/CONTENT/s/.*/perl src/Markdown_1.0.1/Markdown.pl src/index.md/" src/template.html > build/template.html sed: 1: "/CONTENT/s/.*/<h1>Proje ...": bad flag in substitute command: 'h' make: *** [template_markdown] Error 1 $ make --v GNU Make 3.81 Copyright (C) 2006 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. This program built for i386-apple-darwin11.0 –  chelmertz Jan 29 '12 at 19:56
    
Thanks for the tip but it didn't quite work out for me. Cheers. –  chelmertz Jan 29 '12 at 20:35
    
First problem is that your output from perl seems more complex than just replacing a line with text. The second is that it looks like it may be multiple lines, which sed doesn't do well as well as say, ed. Editing again to be a little more generic. –  Arcege Jan 30 '12 at 1:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.