Copying lines from one file to another using sed and shell variablesUsing sed to replace a string with...

Billiard balls collision

Do you pay one or two mana to bounce a transformed Delver of Secrets with Repeal?

Why is strlen so complex in C?

Talk interpreter

Is a memoized pure function itself considered pure?

How does the OS tell whether an "Address is already in use"?

Contours of a national emergency in the United States

How long do you think advanced cybernetic implants would plausibly last?

Set orthographic view using python?

How to prevent a hosting company from accessing a VM's encryption keys?

Why is a statement like 1 + n *= 3 allowed in Ruby?

Changing JPEG to RAW to use on Lightroom?

How to say "I only speak one which is English." in French?

What is the best way to solve this 6x6 sudoku?

74S vs 74LS ICs

Why is "dyadic" the only word with the prefix "dy-"?

Why does matter stays collapsed following the supernova explosion?

Breaker Mapping Questions

Given current technology, could TV display screens double as video camera sensors?

Why does Windows store Wi-Fi passwords in a reversible format?

Is it unusual for a math department not to have a mail/web server?

How does Mercy remove conditions?

Which old Technic set included large yellow motor?

Can MuseScore be used programmatically?



Copying lines from one file to another using sed and shell variables


Using sed to replace a string with special chars with another string with special charactersCutting logs (and not copying) from a log file between two time stampsShell Script to insert lines in between filePerforming Prime Key function using 'Sed' in BashDelete all lines that contain duplicate lettersFind words after specific symbol on linesed - calling a variable from a file with multilineReplace a pattern in File1 and replace it with corresponding matched pattern + column in File2Fixing a .csv file where some rows have missing columnsHow can I use sed to insert some text after a multiline match?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







0















I've been trying to figure out how to copy lines from one file to the end of another using sed with some shell variables.



sed -n "$var1,$var2'p'" file1.txt > file2.txt



I've tried that and many close variations but no cigar. Could someone please enlighten me about the correct syntax? Thanks.










share|improve this question







New contributor



Crush is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

















  • 1





    What error do you receive when running the sed command in your question? Please edit your post and provide that information.

    – Peschke
    1 hour ago






  • 1





    > file2.txt will overwrite file2: if you want to append the copied lines to the end of the file, use >> file2.txt

    – steeldriver
    1 hour ago


















0















I've been trying to figure out how to copy lines from one file to the end of another using sed with some shell variables.



sed -n "$var1,$var2'p'" file1.txt > file2.txt



I've tried that and many close variations but no cigar. Could someone please enlighten me about the correct syntax? Thanks.










share|improve this question







New contributor



Crush is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

















  • 1





    What error do you receive when running the sed command in your question? Please edit your post and provide that information.

    – Peschke
    1 hour ago






  • 1





    > file2.txt will overwrite file2: if you want to append the copied lines to the end of the file, use >> file2.txt

    – steeldriver
    1 hour ago














0












0








0








I've been trying to figure out how to copy lines from one file to the end of another using sed with some shell variables.



sed -n "$var1,$var2'p'" file1.txt > file2.txt



I've tried that and many close variations but no cigar. Could someone please enlighten me about the correct syntax? Thanks.










share|improve this question







New contributor



Crush is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I've been trying to figure out how to copy lines from one file to the end of another using sed with some shell variables.



sed -n "$var1,$var2'p'" file1.txt > file2.txt



I've tried that and many close variations but no cigar. Could someone please enlighten me about the correct syntax? Thanks.







shell sed variable-substitution






share|improve this question







New contributor



Crush is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.










share|improve this question







New contributor



Crush is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








share|improve this question




share|improve this question






New contributor



Crush is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








asked 1 hour ago









CrushCrush

1




1




New contributor



Crush is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




New contributor




Crush is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.













  • 1





    What error do you receive when running the sed command in your question? Please edit your post and provide that information.

    – Peschke
    1 hour ago






  • 1





    > file2.txt will overwrite file2: if you want to append the copied lines to the end of the file, use >> file2.txt

    – steeldriver
    1 hour ago














  • 1





    What error do you receive when running the sed command in your question? Please edit your post and provide that information.

    – Peschke
    1 hour ago






  • 1





    > file2.txt will overwrite file2: if you want to append the copied lines to the end of the file, use >> file2.txt

    – steeldriver
    1 hour ago








1




1





What error do you receive when running the sed command in your question? Please edit your post and provide that information.

– Peschke
1 hour ago





What error do you receive when running the sed command in your question? Please edit your post and provide that information.

– Peschke
1 hour ago




1




1





> file2.txt will overwrite file2: if you want to append the copied lines to the end of the file, use >> file2.txt

– steeldriver
1 hour ago





> file2.txt will overwrite file2: if you want to append the copied lines to the end of the file, use >> file2.txt

– steeldriver
1 hour ago










2 Answers
2






active

oldest

votes


















0















Use curly braces around your variable names:



sed -n "${var1},${var2}p" file1.txt > file2.txt


The braces around the first variable are not needed, but better make it consistent.






share|improve this answer

































    0















    Freddy's answer works, but lacks an explanation. The sed format that you are probably aiming for is something like



    sed -n 4,6p file1.txt


    Where I've used 4 and 6 as dummy numbers.



    Let's examine your expression, using these dummy numbers again, and the echo command.



    $ var1=4
    $ var2=6
    $ echo "$var1,$var2'p'"
    4,6'p'


    Because you enclosed the 's in the "s, they are interpreted literally, and you are literal 's to the sed command. Hence, when you try to run your command:



    $ sed -n "$var1,$var2'p'" file1.txt
    sed: -e expression #1, char 4: unknown command: `''


    Solution



    You can remove the 's from your expression, but this would change the variable name to $var2p instead. Hence, as Freddy suggests, you can use {…} to explicitly enclose the variable name(s). i.e.



    sed -n "${var1},${var2}p" file1.txt


    You can test if this works by putting an echo before the command. An alternative format is



    sed -n "$var1,$var2"p file1.txt


    but IMO it's less readable.



    Finally, as steeldriver points out, if you are aiming to append to the file, use >> file2.txt instead of > file2.txt.





    share




























      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
      });


      }
      });






      Crush is a new contributor. Be nice, and check out our Code of Conduct.










      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f537555%2fcopying-lines-from-one-file-to-another-using-sed-and-shell-variables%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









      0















      Use curly braces around your variable names:



      sed -n "${var1},${var2}p" file1.txt > file2.txt


      The braces around the first variable are not needed, but better make it consistent.






      share|improve this answer






























        0















        Use curly braces around your variable names:



        sed -n "${var1},${var2}p" file1.txt > file2.txt


        The braces around the first variable are not needed, but better make it consistent.






        share|improve this answer




























          0














          0










          0









          Use curly braces around your variable names:



          sed -n "${var1},${var2}p" file1.txt > file2.txt


          The braces around the first variable are not needed, but better make it consistent.






          share|improve this answer













          Use curly braces around your variable names:



          sed -n "${var1},${var2}p" file1.txt > file2.txt


          The braces around the first variable are not needed, but better make it consistent.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 1 hour ago









          FreddyFreddy

          6,8481 gold badge6 silver badges24 bronze badges




          6,8481 gold badge6 silver badges24 bronze badges




























              0















              Freddy's answer works, but lacks an explanation. The sed format that you are probably aiming for is something like



              sed -n 4,6p file1.txt


              Where I've used 4 and 6 as dummy numbers.



              Let's examine your expression, using these dummy numbers again, and the echo command.



              $ var1=4
              $ var2=6
              $ echo "$var1,$var2'p'"
              4,6'p'


              Because you enclosed the 's in the "s, they are interpreted literally, and you are literal 's to the sed command. Hence, when you try to run your command:



              $ sed -n "$var1,$var2'p'" file1.txt
              sed: -e expression #1, char 4: unknown command: `''


              Solution



              You can remove the 's from your expression, but this would change the variable name to $var2p instead. Hence, as Freddy suggests, you can use {…} to explicitly enclose the variable name(s). i.e.



              sed -n "${var1},${var2}p" file1.txt


              You can test if this works by putting an echo before the command. An alternative format is



              sed -n "$var1,$var2"p file1.txt


              but IMO it's less readable.



              Finally, as steeldriver points out, if you are aiming to append to the file, use >> file2.txt instead of > file2.txt.





              share






























                0















                Freddy's answer works, but lacks an explanation. The sed format that you are probably aiming for is something like



                sed -n 4,6p file1.txt


                Where I've used 4 and 6 as dummy numbers.



                Let's examine your expression, using these dummy numbers again, and the echo command.



                $ var1=4
                $ var2=6
                $ echo "$var1,$var2'p'"
                4,6'p'


                Because you enclosed the 's in the "s, they are interpreted literally, and you are literal 's to the sed command. Hence, when you try to run your command:



                $ sed -n "$var1,$var2'p'" file1.txt
                sed: -e expression #1, char 4: unknown command: `''


                Solution



                You can remove the 's from your expression, but this would change the variable name to $var2p instead. Hence, as Freddy suggests, you can use {…} to explicitly enclose the variable name(s). i.e.



                sed -n "${var1},${var2}p" file1.txt


                You can test if this works by putting an echo before the command. An alternative format is



                sed -n "$var1,$var2"p file1.txt


                but IMO it's less readable.



                Finally, as steeldriver points out, if you are aiming to append to the file, use >> file2.txt instead of > file2.txt.





                share




























                  0














                  0










                  0









                  Freddy's answer works, but lacks an explanation. The sed format that you are probably aiming for is something like



                  sed -n 4,6p file1.txt


                  Where I've used 4 and 6 as dummy numbers.



                  Let's examine your expression, using these dummy numbers again, and the echo command.



                  $ var1=4
                  $ var2=6
                  $ echo "$var1,$var2'p'"
                  4,6'p'


                  Because you enclosed the 's in the "s, they are interpreted literally, and you are literal 's to the sed command. Hence, when you try to run your command:



                  $ sed -n "$var1,$var2'p'" file1.txt
                  sed: -e expression #1, char 4: unknown command: `''


                  Solution



                  You can remove the 's from your expression, but this would change the variable name to $var2p instead. Hence, as Freddy suggests, you can use {…} to explicitly enclose the variable name(s). i.e.



                  sed -n "${var1},${var2}p" file1.txt


                  You can test if this works by putting an echo before the command. An alternative format is



                  sed -n "$var1,$var2"p file1.txt


                  but IMO it's less readable.



                  Finally, as steeldriver points out, if you are aiming to append to the file, use >> file2.txt instead of > file2.txt.





                  share













                  Freddy's answer works, but lacks an explanation. The sed format that you are probably aiming for is something like



                  sed -n 4,6p file1.txt


                  Where I've used 4 and 6 as dummy numbers.



                  Let's examine your expression, using these dummy numbers again, and the echo command.



                  $ var1=4
                  $ var2=6
                  $ echo "$var1,$var2'p'"
                  4,6'p'


                  Because you enclosed the 's in the "s, they are interpreted literally, and you are literal 's to the sed command. Hence, when you try to run your command:



                  $ sed -n "$var1,$var2'p'" file1.txt
                  sed: -e expression #1, char 4: unknown command: `''


                  Solution



                  You can remove the 's from your expression, but this would change the variable name to $var2p instead. Hence, as Freddy suggests, you can use {…} to explicitly enclose the variable name(s). i.e.



                  sed -n "${var1},${var2}p" file1.txt


                  You can test if this works by putting an echo before the command. An alternative format is



                  sed -n "$var1,$var2"p file1.txt


                  but IMO it's less readable.



                  Finally, as steeldriver points out, if you are aiming to append to the file, use >> file2.txt instead of > file2.txt.






                  share











                  share


                  share










                  answered 8 mins ago









                  SparhawkSparhawk

                  11.3k8 gold badges53 silver badges107 bronze badges




                  11.3k8 gold badges53 silver badges107 bronze badges

























                      Crush is a new contributor. Be nice, and check out our Code of Conduct.










                      draft saved

                      draft discarded


















                      Crush is a new contributor. Be nice, and check out our Code of Conduct.













                      Crush is a new contributor. Be nice, and check out our Code of Conduct.












                      Crush is a new contributor. Be nice, and check out our Code of Conduct.
















                      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%2f537555%2fcopying-lines-from-one-file-to-another-using-sed-and-shell-variables%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

                      Taj Mahal Inhaltsverzeichnis Aufbau | Geschichte | 350-Jahr-Feier | Heutige Bedeutung | Siehe auch |...

                      Baia Sprie Cuprins Etimologie | Istorie | Demografie | Politică și administrație | Arii naturale...

                      Nicolae Petrescu-Găină Cuprins Biografie | Opera | In memoriam | Varia | Controverse, incertitudini...