1

I have a $template file that will invoke sed to do some templating. after this is done I want to redirect the new $template file to another directory named output and the new file name will be newtemplate.
portion of my shell script:

        sed -r -i.bak -e "{
            # templating done here.
        }" $template > $data/$newtemplate

this successful creates a file $newtemplate to the $output directory but it doesn't invoke sed or does any templating.
So in short, how do I use sed on the $template file and then take the templated file and redirect it to output directory under the new name newtemplate

bull
  • 61
  • 2
    Why are you using the -i option if you want to create another file, with a different name? –  Oct 25 '20 at 17:11
  • what should I use? still new to this – bull Oct 25 '20 at 17:22
  • 3
    People: this is a perfectly clear question where the OP has shown what they are trying to do. Yes, it's a simple error, but we were all newbies once. There's no reason to downvote just because the mistake was trivial. Nothing is trivial when you're learning! – terdon Oct 25 '20 at 17:50

1 Answers1

3

The -i option of sed means it won't produce any output. Instead, it will modify the original file in place. So your command is working fine, it is doing whatever #templating is, but it is doing it to $template, there is no output. Since there is no output, > $data/$newtemplate is just creating an empty file.

If you don't want to affect the original $template file and, instead, you want the output of the sed command to be printed to standard output and, therefore, into the file you redirect standard output to with > $data/$newtemplate, then you need to remove the -i option:

sed -r -e "{
            # templating done here.
        }" "$template" > "$data/$newtemplate"

(you also should always quote your variables as I did above)

terdon
  • 242,166