Printing the difference between adjacent values in a column to a new column

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











up vote
0
down vote

favorite












I have a file



A 1
A 2
A 4
A 6


I want to print the difference between adjacent values (below-above) in column 2 to a new column 3, to get this



A 1
A 2 1
A 4 2
A 6 2


I have discovered something like this on SO, but failed to print it as a new column.



awk 'NR>1print $1-p p=$1' file









share|improve this question



























    up vote
    0
    down vote

    favorite












    I have a file



    A 1
    A 2
    A 4
    A 6


    I want to print the difference between adjacent values (below-above) in column 2 to a new column 3, to get this



    A 1
    A 2 1
    A 4 2
    A 6 2


    I have discovered something like this on SO, but failed to print it as a new column.



    awk 'NR>1print $1-p p=$1' file









    share|improve this question

























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have a file



      A 1
      A 2
      A 4
      A 6


      I want to print the difference between adjacent values (below-above) in column 2 to a new column 3, to get this



      A 1
      A 2 1
      A 4 2
      A 6 2


      I have discovered something like this on SO, but failed to print it as a new column.



      awk 'NR>1print $1-p p=$1' file









      share|improve this question















      I have a file



      A 1
      A 2
      A 4
      A 6


      I want to print the difference between adjacent values (below-above) in column 2 to a new column 3, to get this



      A 1
      A 2 1
      A 4 2
      A 6 2


      I have discovered something like this on SO, but failed to print it as a new column.



      awk 'NR>1print $1-p p=$1' file






      awk






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Oct 3 '17 at 3:59









      αғsнιη

      15.7k92563




      15.7k92563










      asked Oct 3 '17 at 3:29









      Johnny Tam

      1208




      1208




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          5
          down vote



          accepted










          To modify the given code in question



          $ awk 'NR>1$3=$2-p p=$2 1' file 
          A 1
          A 2 1
          A 4 2
          A 6 2


          • Fields are indexed from 1, so use $2 for second column


            • $0 contains entire input record


          • After modifying, you need to print the record. Default action is printing contents of $0 if condition is true. 1 is used idiomatically for such cases





          share|improve this answer




















          • Now I would like to modify the script so it prints the quotient of the division by the value 10 steps above. I tried awk 'NR>1$3=$2/p p=$2 10' file but failed. Maybe I misunderstood some parts of the script. How should I correct it?
            – Johnny Tam
            Oct 4 '17 at 4:14










          • @JohnnyTam I didn't get what are you trying... can you tell the three values you need? is this what you need? awk 'NR>1$3=int($2/p) p=$2 1'
            – Sundeep
            Oct 4 '17 at 4:58










          • Please go here: unix.stackexchange.com/questions/396195/… for a simplified illustration. :)
            – Johnny Tam
            Oct 5 '17 at 6:13










          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%2f395746%2fprinting-the-difference-between-adjacent-values-in-a-column-to-a-new-column%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          5
          down vote



          accepted










          To modify the given code in question



          $ awk 'NR>1$3=$2-p p=$2 1' file 
          A 1
          A 2 1
          A 4 2
          A 6 2


          • Fields are indexed from 1, so use $2 for second column


            • $0 contains entire input record


          • After modifying, you need to print the record. Default action is printing contents of $0 if condition is true. 1 is used idiomatically for such cases





          share|improve this answer




















          • Now I would like to modify the script so it prints the quotient of the division by the value 10 steps above. I tried awk 'NR>1$3=$2/p p=$2 10' file but failed. Maybe I misunderstood some parts of the script. How should I correct it?
            – Johnny Tam
            Oct 4 '17 at 4:14










          • @JohnnyTam I didn't get what are you trying... can you tell the three values you need? is this what you need? awk 'NR>1$3=int($2/p) p=$2 1'
            – Sundeep
            Oct 4 '17 at 4:58










          • Please go here: unix.stackexchange.com/questions/396195/… for a simplified illustration. :)
            – Johnny Tam
            Oct 5 '17 at 6:13














          up vote
          5
          down vote



          accepted










          To modify the given code in question



          $ awk 'NR>1$3=$2-p p=$2 1' file 
          A 1
          A 2 1
          A 4 2
          A 6 2


          • Fields are indexed from 1, so use $2 for second column


            • $0 contains entire input record


          • After modifying, you need to print the record. Default action is printing contents of $0 if condition is true. 1 is used idiomatically for such cases





          share|improve this answer




















          • Now I would like to modify the script so it prints the quotient of the division by the value 10 steps above. I tried awk 'NR>1$3=$2/p p=$2 10' file but failed. Maybe I misunderstood some parts of the script. How should I correct it?
            – Johnny Tam
            Oct 4 '17 at 4:14










          • @JohnnyTam I didn't get what are you trying... can you tell the three values you need? is this what you need? awk 'NR>1$3=int($2/p) p=$2 1'
            – Sundeep
            Oct 4 '17 at 4:58










          • Please go here: unix.stackexchange.com/questions/396195/… for a simplified illustration. :)
            – Johnny Tam
            Oct 5 '17 at 6:13












          up vote
          5
          down vote



          accepted







          up vote
          5
          down vote



          accepted






          To modify the given code in question



          $ awk 'NR>1$3=$2-p p=$2 1' file 
          A 1
          A 2 1
          A 4 2
          A 6 2


          • Fields are indexed from 1, so use $2 for second column


            • $0 contains entire input record


          • After modifying, you need to print the record. Default action is printing contents of $0 if condition is true. 1 is used idiomatically for such cases





          share|improve this answer












          To modify the given code in question



          $ awk 'NR>1$3=$2-p p=$2 1' file 
          A 1
          A 2 1
          A 4 2
          A 6 2


          • Fields are indexed from 1, so use $2 for second column


            • $0 contains entire input record


          • After modifying, you need to print the record. Default action is printing contents of $0 if condition is true. 1 is used idiomatically for such cases






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Oct 3 '17 at 4:14









          Sundeep

          6,9611826




          6,9611826











          • Now I would like to modify the script so it prints the quotient of the division by the value 10 steps above. I tried awk 'NR>1$3=$2/p p=$2 10' file but failed. Maybe I misunderstood some parts of the script. How should I correct it?
            – Johnny Tam
            Oct 4 '17 at 4:14










          • @JohnnyTam I didn't get what are you trying... can you tell the three values you need? is this what you need? awk 'NR>1$3=int($2/p) p=$2 1'
            – Sundeep
            Oct 4 '17 at 4:58










          • Please go here: unix.stackexchange.com/questions/396195/… for a simplified illustration. :)
            – Johnny Tam
            Oct 5 '17 at 6:13
















          • Now I would like to modify the script so it prints the quotient of the division by the value 10 steps above. I tried awk 'NR>1$3=$2/p p=$2 10' file but failed. Maybe I misunderstood some parts of the script. How should I correct it?
            – Johnny Tam
            Oct 4 '17 at 4:14










          • @JohnnyTam I didn't get what are you trying... can you tell the three values you need? is this what you need? awk 'NR>1$3=int($2/p) p=$2 1'
            – Sundeep
            Oct 4 '17 at 4:58










          • Please go here: unix.stackexchange.com/questions/396195/… for a simplified illustration. :)
            – Johnny Tam
            Oct 5 '17 at 6:13















          Now I would like to modify the script so it prints the quotient of the division by the value 10 steps above. I tried awk 'NR>1$3=$2/p p=$2 10' file but failed. Maybe I misunderstood some parts of the script. How should I correct it?
          – Johnny Tam
          Oct 4 '17 at 4:14




          Now I would like to modify the script so it prints the quotient of the division by the value 10 steps above. I tried awk 'NR>1$3=$2/p p=$2 10' file but failed. Maybe I misunderstood some parts of the script. How should I correct it?
          – Johnny Tam
          Oct 4 '17 at 4:14












          @JohnnyTam I didn't get what are you trying... can you tell the three values you need? is this what you need? awk 'NR>1$3=int($2/p) p=$2 1'
          – Sundeep
          Oct 4 '17 at 4:58




          @JohnnyTam I didn't get what are you trying... can you tell the three values you need? is this what you need? awk 'NR>1$3=int($2/p) p=$2 1'
          – Sundeep
          Oct 4 '17 at 4:58












          Please go here: unix.stackexchange.com/questions/396195/… for a simplified illustration. :)
          – Johnny Tam
          Oct 5 '17 at 6:13




          Please go here: unix.stackexchange.com/questions/396195/… for a simplified illustration. :)
          – Johnny Tam
          Oct 5 '17 at 6:13

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f395746%2fprinting-the-difference-between-adjacent-values-in-a-column-to-a-new-column%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

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

          Bahrain

          Postfix configuration issue with fips on centos 7; mailgun relay