np20
np20

Reputation: 2095

Using sed in makefile

I am trying to use sed in makefile as shown below. But it doesn't seem to produce the modified file. I have tried the sed command in the shell and made sure it works.

ana:
    -for ana1 in $(anas) ; do \
        for ana2 in $(anas) ; do \
            sed "s/STF1/$$ana1/g" ./planalysis/src/analysis.arr > ./planalysis/src/spanalysis.arr ; \
            sed "s/STF2/$$ana2/g" ./planalysis/src/spanalysis.arr > ./planalysis/src/spanalysis.arr ; \
            # ... perform some analysis with the modified file..
            # ...
        done \
    done

Is there something I'm doing wrong?

Upvotes: 0

Views: 2242

Answers (1)

rici
rici

Reputation: 241741

This command won't do whst you expect:

sed "s/STF2/$$ana2/g" ./planalysis/src/spanalysis.arr > ./planalysis/src/spanalysis.arr ;

regardless of whether you execute it in a Makefile or in your shell.

If you execute

some-command my.file > my.file

You are likely to end up with an empty my.file, regardless of the command, because redirections are set up before the command executes. As soon as the shell does the redirection > my.file, that file is emptied, because that's what output redirection does. When the command eventually executes and attempts to read my.file, it will find thar the file is empty. In the case of sed, and many other commands, an empty input produces an empty output, and I suppse that is what you are seeing.

Use a temporary file, or use sed -i (see man sed) or, as suggested by @AlexeySemenyuk in a comment, combine the edits into a single sed invocation:

sed -e "s/STF1/$$ana1/g" -e "s/STF2/$$ana2/g" \
    ./planalysis/src/analysis.arr > ./planalysis/src/spanalysis.arr

Upvotes: 3

Related Questions