Subtracting numbers from adjacent columns and successive rows using awk The Next CEO of Stack Overflowtext processing-perlawk manipulationAvoid conversion to scientific notation in awkBash to join columns from multiple filesUsing AWK to calculate mean and variance of columnsHow extract values which are less than 0 and dot from specific columns and print the entire row using awktext processing - Extracting using cshell and awkSubtracting same column between two rows in awkSwitch columns in CSV using awk?Looping through columns and rows to count specific values

logical reads on global temp table, but not on session-level temp table

Is there a rule of thumb for determining the amount one should accept for a settlement offer?

Calculate the Mean mean of two numbers

That's an odd coin - I wonder why

Incomplete cube

"Eavesdropping" vs "Listen in on"

Traveling with my 5 year old daughter (as the father) without the mother from Germany to Mexico

What day is it again?

How does a dynamic QR code work?

Small nick on power cord from an electric alarm clock, and copper wiring exposed but intact

How can I prove that a state of equilibrium is unstable?

Creating a script with console commands

Is a distribution that is normal, but highly skewed, considered Gaussian?

Why did early computer designers eschew integers?

Why can't we say "I have been having a dog"?

How can the PCs determine if an item is a phylactery?

Does Germany produce more waste than the US?

What steps are necessary to read a Modern SSD in Medieval Europe?

Upgrading From a 9 Speed Sora Derailleur?

How can I replace x-axis labels with pre-determined symbols?

Shortening a title without changing its meaning

How dangerous is XSS

Masking layers by a vector polygon layer in QGIS

Planeswalker Ability and Death Timing



Subtracting numbers from adjacent columns and successive rows using awk



The Next CEO of Stack Overflowtext processing-perlawk manipulationAvoid conversion to scientific notation in awkBash to join columns from multiple filesUsing AWK to calculate mean and variance of columnsHow extract values which are less than 0 and dot from specific columns and print the entire row using awktext processing - Extracting using cshell and awkSubtracting same column between two rows in awkSwitch columns in CSV using awk?Looping through columns and rows to count specific values










4















I have a tab-separated file that looks like this:



NZ_CP023599.1 WP_003911075.1 302845 305406
NZ_CP023599.1 WP_003898428.1 471171 472583
NZ_CP023599.1 WP_003402248.1 534387 535157
NZ_CP023599.1 WP_003402301.1 552556 553950
NZ_CP023599.1 WP_003402318.1 558837 559697


I need to subtract the number in 4th column of each row from the number in 3rd column of the next line, and then print the difference in the next line as a 5th column.



The output would look like this:



NZ_CP023599.1 WP_003911075.1 302845 305406 
NZ_CP023599.1 WP_003898428.1 471171 472583 165765
NZ_CP023599.1 WP_003402248.1 534387 535157 61804
NZ_CP023599.1 WP_003402301.1 552556 553950 17399
NZ_CP023599.1 WP_003402318.1 558837 559697 4887


How do I go about this using awk?










share|improve this question







New contributor




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
























    4















    I have a tab-separated file that looks like this:



    NZ_CP023599.1 WP_003911075.1 302845 305406
    NZ_CP023599.1 WP_003898428.1 471171 472583
    NZ_CP023599.1 WP_003402248.1 534387 535157
    NZ_CP023599.1 WP_003402301.1 552556 553950
    NZ_CP023599.1 WP_003402318.1 558837 559697


    I need to subtract the number in 4th column of each row from the number in 3rd column of the next line, and then print the difference in the next line as a 5th column.



    The output would look like this:



    NZ_CP023599.1 WP_003911075.1 302845 305406 
    NZ_CP023599.1 WP_003898428.1 471171 472583 165765
    NZ_CP023599.1 WP_003402248.1 534387 535157 61804
    NZ_CP023599.1 WP_003402301.1 552556 553950 17399
    NZ_CP023599.1 WP_003402318.1 558837 559697 4887


    How do I go about this using awk?










    share|improve this question







    New contributor




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






















      4












      4








      4








      I have a tab-separated file that looks like this:



      NZ_CP023599.1 WP_003911075.1 302845 305406
      NZ_CP023599.1 WP_003898428.1 471171 472583
      NZ_CP023599.1 WP_003402248.1 534387 535157
      NZ_CP023599.1 WP_003402301.1 552556 553950
      NZ_CP023599.1 WP_003402318.1 558837 559697


      I need to subtract the number in 4th column of each row from the number in 3rd column of the next line, and then print the difference in the next line as a 5th column.



      The output would look like this:



      NZ_CP023599.1 WP_003911075.1 302845 305406 
      NZ_CP023599.1 WP_003898428.1 471171 472583 165765
      NZ_CP023599.1 WP_003402248.1 534387 535157 61804
      NZ_CP023599.1 WP_003402301.1 552556 553950 17399
      NZ_CP023599.1 WP_003402318.1 558837 559697 4887


      How do I go about this using awk?










      share|improve this question







      New contributor




      BhushanDhamale 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 tab-separated file that looks like this:



      NZ_CP023599.1 WP_003911075.1 302845 305406
      NZ_CP023599.1 WP_003898428.1 471171 472583
      NZ_CP023599.1 WP_003402248.1 534387 535157
      NZ_CP023599.1 WP_003402301.1 552556 553950
      NZ_CP023599.1 WP_003402318.1 558837 559697


      I need to subtract the number in 4th column of each row from the number in 3rd column of the next line, and then print the difference in the next line as a 5th column.



      The output would look like this:



      NZ_CP023599.1 WP_003911075.1 302845 305406 
      NZ_CP023599.1 WP_003898428.1 471171 472583 165765
      NZ_CP023599.1 WP_003402248.1 534387 535157 61804
      NZ_CP023599.1 WP_003402301.1 552556 553950 17399
      NZ_CP023599.1 WP_003402318.1 558837 559697 4887


      How do I go about this using awk?







      awk






      share|improve this question







      New contributor




      BhushanDhamale 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




      BhushanDhamale 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




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









      asked 15 hours ago









      BhushanDhamaleBhushanDhamale

      1233




      1233




      New contributor




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





      New contributor





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






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




















          3 Answers
          3






          active

          oldest

          votes


















          5














          You can do this as below. Defer the subtraction except for the first line but get its last column value as input for the subsequent line.



          awk -F't' 'BEGIN OFS = FS NR == 1 last = $4; print; next $5 = $3 - last; last = $4 1' file





          share|improve this answer
































            0














            awk -F\t ' if (length(prev4)>0) col5 = FS ($3-prev4) ; print $0 col5; prev4 = $4 ' InputFile





            share|improve this answer








            New contributor




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



























              -2














              Tried with Below command and its working fine



               for ((i=1;i<=5;i++)); do if [[ $i == 1 ]]; then awk -v i="$i" 'NR==iprint $0' l.txt ; j=`awk -v i="$i" 'NR==iprint $3 - $4 ' l.txt`; else awk -v i="$i" -v j="$j" 'NR==i$5=j;print $0' l.txt;j=`awk -v i="$i" 'NR == iprint $3 - $4' l.txt`;fi;done 





              share|improve this answer























              • This is extremely inefficient for several reasons, among them (1) awk is invoked once per line, with all the overhead of starting and stopping a process; (2) each time it is started, awk has to search through the file until it comes to the next line to be edited

                – pericynthion
                8 hours ago











              • In fact, this is so inefficient I'm wondering if it's an April fool's? If so, well played.

                – pericynthion
                8 hours ago











              • (1) This does not produce the correct, desired result. (2) Even though previous answers have shown that it is possible to do this with a single invocation of awk, you believe that it needs 10? (3) If sample data were real data (and the only data that will ever occur), we could just take the desired result from the question and wrap it in a printf or a cat. You have assumed that the real data will have only 5 lines of data, based on the fact that the sample input has only 5 lines of data.

                – G-Man
                8 hours ago











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



              );






              BhushanDhamale 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%2f509834%2fsubtracting-numbers-from-adjacent-columns-and-successive-rows-using-awk%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              5














              You can do this as below. Defer the subtraction except for the first line but get its last column value as input for the subsequent line.



              awk -F't' 'BEGIN OFS = FS NR == 1 last = $4; print; next $5 = $3 - last; last = $4 1' file





              share|improve this answer





























                5














                You can do this as below. Defer the subtraction except for the first line but get its last column value as input for the subsequent line.



                awk -F't' 'BEGIN OFS = FS NR == 1 last = $4; print; next $5 = $3 - last; last = $4 1' file





                share|improve this answer



























                  5












                  5








                  5







                  You can do this as below. Defer the subtraction except for the first line but get its last column value as input for the subsequent line.



                  awk -F't' 'BEGIN OFS = FS NR == 1 last = $4; print; next $5 = $3 - last; last = $4 1' file





                  share|improve this answer















                  You can do this as below. Defer the subtraction except for the first line but get its last column value as input for the subsequent line.



                  awk -F't' 'BEGIN OFS = FS NR == 1 last = $4; print; next $5 = $3 - last; last = $4 1' file






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 14 hours ago

























                  answered 15 hours ago









                  InianInian

                  5,3001530




                  5,3001530























                      0














                      awk -F\t ' if (length(prev4)>0) col5 = FS ($3-prev4) ; print $0 col5; prev4 = $4 ' InputFile





                      share|improve this answer








                      New contributor




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
























                        0














                        awk -F\t ' if (length(prev4)>0) col5 = FS ($3-prev4) ; print $0 col5; prev4 = $4 ' InputFile





                        share|improve this answer








                        New contributor




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






















                          0












                          0








                          0







                          awk -F\t ' if (length(prev4)>0) col5 = FS ($3-prev4) ; print $0 col5; prev4 = $4 ' InputFile





                          share|improve this answer








                          New contributor




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










                          awk -F\t ' if (length(prev4)>0) col5 = FS ($3-prev4) ; print $0 col5; prev4 = $4 ' InputFile






                          share|improve this answer








                          New contributor




                          John Martin 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 answer



                          share|improve this answer






                          New contributor




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









                          answered 11 hours ago









                          John MartinJohn Martin

                          1




                          1




                          New contributor




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





                          New contributor





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






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





















                              -2














                              Tried with Below command and its working fine



                               for ((i=1;i<=5;i++)); do if [[ $i == 1 ]]; then awk -v i="$i" 'NR==iprint $0' l.txt ; j=`awk -v i="$i" 'NR==iprint $3 - $4 ' l.txt`; else awk -v i="$i" -v j="$j" 'NR==i$5=j;print $0' l.txt;j=`awk -v i="$i" 'NR == iprint $3 - $4' l.txt`;fi;done 





                              share|improve this answer























                              • This is extremely inefficient for several reasons, among them (1) awk is invoked once per line, with all the overhead of starting and stopping a process; (2) each time it is started, awk has to search through the file until it comes to the next line to be edited

                                – pericynthion
                                8 hours ago











                              • In fact, this is so inefficient I'm wondering if it's an April fool's? If so, well played.

                                – pericynthion
                                8 hours ago











                              • (1) This does not produce the correct, desired result. (2) Even though previous answers have shown that it is possible to do this with a single invocation of awk, you believe that it needs 10? (3) If sample data were real data (and the only data that will ever occur), we could just take the desired result from the question and wrap it in a printf or a cat. You have assumed that the real data will have only 5 lines of data, based on the fact that the sample input has only 5 lines of data.

                                – G-Man
                                8 hours ago















                              -2














                              Tried with Below command and its working fine



                               for ((i=1;i<=5;i++)); do if [[ $i == 1 ]]; then awk -v i="$i" 'NR==iprint $0' l.txt ; j=`awk -v i="$i" 'NR==iprint $3 - $4 ' l.txt`; else awk -v i="$i" -v j="$j" 'NR==i$5=j;print $0' l.txt;j=`awk -v i="$i" 'NR == iprint $3 - $4' l.txt`;fi;done 





                              share|improve this answer























                              • This is extremely inefficient for several reasons, among them (1) awk is invoked once per line, with all the overhead of starting and stopping a process; (2) each time it is started, awk has to search through the file until it comes to the next line to be edited

                                – pericynthion
                                8 hours ago











                              • In fact, this is so inefficient I'm wondering if it's an April fool's? If so, well played.

                                – pericynthion
                                8 hours ago











                              • (1) This does not produce the correct, desired result. (2) Even though previous answers have shown that it is possible to do this with a single invocation of awk, you believe that it needs 10? (3) If sample data were real data (and the only data that will ever occur), we could just take the desired result from the question and wrap it in a printf or a cat. You have assumed that the real data will have only 5 lines of data, based on the fact that the sample input has only 5 lines of data.

                                – G-Man
                                8 hours ago













                              -2












                              -2








                              -2







                              Tried with Below command and its working fine



                               for ((i=1;i<=5;i++)); do if [[ $i == 1 ]]; then awk -v i="$i" 'NR==iprint $0' l.txt ; j=`awk -v i="$i" 'NR==iprint $3 - $4 ' l.txt`; else awk -v i="$i" -v j="$j" 'NR==i$5=j;print $0' l.txt;j=`awk -v i="$i" 'NR == iprint $3 - $4' l.txt`;fi;done 





                              share|improve this answer













                              Tried with Below command and its working fine



                               for ((i=1;i<=5;i++)); do if [[ $i == 1 ]]; then awk -v i="$i" 'NR==iprint $0' l.txt ; j=`awk -v i="$i" 'NR==iprint $3 - $4 ' l.txt`; else awk -v i="$i" -v j="$j" 'NR==i$5=j;print $0' l.txt;j=`awk -v i="$i" 'NR == iprint $3 - $4' l.txt`;fi;done 






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered 9 hours ago









                              Praveen Kumar BSPraveen Kumar BS

                              1,7101311




                              1,7101311












                              • This is extremely inefficient for several reasons, among them (1) awk is invoked once per line, with all the overhead of starting and stopping a process; (2) each time it is started, awk has to search through the file until it comes to the next line to be edited

                                – pericynthion
                                8 hours ago











                              • In fact, this is so inefficient I'm wondering if it's an April fool's? If so, well played.

                                – pericynthion
                                8 hours ago











                              • (1) This does not produce the correct, desired result. (2) Even though previous answers have shown that it is possible to do this with a single invocation of awk, you believe that it needs 10? (3) If sample data were real data (and the only data that will ever occur), we could just take the desired result from the question and wrap it in a printf or a cat. You have assumed that the real data will have only 5 lines of data, based on the fact that the sample input has only 5 lines of data.

                                – G-Man
                                8 hours ago

















                              • This is extremely inefficient for several reasons, among them (1) awk is invoked once per line, with all the overhead of starting and stopping a process; (2) each time it is started, awk has to search through the file until it comes to the next line to be edited

                                – pericynthion
                                8 hours ago











                              • In fact, this is so inefficient I'm wondering if it's an April fool's? If so, well played.

                                – pericynthion
                                8 hours ago











                              • (1) This does not produce the correct, desired result. (2) Even though previous answers have shown that it is possible to do this with a single invocation of awk, you believe that it needs 10? (3) If sample data were real data (and the only data that will ever occur), we could just take the desired result from the question and wrap it in a printf or a cat. You have assumed that the real data will have only 5 lines of data, based on the fact that the sample input has only 5 lines of data.

                                – G-Man
                                8 hours ago
















                              This is extremely inefficient for several reasons, among them (1) awk is invoked once per line, with all the overhead of starting and stopping a process; (2) each time it is started, awk has to search through the file until it comes to the next line to be edited

                              – pericynthion
                              8 hours ago





                              This is extremely inefficient for several reasons, among them (1) awk is invoked once per line, with all the overhead of starting and stopping a process; (2) each time it is started, awk has to search through the file until it comes to the next line to be edited

                              – pericynthion
                              8 hours ago













                              In fact, this is so inefficient I'm wondering if it's an April fool's? If so, well played.

                              – pericynthion
                              8 hours ago





                              In fact, this is so inefficient I'm wondering if it's an April fool's? If so, well played.

                              – pericynthion
                              8 hours ago













                              (1) This does not produce the correct, desired result. (2) Even though previous answers have shown that it is possible to do this with a single invocation of awk, you believe that it needs 10? (3) If sample data were real data (and the only data that will ever occur), we could just take the desired result from the question and wrap it in a printf or a cat. You have assumed that the real data will have only 5 lines of data, based on the fact that the sample input has only 5 lines of data.

                              – G-Man
                              8 hours ago





                              (1) This does not produce the correct, desired result. (2) Even though previous answers have shown that it is possible to do this with a single invocation of awk, you believe that it needs 10? (3) If sample data were real data (and the only data that will ever occur), we could just take the desired result from the question and wrap it in a printf or a cat. You have assumed that the real data will have only 5 lines of data, based on the fact that the sample input has only 5 lines of data.

                              – G-Man
                              8 hours ago










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









                              draft saved

                              draft discarded


















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












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











                              BhushanDhamale 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%2f509834%2fsubtracting-numbers-from-adjacent-columns-and-successive-rows-using-awk%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

                              Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

                              Circuit construction for execution of conditional statements using least significant bitHow are two different registers being used as “control”?How exactly is the stated composite state of the two registers being produced using the $R_zz$ controlled rotations?Efficiently performing controlled rotations in HHLWould this quantum algorithm implementation work?How to prepare a superposed states of odd integers from $1$ to $sqrtN$?Why is this implementation of the order finding algorithm not working?Circuit construction for Hamiltonian simulationHow can I invert the least significant bit of a certain term of a superposed state?Implementing an oracleImplementing a controlled sum operation

                              Magento 2 “No Payment Methods” in Admin New OrderHow to integrate Paypal Express Checkout with the Magento APIMagento 1.5 - Sales > Order > edit order and shipping methods disappearAuto Invoice Check/Money Order Payment methodAdd more simple payment methods?Shipping methods not showingWhat should I do to change payment methods if changing the configuration has no effects?1.9 - No Payment Methods showing upMy Payment Methods not Showing for downloadable/virtual product when checkout?Magento2 API to access internal payment methodHow to call an existing payment methods in the registration form?