Changing last entries in a comma delimited list2019 Community Moderator ElectionHow to replace line with file contents and around the content with some text?Comparing 3 lines and 2 lines in BASHHow do I get bash to give a relevant error for a file with windows line-endings?Extract strings from between tags and save to new text fileReplace first column of file by command outputRemove all duplicate word from string using shell scriptHow do I prepend a line read from STDIN with an epoch timestamp?Replace string in a huge (70GB), one line, text fileJoining entries based off of column using awk/join

Why Shazam when there is already Superman?

What will be next at the bottom row and why?

What does chmod -u do?

Dealing with a rejection from a journal

Redundant comparison & "if" before assignment

Pre-mixing cryogenic fuels and using only one fuel tank

How does the math work for Perception checks?

Rising and falling intonation

The screen of my macbook suddenly broken down how can I do to recover

Temporarily disable WLAN internet access for children, but allow it for adults

Are paving bricks differently sized for sand bedding vs mortar bedding?

Explaining alternative travel routes when going to the USA

Find the Primitive Roots Mod 31

How could a planet have erratic days?

What was this official D&D 3.5e Lovecraft-flavored rulebook?

Terse Method to Swap Lowest for Highest?

Why did the EU agree to delay the Brexit deadline?

Electoral considerations aside, what are potential benefits, for the US, of policy changes proposed by the tweet recognizing Golan annexation?

Travelling outside the UK without a passport

Why a symmetric relation is defined: ∀x∀y( xRy⟹yRx) and not ∀x∀y (xRy⟺yRx)?

Is it improper etiquette to ask your opponent what his/her rating is before the game?

What if a revenant (monster) gains fire resistance?

Not using 's' for he/she/it

Infinite dials to reset ever?



Changing last entries in a comma delimited list



2019 Community Moderator ElectionHow to replace line with file contents and around the content with some text?Comparing 3 lines and 2 lines in BASHHow do I get bash to give a relevant error for a file with windows line-endings?Extract strings from between tags and save to new text fileReplace first column of file by command outputRemove all duplicate word from string using shell scriptHow do I prepend a line read from STDIN with an epoch timestamp?Replace string in a huge (70GB), one line, text fileJoining entries based off of column using awk/join










8















I have a huge text file which look like this:



36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,3
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,8
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,14
36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,12


The desired output is this:



36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-03
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-08
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-14
36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,MI-12


I have tried other relevant posts here and on other communities but could not exactly get what I want.



UPDATE



This is the cross-question (I wanted both Unix/perl answers and batch/powershell solutions for this.) that has interesting answers.










share|improve this question




























    8















    I have a huge text file which look like this:



    36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,3
    36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,8
    36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,14
    36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,12


    The desired output is this:



    36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-03
    36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-08
    36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-14
    36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,MI-12


    I have tried other relevant posts here and on other communities but could not exactly get what I want.



    UPDATE



    This is the cross-question (I wanted both Unix/perl answers and batch/powershell solutions for this.) that has interesting answers.










    share|improve this question


























      8












      8








      8


      1






      I have a huge text file which look like this:



      36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,3
      36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,8
      36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,14
      36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,12


      The desired output is this:



      36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-03
      36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-08
      36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-14
      36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,MI-12


      I have tried other relevant posts here and on other communities but could not exactly get what I want.



      UPDATE



      This is the cross-question (I wanted both Unix/perl answers and batch/powershell solutions for this.) that has interesting answers.










      share|improve this question
















      I have a huge text file which look like this:



      36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,3
      36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,8
      36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,14
      36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,12


      The desired output is this:



      36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-03
      36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-08
      36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-14
      36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,MI-12


      I have tried other relevant posts here and on other communities but could not exactly get what I want.



      UPDATE



      This is the cross-question (I wanted both Unix/perl answers and batch/powershell solutions for this.) that has interesting answers.







      shell-script text-processing






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 12 at 22:00







      M-M

















      asked Apr 13 '17 at 18:56









      M-MM-M

      15818




      15818




















          5 Answers
          5






          active

          oldest

          votes


















          14














          awk approach with sprintf function(to add leading zeros):



          awk -F, -v OFS=',' '$8=sprintf("MI-%02d",$8);' file


          The output:



          36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-03
          36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-08
          36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-14
          36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,MI-12



          -F, - set comma , as field separator



          $8 - points to the eighth field



          %02d - format which treats function argument as 2-digit number




          Note, the last field in a record can be presented by $NF.




          NF is a predefined variable whose value is the number of fields in the current record




          So, $NF is the same as $8(for your input)



          awk -F, -v OFS=',' '$(NF)=sprintf("MI-%02d", $(NF))' file





          share|improve this answer




















          • 1





            You should use NF to refer to the last field rather than hardcoding after counting.

            – heemayl
            Apr 14 '17 at 5:02











          • @heemayl, added additional note

            – RomanPerekhrest
            Apr 14 '17 at 6:44






          • 1





            A word of warning (irrelevant in this exemple, but could apply in other cases) : changing a value of one of the fields (here: $8) "recomputes" the whole line's fields, and have side effets: ex1: loses 'multiple separators': echo "1   2 3    4" | awk '$2=$2;print $0' gives: 1 2 3 4 (only 1 space (or OFS) left between fields). ex2) echo "1,,,2,3,,,,4" | awk -F',' '$2=$2;print $0' gives: 1   2 3    4 (commas became spaces) . There could be other side effects. Test and take another approach (gsub on a copy variable of $0,for ex) if assiging a field have detrimental side effects.

            – Olivier Dulac
            Apr 14 '17 at 12:38



















          3














          You can try using awk:



          awk 'BEGIN FS = OFS = "," $NF = sprintf("MI-%02d", $NF); 1' file





          share|improve this answer
































            2














            Here's perl solution:



            $ perl -F',' -lane '$last=$#F;$F[$last]=sprintf("MI-%02d",$F[$last]);print join ",", @F' input.txt 
            36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-03
            36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-08
            36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-14
            36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,MI-12


            The -a flag allows us to treat input as array, based on separator specified with -F. Basically we alter last item in that array, and rebuild it via join command.






            share|improve this answer

























            • Thank you for your answer. It does help if someone needs perl but still sprintf is the core idea of your answer. Not like if it's not right, just not offering something different than accepted answer. +1 anyways.

              – M-M
              Apr 13 '17 at 20:47






            • 1





              @Masoud well, main reason here is because sprintf() is used typically when writing a string of specific format to a variable, which is why it is used in many other languages. I can write it in Python as well - Python doesn't have sprintf() but the core idea will be the same regardless - writing formatted string to a variable. Alternatively, we can operate on array items directly and just print those. With this type of questions there is finite amount of solutions, basically is what I'm trying to say

              – Sergiy Kolodyazhnyy
              Apr 13 '17 at 20:56











            • Agreed! Your point stands corrected.

              – M-M
              Apr 13 '17 at 21:04


















            1














            With input data like:



            36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,3 
            36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,8
            36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,14
            36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,12


            in text.csv



            the code below



            awk -F"," ' i = 0;
            MyOutLine = "";
            j = NF - 1;
            while ( i < j )
            i++;
            MyOutLine = MyOutLine""$i",";

            i++;
            x = sprintf( "%.2i", $i );
            y = "MI-"x;
            MyOutLine = MyOutLine""y;
            print MyOutLine; ' ./text.csv


            produces output like:



            36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-03
            36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-08
            36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-14
            36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,MI-12





            share|improve this answer

























            • Thank you for your answer. It works fine but I just decided to go with Roman's response since it's just easier to comprehend.

              – M-M
              Apr 13 '17 at 19:39


















            0














            Tcl



            Here is my solution, done using Tcl which reads from input.csv file and puts the result in output.csv file



            set in [open input.csv]
            set out [open output.csv w]

            while ![eof $in]
            set line [gets $in]
            set last_comma_pos [string last , $line]
            puts $out [string range $line 0 $last_comma_pos][format MI-%02d [string range $line $last_comma_pos+1 end]]


            close $in
            close $out


            demonstration






            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%2f358853%2fchanging-last-entries-in-a-comma-delimited-list%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              5 Answers
              5






              active

              oldest

              votes








              5 Answers
              5






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              14














              awk approach with sprintf function(to add leading zeros):



              awk -F, -v OFS=',' '$8=sprintf("MI-%02d",$8);' file


              The output:



              36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-03
              36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-08
              36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-14
              36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,MI-12



              -F, - set comma , as field separator



              $8 - points to the eighth field



              %02d - format which treats function argument as 2-digit number




              Note, the last field in a record can be presented by $NF.




              NF is a predefined variable whose value is the number of fields in the current record




              So, $NF is the same as $8(for your input)



              awk -F, -v OFS=',' '$(NF)=sprintf("MI-%02d", $(NF))' file





              share|improve this answer




















              • 1





                You should use NF to refer to the last field rather than hardcoding after counting.

                – heemayl
                Apr 14 '17 at 5:02











              • @heemayl, added additional note

                – RomanPerekhrest
                Apr 14 '17 at 6:44






              • 1





                A word of warning (irrelevant in this exemple, but could apply in other cases) : changing a value of one of the fields (here: $8) "recomputes" the whole line's fields, and have side effets: ex1: loses 'multiple separators': echo "1   2 3    4" | awk '$2=$2;print $0' gives: 1 2 3 4 (only 1 space (or OFS) left between fields). ex2) echo "1,,,2,3,,,,4" | awk -F',' '$2=$2;print $0' gives: 1   2 3    4 (commas became spaces) . There could be other side effects. Test and take another approach (gsub on a copy variable of $0,for ex) if assiging a field have detrimental side effects.

                – Olivier Dulac
                Apr 14 '17 at 12:38
















              14














              awk approach with sprintf function(to add leading zeros):



              awk -F, -v OFS=',' '$8=sprintf("MI-%02d",$8);' file


              The output:



              36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-03
              36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-08
              36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-14
              36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,MI-12



              -F, - set comma , as field separator



              $8 - points to the eighth field



              %02d - format which treats function argument as 2-digit number




              Note, the last field in a record can be presented by $NF.




              NF is a predefined variable whose value is the number of fields in the current record




              So, $NF is the same as $8(for your input)



              awk -F, -v OFS=',' '$(NF)=sprintf("MI-%02d", $(NF))' file





              share|improve this answer




















              • 1





                You should use NF to refer to the last field rather than hardcoding after counting.

                – heemayl
                Apr 14 '17 at 5:02











              • @heemayl, added additional note

                – RomanPerekhrest
                Apr 14 '17 at 6:44






              • 1





                A word of warning (irrelevant in this exemple, but could apply in other cases) : changing a value of one of the fields (here: $8) "recomputes" the whole line's fields, and have side effets: ex1: loses 'multiple separators': echo "1   2 3    4" | awk '$2=$2;print $0' gives: 1 2 3 4 (only 1 space (or OFS) left between fields). ex2) echo "1,,,2,3,,,,4" | awk -F',' '$2=$2;print $0' gives: 1   2 3    4 (commas became spaces) . There could be other side effects. Test and take another approach (gsub on a copy variable of $0,for ex) if assiging a field have detrimental side effects.

                – Olivier Dulac
                Apr 14 '17 at 12:38














              14












              14








              14







              awk approach with sprintf function(to add leading zeros):



              awk -F, -v OFS=',' '$8=sprintf("MI-%02d",$8);' file


              The output:



              36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-03
              36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-08
              36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-14
              36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,MI-12



              -F, - set comma , as field separator



              $8 - points to the eighth field



              %02d - format which treats function argument as 2-digit number




              Note, the last field in a record can be presented by $NF.




              NF is a predefined variable whose value is the number of fields in the current record




              So, $NF is the same as $8(for your input)



              awk -F, -v OFS=',' '$(NF)=sprintf("MI-%02d", $(NF))' file





              share|improve this answer















              awk approach with sprintf function(to add leading zeros):



              awk -F, -v OFS=',' '$8=sprintf("MI-%02d",$8);' file


              The output:



              36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-03
              36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-08
              36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-14
              36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,MI-12



              -F, - set comma , as field separator



              $8 - points to the eighth field



              %02d - format which treats function argument as 2-digit number




              Note, the last field in a record can be presented by $NF.




              NF is a predefined variable whose value is the number of fields in the current record




              So, $NF is the same as $8(for your input)



              awk -F, -v OFS=',' '$(NF)=sprintf("MI-%02d", $(NF))' file






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Apr 14 '17 at 6:47

























              answered Apr 13 '17 at 19:18









              RomanPerekhrestRomanPerekhrest

              23.2k12448




              23.2k12448







              • 1





                You should use NF to refer to the last field rather than hardcoding after counting.

                – heemayl
                Apr 14 '17 at 5:02











              • @heemayl, added additional note

                – RomanPerekhrest
                Apr 14 '17 at 6:44






              • 1





                A word of warning (irrelevant in this exemple, but could apply in other cases) : changing a value of one of the fields (here: $8) "recomputes" the whole line's fields, and have side effets: ex1: loses 'multiple separators': echo "1   2 3    4" | awk '$2=$2;print $0' gives: 1 2 3 4 (only 1 space (or OFS) left between fields). ex2) echo "1,,,2,3,,,,4" | awk -F',' '$2=$2;print $0' gives: 1   2 3    4 (commas became spaces) . There could be other side effects. Test and take another approach (gsub on a copy variable of $0,for ex) if assiging a field have detrimental side effects.

                – Olivier Dulac
                Apr 14 '17 at 12:38













              • 1





                You should use NF to refer to the last field rather than hardcoding after counting.

                – heemayl
                Apr 14 '17 at 5:02











              • @heemayl, added additional note

                – RomanPerekhrest
                Apr 14 '17 at 6:44






              • 1





                A word of warning (irrelevant in this exemple, but could apply in other cases) : changing a value of one of the fields (here: $8) "recomputes" the whole line's fields, and have side effets: ex1: loses 'multiple separators': echo "1   2 3    4" | awk '$2=$2;print $0' gives: 1 2 3 4 (only 1 space (or OFS) left between fields). ex2) echo "1,,,2,3,,,,4" | awk -F',' '$2=$2;print $0' gives: 1   2 3    4 (commas became spaces) . There could be other side effects. Test and take another approach (gsub on a copy variable of $0,for ex) if assiging a field have detrimental side effects.

                – Olivier Dulac
                Apr 14 '17 at 12:38








              1




              1





              You should use NF to refer to the last field rather than hardcoding after counting.

              – heemayl
              Apr 14 '17 at 5:02





              You should use NF to refer to the last field rather than hardcoding after counting.

              – heemayl
              Apr 14 '17 at 5:02













              @heemayl, added additional note

              – RomanPerekhrest
              Apr 14 '17 at 6:44





              @heemayl, added additional note

              – RomanPerekhrest
              Apr 14 '17 at 6:44




              1




              1





              A word of warning (irrelevant in this exemple, but could apply in other cases) : changing a value of one of the fields (here: $8) "recomputes" the whole line's fields, and have side effets: ex1: loses 'multiple separators': echo "1   2 3    4" | awk '$2=$2;print $0' gives: 1 2 3 4 (only 1 space (or OFS) left between fields). ex2) echo "1,,,2,3,,,,4" | awk -F',' '$2=$2;print $0' gives: 1   2 3    4 (commas became spaces) . There could be other side effects. Test and take another approach (gsub on a copy variable of $0,for ex) if assiging a field have detrimental side effects.

              – Olivier Dulac
              Apr 14 '17 at 12:38






              A word of warning (irrelevant in this exemple, but could apply in other cases) : changing a value of one of the fields (here: $8) "recomputes" the whole line's fields, and have side effets: ex1: loses 'multiple separators': echo "1   2 3    4" | awk '$2=$2;print $0' gives: 1 2 3 4 (only 1 space (or OFS) left between fields). ex2) echo "1,,,2,3,,,,4" | awk -F',' '$2=$2;print $0' gives: 1   2 3    4 (commas became spaces) . There could be other side effects. Test and take another approach (gsub on a copy variable of $0,for ex) if assiging a field have detrimental side effects.

              – Olivier Dulac
              Apr 14 '17 at 12:38














              3














              You can try using awk:



              awk 'BEGIN FS = OFS = "," $NF = sprintf("MI-%02d", $NF); 1' file





              share|improve this answer





























                3














                You can try using awk:



                awk 'BEGIN FS = OFS = "," $NF = sprintf("MI-%02d", $NF); 1' file





                share|improve this answer



























                  3












                  3








                  3







                  You can try using awk:



                  awk 'BEGIN FS = OFS = "," $NF = sprintf("MI-%02d", $NF); 1' file





                  share|improve this answer















                  You can try using awk:



                  awk 'BEGIN FS = OFS = "," $NF = sprintf("MI-%02d", $NF); 1' file






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Apr 13 '17 at 19:28

























                  answered Apr 13 '17 at 19:21









                  taliezintaliezin

                  6,88011728




                  6,88011728





















                      2














                      Here's perl solution:



                      $ perl -F',' -lane '$last=$#F;$F[$last]=sprintf("MI-%02d",$F[$last]);print join ",", @F' input.txt 
                      36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-03
                      36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-08
                      36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-14
                      36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,MI-12


                      The -a flag allows us to treat input as array, based on separator specified with -F. Basically we alter last item in that array, and rebuild it via join command.






                      share|improve this answer

























                      • Thank you for your answer. It does help if someone needs perl but still sprintf is the core idea of your answer. Not like if it's not right, just not offering something different than accepted answer. +1 anyways.

                        – M-M
                        Apr 13 '17 at 20:47






                      • 1





                        @Masoud well, main reason here is because sprintf() is used typically when writing a string of specific format to a variable, which is why it is used in many other languages. I can write it in Python as well - Python doesn't have sprintf() but the core idea will be the same regardless - writing formatted string to a variable. Alternatively, we can operate on array items directly and just print those. With this type of questions there is finite amount of solutions, basically is what I'm trying to say

                        – Sergiy Kolodyazhnyy
                        Apr 13 '17 at 20:56











                      • Agreed! Your point stands corrected.

                        – M-M
                        Apr 13 '17 at 21:04















                      2














                      Here's perl solution:



                      $ perl -F',' -lane '$last=$#F;$F[$last]=sprintf("MI-%02d",$F[$last]);print join ",", @F' input.txt 
                      36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-03
                      36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-08
                      36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-14
                      36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,MI-12


                      The -a flag allows us to treat input as array, based on separator specified with -F. Basically we alter last item in that array, and rebuild it via join command.






                      share|improve this answer

























                      • Thank you for your answer. It does help if someone needs perl but still sprintf is the core idea of your answer. Not like if it's not right, just not offering something different than accepted answer. +1 anyways.

                        – M-M
                        Apr 13 '17 at 20:47






                      • 1





                        @Masoud well, main reason here is because sprintf() is used typically when writing a string of specific format to a variable, which is why it is used in many other languages. I can write it in Python as well - Python doesn't have sprintf() but the core idea will be the same regardless - writing formatted string to a variable. Alternatively, we can operate on array items directly and just print those. With this type of questions there is finite amount of solutions, basically is what I'm trying to say

                        – Sergiy Kolodyazhnyy
                        Apr 13 '17 at 20:56











                      • Agreed! Your point stands corrected.

                        – M-M
                        Apr 13 '17 at 21:04













                      2












                      2








                      2







                      Here's perl solution:



                      $ perl -F',' -lane '$last=$#F;$F[$last]=sprintf("MI-%02d",$F[$last]);print join ",", @F' input.txt 
                      36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-03
                      36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-08
                      36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-14
                      36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,MI-12


                      The -a flag allows us to treat input as array, based on separator specified with -F. Basically we alter last item in that array, and rebuild it via join command.






                      share|improve this answer















                      Here's perl solution:



                      $ perl -F',' -lane '$last=$#F;$F[$last]=sprintf("MI-%02d",$F[$last]);print join ",", @F' input.txt 
                      36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-03
                      36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-08
                      36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-14
                      36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,MI-12


                      The -a flag allows us to treat input as array, based on separator specified with -F. Basically we alter last item in that array, and rebuild it via join command.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Apr 13 '17 at 20:44

























                      answered Apr 13 '17 at 20:13









                      Sergiy KolodyazhnyySergiy Kolodyazhnyy

                      10.6k42763




                      10.6k42763












                      • Thank you for your answer. It does help if someone needs perl but still sprintf is the core idea of your answer. Not like if it's not right, just not offering something different than accepted answer. +1 anyways.

                        – M-M
                        Apr 13 '17 at 20:47






                      • 1





                        @Masoud well, main reason here is because sprintf() is used typically when writing a string of specific format to a variable, which is why it is used in many other languages. I can write it in Python as well - Python doesn't have sprintf() but the core idea will be the same regardless - writing formatted string to a variable. Alternatively, we can operate on array items directly and just print those. With this type of questions there is finite amount of solutions, basically is what I'm trying to say

                        – Sergiy Kolodyazhnyy
                        Apr 13 '17 at 20:56











                      • Agreed! Your point stands corrected.

                        – M-M
                        Apr 13 '17 at 21:04

















                      • Thank you for your answer. It does help if someone needs perl but still sprintf is the core idea of your answer. Not like if it's not right, just not offering something different than accepted answer. +1 anyways.

                        – M-M
                        Apr 13 '17 at 20:47






                      • 1





                        @Masoud well, main reason here is because sprintf() is used typically when writing a string of specific format to a variable, which is why it is used in many other languages. I can write it in Python as well - Python doesn't have sprintf() but the core idea will be the same regardless - writing formatted string to a variable. Alternatively, we can operate on array items directly and just print those. With this type of questions there is finite amount of solutions, basically is what I'm trying to say

                        – Sergiy Kolodyazhnyy
                        Apr 13 '17 at 20:56











                      • Agreed! Your point stands corrected.

                        – M-M
                        Apr 13 '17 at 21:04
















                      Thank you for your answer. It does help if someone needs perl but still sprintf is the core idea of your answer. Not like if it's not right, just not offering something different than accepted answer. +1 anyways.

                      – M-M
                      Apr 13 '17 at 20:47





                      Thank you for your answer. It does help if someone needs perl but still sprintf is the core idea of your answer. Not like if it's not right, just not offering something different than accepted answer. +1 anyways.

                      – M-M
                      Apr 13 '17 at 20:47




                      1




                      1





                      @Masoud well, main reason here is because sprintf() is used typically when writing a string of specific format to a variable, which is why it is used in many other languages. I can write it in Python as well - Python doesn't have sprintf() but the core idea will be the same regardless - writing formatted string to a variable. Alternatively, we can operate on array items directly and just print those. With this type of questions there is finite amount of solutions, basically is what I'm trying to say

                      – Sergiy Kolodyazhnyy
                      Apr 13 '17 at 20:56





                      @Masoud well, main reason here is because sprintf() is used typically when writing a string of specific format to a variable, which is why it is used in many other languages. I can write it in Python as well - Python doesn't have sprintf() but the core idea will be the same regardless - writing formatted string to a variable. Alternatively, we can operate on array items directly and just print those. With this type of questions there is finite amount of solutions, basically is what I'm trying to say

                      – Sergiy Kolodyazhnyy
                      Apr 13 '17 at 20:56













                      Agreed! Your point stands corrected.

                      – M-M
                      Apr 13 '17 at 21:04





                      Agreed! Your point stands corrected.

                      – M-M
                      Apr 13 '17 at 21:04











                      1














                      With input data like:



                      36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,3 
                      36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,8
                      36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,14
                      36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,12


                      in text.csv



                      the code below



                      awk -F"," ' i = 0;
                      MyOutLine = "";
                      j = NF - 1;
                      while ( i < j )
                      i++;
                      MyOutLine = MyOutLine""$i",";

                      i++;
                      x = sprintf( "%.2i", $i );
                      y = "MI-"x;
                      MyOutLine = MyOutLine""y;
                      print MyOutLine; ' ./text.csv


                      produces output like:



                      36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-03
                      36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-08
                      36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-14
                      36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,MI-12





                      share|improve this answer

























                      • Thank you for your answer. It works fine but I just decided to go with Roman's response since it's just easier to comprehend.

                        – M-M
                        Apr 13 '17 at 19:39















                      1














                      With input data like:



                      36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,3 
                      36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,8
                      36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,14
                      36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,12


                      in text.csv



                      the code below



                      awk -F"," ' i = 0;
                      MyOutLine = "";
                      j = NF - 1;
                      while ( i < j )
                      i++;
                      MyOutLine = MyOutLine""$i",";

                      i++;
                      x = sprintf( "%.2i", $i );
                      y = "MI-"x;
                      MyOutLine = MyOutLine""y;
                      print MyOutLine; ' ./text.csv


                      produces output like:



                      36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-03
                      36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-08
                      36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-14
                      36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,MI-12





                      share|improve this answer

























                      • Thank you for your answer. It works fine but I just decided to go with Roman's response since it's just easier to comprehend.

                        – M-M
                        Apr 13 '17 at 19:39













                      1












                      1








                      1







                      With input data like:



                      36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,3 
                      36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,8
                      36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,14
                      36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,12


                      in text.csv



                      the code below



                      awk -F"," ' i = 0;
                      MyOutLine = "";
                      j = NF - 1;
                      while ( i < j )
                      i++;
                      MyOutLine = MyOutLine""$i",";

                      i++;
                      x = sprintf( "%.2i", $i );
                      y = "MI-"x;
                      MyOutLine = MyOutLine""y;
                      print MyOutLine; ' ./text.csv


                      produces output like:



                      36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-03
                      36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-08
                      36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-14
                      36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,MI-12





                      share|improve this answer















                      With input data like:



                      36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,3 
                      36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,8
                      36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,14
                      36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,12


                      in text.csv



                      the code below



                      awk -F"," ' i = 0;
                      MyOutLine = "";
                      j = NF - 1;
                      while ( i < j )
                      i++;
                      MyOutLine = MyOutLine""$i",";

                      i++;
                      x = sprintf( "%.2i", $i );
                      y = "MI-"x;
                      MyOutLine = MyOutLine""y;
                      print MyOutLine; ' ./text.csv


                      produces output like:



                      36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-03
                      36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-08
                      36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-14
                      36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,MI-12






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Apr 13 '17 at 19:28

























                      answered Apr 13 '17 at 19:21









                      NormNorm

                      92




                      92












                      • Thank you for your answer. It works fine but I just decided to go with Roman's response since it's just easier to comprehend.

                        – M-M
                        Apr 13 '17 at 19:39

















                      • Thank you for your answer. It works fine but I just decided to go with Roman's response since it's just easier to comprehend.

                        – M-M
                        Apr 13 '17 at 19:39
















                      Thank you for your answer. It works fine but I just decided to go with Roman's response since it's just easier to comprehend.

                      – M-M
                      Apr 13 '17 at 19:39





                      Thank you for your answer. It works fine but I just decided to go with Roman's response since it's just easier to comprehend.

                      – M-M
                      Apr 13 '17 at 19:39











                      0














                      Tcl



                      Here is my solution, done using Tcl which reads from input.csv file and puts the result in output.csv file



                      set in [open input.csv]
                      set out [open output.csv w]

                      while ![eof $in]
                      set line [gets $in]
                      set last_comma_pos [string last , $line]
                      puts $out [string range $line 0 $last_comma_pos][format MI-%02d [string range $line $last_comma_pos+1 end]]


                      close $in
                      close $out


                      demonstration






                      share|improve this answer



























                        0














                        Tcl



                        Here is my solution, done using Tcl which reads from input.csv file and puts the result in output.csv file



                        set in [open input.csv]
                        set out [open output.csv w]

                        while ![eof $in]
                        set line [gets $in]
                        set last_comma_pos [string last , $line]
                        puts $out [string range $line 0 $last_comma_pos][format MI-%02d [string range $line $last_comma_pos+1 end]]


                        close $in
                        close $out


                        demonstration






                        share|improve this answer

























                          0












                          0








                          0







                          Tcl



                          Here is my solution, done using Tcl which reads from input.csv file and puts the result in output.csv file



                          set in [open input.csv]
                          set out [open output.csv w]

                          while ![eof $in]
                          set line [gets $in]
                          set last_comma_pos [string last , $line]
                          puts $out [string range $line 0 $last_comma_pos][format MI-%02d [string range $line $last_comma_pos+1 end]]


                          close $in
                          close $out


                          demonstration






                          share|improve this answer













                          Tcl



                          Here is my solution, done using Tcl which reads from input.csv file and puts the result in output.csv file



                          set in [open input.csv]
                          set out [open output.csv w]

                          while ![eof $in]
                          set line [gets $in]
                          set last_comma_pos [string last , $line]
                          puts $out [string range $line 0 $last_comma_pos][format MI-%02d [string range $line $last_comma_pos+1 end]]


                          close $in
                          close $out


                          demonstration







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Apr 14 '17 at 0:47









                          sergiolsergiol

                          1013




                          1013



























                              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%2f358853%2fchanging-last-entries-in-a-comma-delimited-list%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







                              -shell-script, text-processing

                              Popular posts from this blog

                              Word for a person who has no opinion about whether god existsWord for having a definite opinion while simultaneously withholding judgment?What's the opposite of “newcomer? Is ”veteran" OK?What do you call an “atheist” who might believe in an afterlife?What's a word for someone who wants to voice opinions but not have them challenged?Word for someone who dismisses contrary opinions as irrational?Somone who thinks they are overly special/out of the ordinaryIs there a word, phrase or idiom for “a person who is incapable of thinking about the future”?The belief that a god is human-likeA word for a non-famous person/thing you have heard a lot aboutAdjective for a person who enjoys taking care of their appearance

                              What was this official D&D 3.5e Lovecraft-flavored rulebook?What was this set of RPG tools called?As a first-time DM should I let my players play complex character classes and roles?Nymph's Kiss and the RelationshipWhat was the name of this Cleric Prestige Class that shapes metal with its bare hands?Are the 3.5e Dragonlance books third party or official works?What's up with the domain Vile Darkness?What was this 80s book about RPGs?What was the name of this Werewolf band?What book had Rituals to “upgrade” animal companions to keep them viable at higher levels?What was this RPG that had rules for player-owned businesses?

                              2017 IndyCar Series Contents Series news Teams and drivers Schedule Season summary Footnotes References External links Navigation menu"INDYCAR: Initial 2018 bodywork concepts unveiled"the original"IndyCar confirms switch to Performance Friction brakes in 2017""AJ Foyt Racing will switch to Chevy"the original"Carlos Munoz, Conor Daly will drive for AJ Foyt Racing""Zach Veach's Indy 500 Debut Confirmed with Foyt""No mass exodus from Honda after Ganassi switch""Ex-F1 driver Sato joins Andretti Autosport for 2017 IndyCar season""IndyCar's Ryan Hunter-Reay, sponsor DHL paired through 2020""hhgregg and Andretti Autosport announce partnership for key races in 2016""INDYCAR: Rossi re-signs with Andretti"the original"McLaren Formula 1 - Fernando Alonso to race at Indy 500 with McLaren, Honda and Andretti Autosport""Shank will finally take part in Indy 500 with Harvey, Andretti | MotorSportsTalk""Andretti adds Jack Harvey to Indy 500 field""Ganassi switches to Honda power for 2017""INDYCAR: Chilton returns to Ganassi"the original"IndyCar silly season: Who's going where in 2017?""INDYCAR: Kanaan, NTT Data return to Ganassi"the original"Kimball to remain at Ganassi for 2017""Coyne confirms Bourdais for 2017 IndyCar season""Davison to sub for Bourdais in Indy 500"the original"Gutierrez confirmed for Detroit IndyCar debut""Gutierrez returns with Coyne for rest of 2017 season""Vautier to drive for Coyne at Texas"the original"INDYCAR: Coyne confirms Jones for 2017"the original"Pippa Mann returns to Coyne for Indy 500""Karam, Dreyer & Reinbold teaming up again for Indianapolis 500""Pigot to return to Ed Carpenter Racing""Hildebrand confirmed as full-time Ed Carpenter driver""Veach to replace injured Hildebrand at Barber"the originalNew Team Harding Racing Enters Chaves for 101st Indianapolis 500"Juncos Racing Announces Entry in 101st Running of the Indianapolis 500 :: Juncos Racing""Juncos confirms Pigot for Indy 500""Saavedra confirmed in Juncos' second 500 entry"the original"Lazier confirms Indy 500 run after son's USF2000 debut"the original"Claman DeMelo to race for RLLR at Sonoma"the original"Rahal signs Servia and ace engineer for 2017""IndyCar: Aleshin returns with Schmidt"the original"Aleshin replaced by Saavedra for Toronto""Jack Harvey will pilot SPM No. 7 car at Watkins Glen, Sonoma""Jay Howard confirmed in Tony Stewart's supported SPM Indy entry""INDYCAR: Newgarden to wave the flag at Penske"the original"Pagenaud opts for No. 1 in 2017"the original"Penske confirms Newgarden for 2017""Montoya to stay with Team Penske in 2017""Target leaving IndyCar after 27 seasons with Chip Ganassi""Cavin: IndyCar could see complete driver/team shakeup in 2017""End of the road for KV Racing?""KV Racing confirms closure, equipment sold to Juncos""Juncos confirms IndyCar Series entry"the original"Juncos readies IndyCar program, aims for '17 500"the original"Harding Racing to add Texas, Pocono to schedule"the original"Sato signs with Andretti Autosport for 2017""INDYCAR: Aleshin in Doubt at SPM"the original"Long Beach notebook: JR Hildebrand breaks hand""Hildebrand cleared to return at Phoenix"the original"Bourdais to undergo surgery on multiple fractures""Aleshin loses Schmidt Peterson IndyCar ride""Saavedra in at SPM for Pocono, Gateway"the original"Bourdais to make return at Gateway"the original"The IndyCar Grand Prix no longer is sponsored by Angie's List""2017 IndyCar Series rulebook""2017 Verizon IndyCar Series Official Rulebook"Official websiteeeeee