how to increment a column value with 1 in a csv file












0















I have a text file with 3 columns as below.



$ cat test.txt
1,A,300
1,B,300
1,C,300


Now i want to increment the third column only,
the output should be like below



1,A,300
1,B,301
1,C,302


Till now i have tried as,



awk -F, '{$3=$3+1;print}' OFS=, test.txt


But output is coming as,



1,A,301
1,B,301
1,C,301


Please do suggest, how to achieve the desired output?










share|improve this question
















bumped to the homepage by Community 1 min ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.











  • 1





    or keep a separate variable for the amount to increment, and increment that variable for each line: $3 += inc; inc++

    – glenn jackman
    Feb 1 '17 at 18:36


















0















I have a text file with 3 columns as below.



$ cat test.txt
1,A,300
1,B,300
1,C,300


Now i want to increment the third column only,
the output should be like below



1,A,300
1,B,301
1,C,302


Till now i have tried as,



awk -F, '{$3=$3+1;print}' OFS=, test.txt


But output is coming as,



1,A,301
1,B,301
1,C,301


Please do suggest, how to achieve the desired output?










share|improve this question
















bumped to the homepage by Community 1 min ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.











  • 1





    or keep a separate variable for the amount to increment, and increment that variable for each line: $3 += inc; inc++

    – glenn jackman
    Feb 1 '17 at 18:36
















0












0








0








I have a text file with 3 columns as below.



$ cat test.txt
1,A,300
1,B,300
1,C,300


Now i want to increment the third column only,
the output should be like below



1,A,300
1,B,301
1,C,302


Till now i have tried as,



awk -F, '{$3=$3+1;print}' OFS=, test.txt


But output is coming as,



1,A,301
1,B,301
1,C,301


Please do suggest, how to achieve the desired output?










share|improve this question
















I have a text file with 3 columns as below.



$ cat test.txt
1,A,300
1,B,300
1,C,300


Now i want to increment the third column only,
the output should be like below



1,A,300
1,B,301
1,C,302


Till now i have tried as,



awk -F, '{$3=$3+1;print}' OFS=, test.txt


But output is coming as,



1,A,301
1,B,301
1,C,301


Please do suggest, how to achieve the desired output?







awk






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 1 '17 at 18:35









glenn jackman

51.1k571110




51.1k571110










asked Feb 1 '17 at 18:23









swapneilswapneil

12




12





bumped to the homepage by Community 1 min ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community 1 min ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.










  • 1





    or keep a separate variable for the amount to increment, and increment that variable for each line: $3 += inc; inc++

    – glenn jackman
    Feb 1 '17 at 18:36
















  • 1





    or keep a separate variable for the amount to increment, and increment that variable for each line: $3 += inc; inc++

    – glenn jackman
    Feb 1 '17 at 18:36










1




1





or keep a separate variable for the amount to increment, and increment that variable for each line: $3 += inc; inc++

– glenn jackman
Feb 1 '17 at 18:36







or keep a separate variable for the amount to increment, and increment that variable for each line: $3 += inc; inc++

– glenn jackman
Feb 1 '17 at 18:36












2 Answers
2






active

oldest

votes


















0














awk -F, 'BEGIN {offset=0; OFS=","} {print $1,$2,$3+offset++}"' /path/to/input


or, alternatively:



awk -F, 'BEGIN {OFS=","} {print $1,$2,$3+(NR-1)}"' /path/to/input





share|improve this answer































    0














    Your initial approach with a small tweak:



    awk -F',' 'BEGIN { OFS = FS } { $3 = $3 + (NR-1); print }' test.txt


    The NR variable holds the number of records (lines) read so far.






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


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f341806%2fhow-to-increment-a-column-value-with-1-in-a-csv-file%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














      awk -F, 'BEGIN {offset=0; OFS=","} {print $1,$2,$3+offset++}"' /path/to/input


      or, alternatively:



      awk -F, 'BEGIN {OFS=","} {print $1,$2,$3+(NR-1)}"' /path/to/input





      share|improve this answer




























        0














        awk -F, 'BEGIN {offset=0; OFS=","} {print $1,$2,$3+offset++}"' /path/to/input


        or, alternatively:



        awk -F, 'BEGIN {OFS=","} {print $1,$2,$3+(NR-1)}"' /path/to/input





        share|improve this answer


























          0












          0








          0







          awk -F, 'BEGIN {offset=0; OFS=","} {print $1,$2,$3+offset++}"' /path/to/input


          or, alternatively:



          awk -F, 'BEGIN {OFS=","} {print $1,$2,$3+(NR-1)}"' /path/to/input





          share|improve this answer













          awk -F, 'BEGIN {offset=0; OFS=","} {print $1,$2,$3+offset++}"' /path/to/input


          or, alternatively:



          awk -F, 'BEGIN {OFS=","} {print $1,$2,$3+(NR-1)}"' /path/to/input






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Feb 1 '17 at 18:48









          DopeGhotiDopeGhoti

          44.3k55684




          44.3k55684

























              0














              Your initial approach with a small tweak:



              awk -F',' 'BEGIN { OFS = FS } { $3 = $3 + (NR-1); print }' test.txt


              The NR variable holds the number of records (lines) read so far.






              share|improve this answer




























                0














                Your initial approach with a small tweak:



                awk -F',' 'BEGIN { OFS = FS } { $3 = $3 + (NR-1); print }' test.txt


                The NR variable holds the number of records (lines) read so far.






                share|improve this answer


























                  0












                  0








                  0







                  Your initial approach with a small tweak:



                  awk -F',' 'BEGIN { OFS = FS } { $3 = $3 + (NR-1); print }' test.txt


                  The NR variable holds the number of records (lines) read so far.






                  share|improve this answer













                  Your initial approach with a small tweak:



                  awk -F',' 'BEGIN { OFS = FS } { $3 = $3 + (NR-1); print }' test.txt


                  The NR variable holds the number of records (lines) read so far.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Feb 1 '17 at 19:46









                  KusalanandaKusalananda

                  126k16239393




                  126k16239393






























                      draft saved

                      draft discarded




















































                      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%2f341806%2fhow-to-increment-a-column-value-with-1-in-a-csv-file%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

                      Loup dans la culture

                      How to solve the problem of ntp “Unable to contact time server” from KDE?

                      ASUS Zenbook UX433/UX333 — Configure Touchpad-embedded numpad on Linux