write variable into specific line with sed command

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












0















echo -n "##### STATIONS? #######"
read station
text="stab_site "


I need to write echo $text$station into the 27th line of text file. I used below command but it didn't work,



sed -i "27i$text$station" text.data


it produce; $textankr ista "text" variable cannot be passed within sed function.










share|improve this question


























    0















    echo -n "##### STATIONS? #######"
    read station
    text="stab_site "


    I need to write echo $text$station into the 27th line of text file. I used below command but it didn't work,



    sed -i "27i$text$station" text.data


    it produce; $textankr ista "text" variable cannot be passed within sed function.










    share|improve this question
























      0












      0








      0








      echo -n "##### STATIONS? #######"
      read station
      text="stab_site "


      I need to write echo $text$station into the 27th line of text file. I used below command but it didn't work,



      sed -i "27i$text$station" text.data


      it produce; $textankr ista "text" variable cannot be passed within sed function.










      share|improve this question














      echo -n "##### STATIONS? #######"
      read station
      text="stab_site "


      I need to write echo $text$station into the 27th line of text file. I used below command but it didn't work,



      sed -i "27i$text$station" text.data


      it produce; $textankr ista "text" variable cannot be passed within sed function.







      sed






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 13 '15 at 15:45









      deepblue_86deepblue_86

      12515




      12515




















          2 Answers
          2






          active

          oldest

          votes


















          1














          That's because your shell is interpreting $ to mean a literal (escaped) dollar sign. Try



          sed -i "27i\$text$station" text.data


          or using a mix of single- and double-quotes



          sed -i '27i'"$text$station" text.data


          or with a literal newline



          sed -i "27i
          $text$station" text.data


          (which is more POSIXly correct, I think).






          share|improve this answer























          • how can I give one space after the beginning of the written line?

            – deepblue_86
            Dec 13 '15 at 16:36











          • @deepblue_86 where exactly? You should be able to add a literal space anywhere in the replacement text e.g. ` $text$station` or $text $station or $text$station

            – steeldriver
            Dec 13 '15 at 16:42


















          0














          Maybe the problem can be a empty output file.
          Example:
          #!/bin/bash



          CAMINHO='/etc/scripts/dropbox.log'

          RESULTADO=`date +%d/%m/%Y | tr 'n' ' '; dropbox status;`

          sed -i "1i
          $RESULTADO" /etc/scripts/dropbox.log


          This script cant write to a empty file dropbox.log.



          Write some text on file and execute script again.






          share|improve this answer























          • The problem was solved with the other answer; why would it be caused by an empty output file?

            – Stephen Kitt
            Jan 14 at 13:39










          Your Answer








          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "106"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f249095%2fwrite-variable-into-specific-line-with-sed-command%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          That's because your shell is interpreting $ to mean a literal (escaped) dollar sign. Try



          sed -i "27i\$text$station" text.data


          or using a mix of single- and double-quotes



          sed -i '27i'"$text$station" text.data


          or with a literal newline



          sed -i "27i
          $text$station" text.data


          (which is more POSIXly correct, I think).






          share|improve this answer























          • how can I give one space after the beginning of the written line?

            – deepblue_86
            Dec 13 '15 at 16:36











          • @deepblue_86 where exactly? You should be able to add a literal space anywhere in the replacement text e.g. ` $text$station` or $text $station or $text$station

            – steeldriver
            Dec 13 '15 at 16:42















          1














          That's because your shell is interpreting $ to mean a literal (escaped) dollar sign. Try



          sed -i "27i\$text$station" text.data


          or using a mix of single- and double-quotes



          sed -i '27i'"$text$station" text.data


          or with a literal newline



          sed -i "27i
          $text$station" text.data


          (which is more POSIXly correct, I think).






          share|improve this answer























          • how can I give one space after the beginning of the written line?

            – deepblue_86
            Dec 13 '15 at 16:36











          • @deepblue_86 where exactly? You should be able to add a literal space anywhere in the replacement text e.g. ` $text$station` or $text $station or $text$station

            – steeldriver
            Dec 13 '15 at 16:42













          1












          1








          1







          That's because your shell is interpreting $ to mean a literal (escaped) dollar sign. Try



          sed -i "27i\$text$station" text.data


          or using a mix of single- and double-quotes



          sed -i '27i'"$text$station" text.data


          or with a literal newline



          sed -i "27i
          $text$station" text.data


          (which is more POSIXly correct, I think).






          share|improve this answer













          That's because your shell is interpreting $ to mean a literal (escaped) dollar sign. Try



          sed -i "27i\$text$station" text.data


          or using a mix of single- and double-quotes



          sed -i '27i'"$text$station" text.data


          or with a literal newline



          sed -i "27i
          $text$station" text.data


          (which is more POSIXly correct, I think).







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 13 '15 at 16:00









          steeldriversteeldriver

          35.8k35286




          35.8k35286












          • how can I give one space after the beginning of the written line?

            – deepblue_86
            Dec 13 '15 at 16:36











          • @deepblue_86 where exactly? You should be able to add a literal space anywhere in the replacement text e.g. ` $text$station` or $text $station or $text$station

            – steeldriver
            Dec 13 '15 at 16:42

















          • how can I give one space after the beginning of the written line?

            – deepblue_86
            Dec 13 '15 at 16:36











          • @deepblue_86 where exactly? You should be able to add a literal space anywhere in the replacement text e.g. ` $text$station` or $text $station or $text$station

            – steeldriver
            Dec 13 '15 at 16:42
















          how can I give one space after the beginning of the written line?

          – deepblue_86
          Dec 13 '15 at 16:36





          how can I give one space after the beginning of the written line?

          – deepblue_86
          Dec 13 '15 at 16:36













          @deepblue_86 where exactly? You should be able to add a literal space anywhere in the replacement text e.g. ` $text$station` or $text $station or $text$station

          – steeldriver
          Dec 13 '15 at 16:42





          @deepblue_86 where exactly? You should be able to add a literal space anywhere in the replacement text e.g. ` $text$station` or $text $station or $text$station

          – steeldriver
          Dec 13 '15 at 16:42













          0














          Maybe the problem can be a empty output file.
          Example:
          #!/bin/bash



          CAMINHO='/etc/scripts/dropbox.log'

          RESULTADO=`date +%d/%m/%Y | tr 'n' ' '; dropbox status;`

          sed -i "1i
          $RESULTADO" /etc/scripts/dropbox.log


          This script cant write to a empty file dropbox.log.



          Write some text on file and execute script again.






          share|improve this answer























          • The problem was solved with the other answer; why would it be caused by an empty output file?

            – Stephen Kitt
            Jan 14 at 13:39















          0














          Maybe the problem can be a empty output file.
          Example:
          #!/bin/bash



          CAMINHO='/etc/scripts/dropbox.log'

          RESULTADO=`date +%d/%m/%Y | tr 'n' ' '; dropbox status;`

          sed -i "1i
          $RESULTADO" /etc/scripts/dropbox.log


          This script cant write to a empty file dropbox.log.



          Write some text on file and execute script again.






          share|improve this answer























          • The problem was solved with the other answer; why would it be caused by an empty output file?

            – Stephen Kitt
            Jan 14 at 13:39













          0












          0








          0







          Maybe the problem can be a empty output file.
          Example:
          #!/bin/bash



          CAMINHO='/etc/scripts/dropbox.log'

          RESULTADO=`date +%d/%m/%Y | tr 'n' ' '; dropbox status;`

          sed -i "1i
          $RESULTADO" /etc/scripts/dropbox.log


          This script cant write to a empty file dropbox.log.



          Write some text on file and execute script again.






          share|improve this answer













          Maybe the problem can be a empty output file.
          Example:
          #!/bin/bash



          CAMINHO='/etc/scripts/dropbox.log'

          RESULTADO=`date +%d/%m/%Y | tr 'n' ' '; dropbox status;`

          sed -i "1i
          $RESULTADO" /etc/scripts/dropbox.log


          This script cant write to a empty file dropbox.log.



          Write some text on file and execute script again.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 14 at 13:25









          Lucas CataniLucas Catani

          11




          11












          • The problem was solved with the other answer; why would it be caused by an empty output file?

            – Stephen Kitt
            Jan 14 at 13:39

















          • The problem was solved with the other answer; why would it be caused by an empty output file?

            – Stephen Kitt
            Jan 14 at 13:39
















          The problem was solved with the other answer; why would it be caused by an empty output file?

          – Stephen Kitt
          Jan 14 at 13:39





          The problem was solved with the other answer; why would it be caused by an empty output file?

          – Stephen Kitt
          Jan 14 at 13:39

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Unix & Linux Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f249095%2fwrite-variable-into-specific-line-with-sed-command%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown






          Popular posts from this blog

          How to check contact read email or not when send email to Individual?

          Displaying single band from multi-band raster using QGIS

          How many registers does an x86_64 CPU actually have?