Subtracting same column between two rows in awk

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





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
4
down vote

favorite












I want to subtract the second line with the first line. The file is like this



tmptxt



A B 1 2 3 4 
C D 9 8 7 6


The desired output is



8 6 4 2


How to do this in awk?



I managed to output for a single column only:



awk '$temp=$3-prev3; prev3=$3print $temp'






share|improve this question

















  • 1




    @John1024I managed to output for single column awk '$temp=$3-prev3; prev3=$3print $temp'. The only limitation is i am using tsch on CentOS. I know tsch is inferior compared to other interpreter but this is what i have to use. I tried this too but didnt get what i want cat tmpfile | awk 'for(i=3;i<=NF;i++)$temp[i]=$i-prev; prev=$i;print $temp[i]'
    – nabilishes
    Nov 23 '17 at 5:08

















up vote
4
down vote

favorite












I want to subtract the second line with the first line. The file is like this



tmptxt



A B 1 2 3 4 
C D 9 8 7 6


The desired output is



8 6 4 2


How to do this in awk?



I managed to output for a single column only:



awk '$temp=$3-prev3; prev3=$3print $temp'






share|improve this question

















  • 1




    @John1024I managed to output for single column awk '$temp=$3-prev3; prev3=$3print $temp'. The only limitation is i am using tsch on CentOS. I know tsch is inferior compared to other interpreter but this is what i have to use. I tried this too but didnt get what i want cat tmpfile | awk 'for(i=3;i<=NF;i++)$temp[i]=$i-prev; prev=$i;print $temp[i]'
    – nabilishes
    Nov 23 '17 at 5:08













up vote
4
down vote

favorite









up vote
4
down vote

favorite











I want to subtract the second line with the first line. The file is like this



tmptxt



A B 1 2 3 4 
C D 9 8 7 6


The desired output is



8 6 4 2


How to do this in awk?



I managed to output for a single column only:



awk '$temp=$3-prev3; prev3=$3print $temp'






share|improve this question













I want to subtract the second line with the first line. The file is like this



tmptxt



A B 1 2 3 4 
C D 9 8 7 6


The desired output is



8 6 4 2


How to do this in awk?



I managed to output for a single column only:



awk '$temp=$3-prev3; prev3=$3print $temp'








share|improve this question












share|improve this question




share|improve this question








edited Nov 23 '17 at 5:57
























asked Nov 23 '17 at 4:07









nabilishes

337




337







  • 1




    @John1024I managed to output for single column awk '$temp=$3-prev3; prev3=$3print $temp'. The only limitation is i am using tsch on CentOS. I know tsch is inferior compared to other interpreter but this is what i have to use. I tried this too but didnt get what i want cat tmpfile | awk 'for(i=3;i<=NF;i++)$temp[i]=$i-prev; prev=$i;print $temp[i]'
    – nabilishes
    Nov 23 '17 at 5:08













  • 1




    @John1024I managed to output for single column awk '$temp=$3-prev3; prev3=$3print $temp'. The only limitation is i am using tsch on CentOS. I know tsch is inferior compared to other interpreter but this is what i have to use. I tried this too but didnt get what i want cat tmpfile | awk 'for(i=3;i<=NF;i++)$temp[i]=$i-prev; prev=$i;print $temp[i]'
    – nabilishes
    Nov 23 '17 at 5:08








1




1




@John1024I managed to output for single column awk '$temp=$3-prev3; prev3=$3print $temp'. The only limitation is i am using tsch on CentOS. I know tsch is inferior compared to other interpreter but this is what i have to use. I tried this too but didnt get what i want cat tmpfile | awk 'for(i=3;i<=NF;i++)$temp[i]=$i-prev; prev=$i;print $temp[i]'
– nabilishes
Nov 23 '17 at 5:08





@John1024I managed to output for single column awk '$temp=$3-prev3; prev3=$3print $temp'. The only limitation is i am using tsch on CentOS. I know tsch is inferior compared to other interpreter but this is what i have to use. I tried this too but didnt get what i want cat tmpfile | awk 'for(i=3;i<=NF;i++)$temp[i]=$i-prev; prev=$i;print $temp[i]'
– nabilishes
Nov 23 '17 at 5:08











3 Answers
3






active

oldest

votes

















up vote
4
down vote



accepted










A solution with awk



awk '
NR==1 split($0,a)
NR==2 split($0,b)
END for(i=1;i<=NF;i++) printf "%d ", b[i]-a[i]
' input.txt


gives a result of



0 0 8 6 4 2


Since awk interpret strings without valid numbers as 0 during arithmetic operations, in case you want to remove the results in which the source field contains non-numeric values, you can do this by adding an additional condition.



awk '
NR==1 split($0,a)
NR==2 split($0,b)
END
for(i=1;i<=NF;i++)
if(a[i] ~ /^[0-9]+$/ && b[i] ~ /^[0-9]+$/)
printf "%d ", b[i]-a[i]

' input.txt


gives a result of



8 6 4 2





share|improve this answer



















  • 1




    thanks! this somehow help me in understanding lines splitting. However due to the nature of my workstation running on tsch, the arrangement of output is different on my side. It gives out with your first solution 6 4 2 0 0 8
    – nabilishes
    Nov 23 '17 at 6:11










  • @nabilishes Hmm, that's odd, what's the awk version you are currently using awk --version?
    – etopylight
    Nov 23 '17 at 6:52










  • Its GNU Awk 3.1.7
    – nabilishes
    Nov 23 '17 at 6:54







  • 1




    @nabilishes Thanks for the feedback, I can reproduce the same result using gawk 3.1.7 with bash, it seems that the scanning order of array in awk is different in older versions. A safer solution is to replace for(i in a) with an explicit order for(i=1;i<=NF;i++)
    – etopylight
    Nov 23 '17 at 7:09










  • Thanks! this solution works like a charm. I previously had the same problem with the array sorting - Here is the link of the same array sorting problem. It is in the comment section. Now i know its due to the version and not environment or interpreter
    – nabilishes
    Nov 23 '17 at 7:23


















up vote
2
down vote













Alternative Python solution:



python -c 'import sys; f=open(sys.argv[1],"r"); 
print(" ".join(str(int(d2)-int(d1))
for d1,d2 in zip(next(f).split()[2:], next(f).split()[2:]))); f.close()' file


The output:



8 6 4 2





share|improve this answer




























    up vote
    0
    down vote













    below command also provides same output. l.txt contains the input which you have provided in question



    for i in 1..6; do awk -v i="$i" 'BEGINsum=0sum=$i-sumENDprint sum' l.txt ; done| tr "n" " " | awk '$1="";$2="";print $0' | sed "s/^ //g"


    output



    8 6 4 2





    share|improve this answer























      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',
      convertImagesToLinks: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      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%2f406465%2fsubtracting-same-column-between-two-rows-in-awk%23new-answer', 'question_page');

      );

      Post as a guest






























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      4
      down vote



      accepted










      A solution with awk



      awk '
      NR==1 split($0,a)
      NR==2 split($0,b)
      END for(i=1;i<=NF;i++) printf "%d ", b[i]-a[i]
      ' input.txt


      gives a result of



      0 0 8 6 4 2


      Since awk interpret strings without valid numbers as 0 during arithmetic operations, in case you want to remove the results in which the source field contains non-numeric values, you can do this by adding an additional condition.



      awk '
      NR==1 split($0,a)
      NR==2 split($0,b)
      END
      for(i=1;i<=NF;i++)
      if(a[i] ~ /^[0-9]+$/ && b[i] ~ /^[0-9]+$/)
      printf "%d ", b[i]-a[i]

      ' input.txt


      gives a result of



      8 6 4 2





      share|improve this answer



















      • 1




        thanks! this somehow help me in understanding lines splitting. However due to the nature of my workstation running on tsch, the arrangement of output is different on my side. It gives out with your first solution 6 4 2 0 0 8
        – nabilishes
        Nov 23 '17 at 6:11










      • @nabilishes Hmm, that's odd, what's the awk version you are currently using awk --version?
        – etopylight
        Nov 23 '17 at 6:52










      • Its GNU Awk 3.1.7
        – nabilishes
        Nov 23 '17 at 6:54







      • 1




        @nabilishes Thanks for the feedback, I can reproduce the same result using gawk 3.1.7 with bash, it seems that the scanning order of array in awk is different in older versions. A safer solution is to replace for(i in a) with an explicit order for(i=1;i<=NF;i++)
        – etopylight
        Nov 23 '17 at 7:09










      • Thanks! this solution works like a charm. I previously had the same problem with the array sorting - Here is the link of the same array sorting problem. It is in the comment section. Now i know its due to the version and not environment or interpreter
        – nabilishes
        Nov 23 '17 at 7:23















      up vote
      4
      down vote



      accepted










      A solution with awk



      awk '
      NR==1 split($0,a)
      NR==2 split($0,b)
      END for(i=1;i<=NF;i++) printf "%d ", b[i]-a[i]
      ' input.txt


      gives a result of



      0 0 8 6 4 2


      Since awk interpret strings without valid numbers as 0 during arithmetic operations, in case you want to remove the results in which the source field contains non-numeric values, you can do this by adding an additional condition.



      awk '
      NR==1 split($0,a)
      NR==2 split($0,b)
      END
      for(i=1;i<=NF;i++)
      if(a[i] ~ /^[0-9]+$/ && b[i] ~ /^[0-9]+$/)
      printf "%d ", b[i]-a[i]

      ' input.txt


      gives a result of



      8 6 4 2





      share|improve this answer



















      • 1




        thanks! this somehow help me in understanding lines splitting. However due to the nature of my workstation running on tsch, the arrangement of output is different on my side. It gives out with your first solution 6 4 2 0 0 8
        – nabilishes
        Nov 23 '17 at 6:11










      • @nabilishes Hmm, that's odd, what's the awk version you are currently using awk --version?
        – etopylight
        Nov 23 '17 at 6:52










      • Its GNU Awk 3.1.7
        – nabilishes
        Nov 23 '17 at 6:54







      • 1




        @nabilishes Thanks for the feedback, I can reproduce the same result using gawk 3.1.7 with bash, it seems that the scanning order of array in awk is different in older versions. A safer solution is to replace for(i in a) with an explicit order for(i=1;i<=NF;i++)
        – etopylight
        Nov 23 '17 at 7:09










      • Thanks! this solution works like a charm. I previously had the same problem with the array sorting - Here is the link of the same array sorting problem. It is in the comment section. Now i know its due to the version and not environment or interpreter
        – nabilishes
        Nov 23 '17 at 7:23













      up vote
      4
      down vote



      accepted







      up vote
      4
      down vote



      accepted






      A solution with awk



      awk '
      NR==1 split($0,a)
      NR==2 split($0,b)
      END for(i=1;i<=NF;i++) printf "%d ", b[i]-a[i]
      ' input.txt


      gives a result of



      0 0 8 6 4 2


      Since awk interpret strings without valid numbers as 0 during arithmetic operations, in case you want to remove the results in which the source field contains non-numeric values, you can do this by adding an additional condition.



      awk '
      NR==1 split($0,a)
      NR==2 split($0,b)
      END
      for(i=1;i<=NF;i++)
      if(a[i] ~ /^[0-9]+$/ && b[i] ~ /^[0-9]+$/)
      printf "%d ", b[i]-a[i]

      ' input.txt


      gives a result of



      8 6 4 2





      share|improve this answer















      A solution with awk



      awk '
      NR==1 split($0,a)
      NR==2 split($0,b)
      END for(i=1;i<=NF;i++) printf "%d ", b[i]-a[i]
      ' input.txt


      gives a result of



      0 0 8 6 4 2


      Since awk interpret strings without valid numbers as 0 during arithmetic operations, in case you want to remove the results in which the source field contains non-numeric values, you can do this by adding an additional condition.



      awk '
      NR==1 split($0,a)
      NR==2 split($0,b)
      END
      for(i=1;i<=NF;i++)
      if(a[i] ~ /^[0-9]+$/ && b[i] ~ /^[0-9]+$/)
      printf "%d ", b[i]-a[i]

      ' input.txt


      gives a result of



      8 6 4 2






      share|improve this answer















      share|improve this answer



      share|improve this answer








      edited Nov 23 '17 at 7:17


























      answered Nov 23 '17 at 4:45









      etopylight

      383117




      383117







      • 1




        thanks! this somehow help me in understanding lines splitting. However due to the nature of my workstation running on tsch, the arrangement of output is different on my side. It gives out with your first solution 6 4 2 0 0 8
        – nabilishes
        Nov 23 '17 at 6:11










      • @nabilishes Hmm, that's odd, what's the awk version you are currently using awk --version?
        – etopylight
        Nov 23 '17 at 6:52










      • Its GNU Awk 3.1.7
        – nabilishes
        Nov 23 '17 at 6:54







      • 1




        @nabilishes Thanks for the feedback, I can reproduce the same result using gawk 3.1.7 with bash, it seems that the scanning order of array in awk is different in older versions. A safer solution is to replace for(i in a) with an explicit order for(i=1;i<=NF;i++)
        – etopylight
        Nov 23 '17 at 7:09










      • Thanks! this solution works like a charm. I previously had the same problem with the array sorting - Here is the link of the same array sorting problem. It is in the comment section. Now i know its due to the version and not environment or interpreter
        – nabilishes
        Nov 23 '17 at 7:23













      • 1




        thanks! this somehow help me in understanding lines splitting. However due to the nature of my workstation running on tsch, the arrangement of output is different on my side. It gives out with your first solution 6 4 2 0 0 8
        – nabilishes
        Nov 23 '17 at 6:11










      • @nabilishes Hmm, that's odd, what's the awk version you are currently using awk --version?
        – etopylight
        Nov 23 '17 at 6:52










      • Its GNU Awk 3.1.7
        – nabilishes
        Nov 23 '17 at 6:54







      • 1




        @nabilishes Thanks for the feedback, I can reproduce the same result using gawk 3.1.7 with bash, it seems that the scanning order of array in awk is different in older versions. A safer solution is to replace for(i in a) with an explicit order for(i=1;i<=NF;i++)
        – etopylight
        Nov 23 '17 at 7:09










      • Thanks! this solution works like a charm. I previously had the same problem with the array sorting - Here is the link of the same array sorting problem. It is in the comment section. Now i know its due to the version and not environment or interpreter
        – nabilishes
        Nov 23 '17 at 7:23








      1




      1




      thanks! this somehow help me in understanding lines splitting. However due to the nature of my workstation running on tsch, the arrangement of output is different on my side. It gives out with your first solution 6 4 2 0 0 8
      – nabilishes
      Nov 23 '17 at 6:11




      thanks! this somehow help me in understanding lines splitting. However due to the nature of my workstation running on tsch, the arrangement of output is different on my side. It gives out with your first solution 6 4 2 0 0 8
      – nabilishes
      Nov 23 '17 at 6:11












      @nabilishes Hmm, that's odd, what's the awk version you are currently using awk --version?
      – etopylight
      Nov 23 '17 at 6:52




      @nabilishes Hmm, that's odd, what's the awk version you are currently using awk --version?
      – etopylight
      Nov 23 '17 at 6:52












      Its GNU Awk 3.1.7
      – nabilishes
      Nov 23 '17 at 6:54





      Its GNU Awk 3.1.7
      – nabilishes
      Nov 23 '17 at 6:54





      1




      1




      @nabilishes Thanks for the feedback, I can reproduce the same result using gawk 3.1.7 with bash, it seems that the scanning order of array in awk is different in older versions. A safer solution is to replace for(i in a) with an explicit order for(i=1;i<=NF;i++)
      – etopylight
      Nov 23 '17 at 7:09




      @nabilishes Thanks for the feedback, I can reproduce the same result using gawk 3.1.7 with bash, it seems that the scanning order of array in awk is different in older versions. A safer solution is to replace for(i in a) with an explicit order for(i=1;i<=NF;i++)
      – etopylight
      Nov 23 '17 at 7:09












      Thanks! this solution works like a charm. I previously had the same problem with the array sorting - Here is the link of the same array sorting problem. It is in the comment section. Now i know its due to the version and not environment or interpreter
      – nabilishes
      Nov 23 '17 at 7:23





      Thanks! this solution works like a charm. I previously had the same problem with the array sorting - Here is the link of the same array sorting problem. It is in the comment section. Now i know its due to the version and not environment or interpreter
      – nabilishes
      Nov 23 '17 at 7:23













      up vote
      2
      down vote













      Alternative Python solution:



      python -c 'import sys; f=open(sys.argv[1],"r"); 
      print(" ".join(str(int(d2)-int(d1))
      for d1,d2 in zip(next(f).split()[2:], next(f).split()[2:]))); f.close()' file


      The output:



      8 6 4 2





      share|improve this answer

























        up vote
        2
        down vote













        Alternative Python solution:



        python -c 'import sys; f=open(sys.argv[1],"r"); 
        print(" ".join(str(int(d2)-int(d1))
        for d1,d2 in zip(next(f).split()[2:], next(f).split()[2:]))); f.close()' file


        The output:



        8 6 4 2





        share|improve this answer























          up vote
          2
          down vote










          up vote
          2
          down vote









          Alternative Python solution:



          python -c 'import sys; f=open(sys.argv[1],"r"); 
          print(" ".join(str(int(d2)-int(d1))
          for d1,d2 in zip(next(f).split()[2:], next(f).split()[2:]))); f.close()' file


          The output:



          8 6 4 2





          share|improve this answer













          Alternative Python solution:



          python -c 'import sys; f=open(sys.argv[1],"r"); 
          print(" ".join(str(int(d2)-int(d1))
          for d1,d2 in zip(next(f).split()[2:], next(f).split()[2:]))); f.close()' file


          The output:



          8 6 4 2






          share|improve this answer













          share|improve this answer



          share|improve this answer











          answered Nov 23 '17 at 8:31









          RomanPerekhrest

          22.4k12144




          22.4k12144




















              up vote
              0
              down vote













              below command also provides same output. l.txt contains the input which you have provided in question



              for i in 1..6; do awk -v i="$i" 'BEGINsum=0sum=$i-sumENDprint sum' l.txt ; done| tr "n" " " | awk '$1="";$2="";print $0' | sed "s/^ //g"


              output



              8 6 4 2





              share|improve this answer



























                up vote
                0
                down vote













                below command also provides same output. l.txt contains the input which you have provided in question



                for i in 1..6; do awk -v i="$i" 'BEGINsum=0sum=$i-sumENDprint sum' l.txt ; done| tr "n" " " | awk '$1="";$2="";print $0' | sed "s/^ //g"


                output



                8 6 4 2





                share|improve this answer

























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  below command also provides same output. l.txt contains the input which you have provided in question



                  for i in 1..6; do awk -v i="$i" 'BEGINsum=0sum=$i-sumENDprint sum' l.txt ; done| tr "n" " " | awk '$1="";$2="";print $0' | sed "s/^ //g"


                  output



                  8 6 4 2





                  share|improve this answer















                  below command also provides same output. l.txt contains the input which you have provided in question



                  for i in 1..6; do awk -v i="$i" 'BEGINsum=0sum=$i-sumENDprint sum' l.txt ; done| tr "n" " " | awk '$1="";$2="";print $0' | sed "s/^ //g"


                  output



                  8 6 4 2






                  share|improve this answer















                  share|improve this answer



                  share|improve this answer








                  edited Nov 23 '17 at 12:23









                  Jeff Schaller

                  30.7k846104




                  30.7k846104











                  answered Nov 23 '17 at 6:44









                  Praveen Kumar BS

                  1,010128




                  1,010128






















                       

                      draft saved


                      draft discarded


























                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f406465%2fsubtracting-same-column-between-two-rows-in-awk%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      Peggy Mitchell

                      Palaiologos

                      The Forum (Inglewood, California)