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

                              Frič See also Navigation menuinternal link

                              Identify plant with long narrow paired leaves and reddish stems Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?What is this plant with long sharp leaves? Is it a weed?What is this 3ft high, stalky plant, with mid sized narrow leaves?What is this young shrub with opposite ovate, crenate leaves and reddish stems?What is this plant with large broad serrated leaves?Identify this upright branching weed with long leaves and reddish stemsPlease help me identify this bulbous plant with long, broad leaves and white flowersWhat is this small annual with narrow gray/green leaves and rust colored daisy-type flowers?What is this chilli plant?Does anyone know what type of chilli plant this is?Help identify this plant

                              fontconfig warning: “/etc/fonts/fonts.conf”, line 100: unknown “element blank” The 2019 Stack Overflow Developer Survey Results Are In“tar: unrecognized option --warning” during 'apt-get install'How to fix Fontconfig errorHow do I figure out which font file is chosen for a system generic font alias?Why are some apt-get-installed fonts being ignored by fc-list, xfontsel, etc?Reload settings in /etc/fonts/conf.dTaking 30 seconds longer to boot after upgrade from jessie to stretchHow to match multiple font names with a single <match> element?Adding a custom font to fontconfigRemoving fonts from fontconfig <match> resultsBroken fonts after upgrading Firefox ESR to latest Firefox