How do I add numbers from two txt files with Bash?












1















I have a txt file that contains some numbers like this:



1  
2
3
4
5


And I have another txt file that contains the same number of lines, but with other numbers:



6  
7
8
9
10


I want to add them together, namely 1+6, 2+7, 3+8, etc.. How do I write the script?










share|improve this question









New contributor




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
















  • 1





    Do you really want to do this "with Bash" - or are you looking for a command line solution more generally?

    – steeldriver
    22 mins ago






  • 1





    You will get a much more friendly reception and much better help here if you show what code you have tried so far and describe what problems you were having with it. Without code, your question looks like a request for free consulting and many people don't like that.

    – John1024
    15 mins ago
















1















I have a txt file that contains some numbers like this:



1  
2
3
4
5


And I have another txt file that contains the same number of lines, but with other numbers:



6  
7
8
9
10


I want to add them together, namely 1+6, 2+7, 3+8, etc.. How do I write the script?










share|improve this question









New contributor




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
















  • 1





    Do you really want to do this "with Bash" - or are you looking for a command line solution more generally?

    – steeldriver
    22 mins ago






  • 1





    You will get a much more friendly reception and much better help here if you show what code you have tried so far and describe what problems you were having with it. Without code, your question looks like a request for free consulting and many people don't like that.

    – John1024
    15 mins ago














1












1








1








I have a txt file that contains some numbers like this:



1  
2
3
4
5


And I have another txt file that contains the same number of lines, but with other numbers:



6  
7
8
9
10


I want to add them together, namely 1+6, 2+7, 3+8, etc.. How do I write the script?










share|improve this question









New contributor




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












I have a txt file that contains some numbers like this:



1  
2
3
4
5


And I have another txt file that contains the same number of lines, but with other numbers:



6  
7
8
9
10


I want to add them together, namely 1+6, 2+7, 3+8, etc.. How do I write the script?







bash shell-script text-processing numeric-data






share|improve this question









New contributor




OhLook 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




OhLook 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








edited 5 mins ago









jimmij

31.6k873108




31.6k873108






New contributor




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









asked 26 mins ago









OhLookOhLook

1061




1061




New contributor




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





New contributor





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






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








  • 1





    Do you really want to do this "with Bash" - or are you looking for a command line solution more generally?

    – steeldriver
    22 mins ago






  • 1





    You will get a much more friendly reception and much better help here if you show what code you have tried so far and describe what problems you were having with it. Without code, your question looks like a request for free consulting and many people don't like that.

    – John1024
    15 mins ago














  • 1





    Do you really want to do this "with Bash" - or are you looking for a command line solution more generally?

    – steeldriver
    22 mins ago






  • 1





    You will get a much more friendly reception and much better help here if you show what code you have tried so far and describe what problems you were having with it. Without code, your question looks like a request for free consulting and many people don't like that.

    – John1024
    15 mins ago








1




1





Do you really want to do this "with Bash" - or are you looking for a command line solution more generally?

– steeldriver
22 mins ago





Do you really want to do this "with Bash" - or are you looking for a command line solution more generally?

– steeldriver
22 mins ago




1




1





You will get a much more friendly reception and much better help here if you show what code you have tried so far and describe what problems you were having with it. Without code, your question looks like a request for free consulting and many people don't like that.

– John1024
15 mins ago





You will get a much more friendly reception and much better help here if you show what code you have tried so far and describe what problems you were having with it. Without code, your question looks like a request for free consulting and many people don't like that.

– John1024
15 mins ago










2 Answers
2






active

oldest

votes


















1














This is basic task many tools can solve; paste + awk combo seems exceptionally handy:



$ paste file1 file2 | awk '$0=$1+$2'

7
9
11
13
15




share































    1














    Along the paste lines, but doing the math with bc:



    $ paste -d+ file1 file2 | bc
    7
    9
    11
    13
    15


    The intermediate result (before bc):



    $ paste -d+ file1 file2
    1+6
    2+7
    3+8
    4+9
    5+10




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


      }
      });






      OhLook 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%2f501486%2fhow-do-i-add-numbers-from-two-txt-files-with-bash%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














      This is basic task many tools can solve; paste + awk combo seems exceptionally handy:



      $ paste file1 file2 | awk '$0=$1+$2'

      7
      9
      11
      13
      15




      share




























        1














        This is basic task many tools can solve; paste + awk combo seems exceptionally handy:



        $ paste file1 file2 | awk '$0=$1+$2'

        7
        9
        11
        13
        15




        share


























          1












          1








          1







          This is basic task many tools can solve; paste + awk combo seems exceptionally handy:



          $ paste file1 file2 | awk '$0=$1+$2'

          7
          9
          11
          13
          15




          share













          This is basic task many tools can solve; paste + awk combo seems exceptionally handy:



          $ paste file1 file2 | awk '$0=$1+$2'

          7
          9
          11
          13
          15





          share











          share


          share










          answered 7 mins ago









          jimmijjimmij

          31.6k873108




          31.6k873108

























              1














              Along the paste lines, but doing the math with bc:



              $ paste -d+ file1 file2 | bc
              7
              9
              11
              13
              15


              The intermediate result (before bc):



              $ paste -d+ file1 file2
              1+6
              2+7
              3+8
              4+9
              5+10




              share




























                1














                Along the paste lines, but doing the math with bc:



                $ paste -d+ file1 file2 | bc
                7
                9
                11
                13
                15


                The intermediate result (before bc):



                $ paste -d+ file1 file2
                1+6
                2+7
                3+8
                4+9
                5+10




                share


























                  1












                  1








                  1







                  Along the paste lines, but doing the math with bc:



                  $ paste -d+ file1 file2 | bc
                  7
                  9
                  11
                  13
                  15


                  The intermediate result (before bc):



                  $ paste -d+ file1 file2
                  1+6
                  2+7
                  3+8
                  4+9
                  5+10




                  share













                  Along the paste lines, but doing the math with bc:



                  $ paste -d+ file1 file2 | bc
                  7
                  9
                  11
                  13
                  15


                  The intermediate result (before bc):



                  $ paste -d+ file1 file2
                  1+6
                  2+7
                  3+8
                  4+9
                  5+10





                  share











                  share


                  share










                  answered 4 mins ago









                  Jeff SchallerJeff Schaller

                  41.5k1056132




                  41.5k1056132






















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










                      draft saved

                      draft discarded


















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













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












                      OhLook 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%2f501486%2fhow-do-i-add-numbers-from-two-txt-files-with-bash%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

                      CARDNET

                      Boot-repair Failure: Unable to locate package grub-common:i386

                      濃尾地震