CSV how to trim values to 2 places in multiple columns using UNIXre-arrange values in one column without affecting other columns using awk or sedUsing jq to extract values and format in CSVHow to Trim below highlighted lines from Shell outputConcatenate CSV with some shared columnsRemove specific columns from csv using awkhow to capture only the “csv” lines with 5 valuesPattern recognition and summing columns between two csv/excel filesHow do I use different replacement texts for a pattern found multiple timesParsing CSV that has multiple line values with carriage returnsHow to Merge Multiple Columns in to Two Columns based on Column 1 Value?

bmatrix: how to align elements' subscripts?

How to safely destroy (a large quantity of) valid checks?

Why can my keyboard only digest 6 keypresses at a time?

How can I get an unreasonable manager to approve time off?

US doctor working in Tripoli wants me to open online account

Why are trash cans referred to as "zafacón" in Puerto Rico?

Why can I traceroute to this IP address, but not ping?

Longest bridge/tunnel that can be cycled over/through?

Why was this person allowed to become Grand Maester?

How can I end combat quickly when the outcome is inevitable?

What ways have you found to get edits from non-LaTeX users?

If I leave the US through an airport, do I have to return through the same airport?

Should I ask for an extra raise?

Let M and N be single-digit integers. If the product 2M5 x 13N is divisible by 36, how many ordered pairs (M,N) are possible?

Is it legal for a bar bouncer to confiscate a fake ID

Why am I getting a strange double quote (“) in Open Office instead of the ordinary one (")?

sed + add word before string only if not exists

Who enforces MPAA rating adherence?

Who won a Game of Bar Dice?

A strange trigonometric identity

Is using 'echo' to display attacker-controlled data on the terminal dangerous?

How to hide an urban landmark?

Interval of parallel 5ths in the resolution of a German 6th chord

ed command: Delete from line 1 until the first blank line



CSV how to trim values to 2 places in multiple columns using UNIX


re-arrange values in one column without affecting other columns using awk or sedUsing jq to extract values and format in CSVHow to Trim below highlighted lines from Shell outputConcatenate CSV with some shared columnsRemove specific columns from csv using awkhow to capture only the “csv” lines with 5 valuesPattern recognition and summing columns between two csv/excel filesHow do I use different replacement texts for a pattern found multiple timesParsing CSV that has multiple line values with carriage returnsHow to Merge Multiple Columns in to Two Columns based on Column 1 Value?






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








3















The sample file structure is given below



Product,Base_Price,Promotion_Price,Discount
Shampoo,1.999999,1.409999,15.9988999
Biscuit,2.999999,2.409999,15.9988999


The output file is expected to be in the format below:



Product,Base_Price,Promotion_Price,Discount
Shampoo,1.99,1.40,15.99
Biscuit,2.99,2.40,15.99









share|improve this question









New contributor



Ashish is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • Do you really mean UNIX or are you running Linux? If UNIX, do you have access to GNU tools?

    – terdon
    9 hours ago

















3















The sample file structure is given below



Product,Base_Price,Promotion_Price,Discount
Shampoo,1.999999,1.409999,15.9988999
Biscuit,2.999999,2.409999,15.9988999


The output file is expected to be in the format below:



Product,Base_Price,Promotion_Price,Discount
Shampoo,1.99,1.40,15.99
Biscuit,2.99,2.40,15.99









share|improve this question









New contributor



Ashish is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • Do you really mean UNIX or are you running Linux? If UNIX, do you have access to GNU tools?

    – terdon
    9 hours ago













3












3








3








The sample file structure is given below



Product,Base_Price,Promotion_Price,Discount
Shampoo,1.999999,1.409999,15.9988999
Biscuit,2.999999,2.409999,15.9988999


The output file is expected to be in the format below:



Product,Base_Price,Promotion_Price,Discount
Shampoo,1.99,1.40,15.99
Biscuit,2.99,2.40,15.99









share|improve this question









New contributor



Ashish is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











The sample file structure is given below



Product,Base_Price,Promotion_Price,Discount
Shampoo,1.999999,1.409999,15.9988999
Biscuit,2.999999,2.409999,15.9988999


The output file is expected to be in the format below:



Product,Base_Price,Promotion_Price,Discount
Shampoo,1.99,1.40,15.99
Biscuit,2.99,2.40,15.99






text-processing sed csv






share|improve this question









New contributor



Ashish is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.










share|improve this question









New contributor



Ashish is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








share|improve this question




share|improve this question








edited 6 hours ago









K7AAY

1,8731029




1,8731029






New contributor



Ashish is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








asked 9 hours ago









AshishAshish

161




161




New contributor



Ashish is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




New contributor




Ashish is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.














  • Do you really mean UNIX or are you running Linux? If UNIX, do you have access to GNU tools?

    – terdon
    9 hours ago

















  • Do you really mean UNIX or are you running Linux? If UNIX, do you have access to GNU tools?

    – terdon
    9 hours ago
















Do you really mean UNIX or are you running Linux? If UNIX, do you have access to GNU tools?

– terdon
9 hours ago





Do you really mean UNIX or are you running Linux? If UNIX, do you have access to GNU tools?

– terdon
9 hours ago










3 Answers
3






active

oldest

votes


















6














On systems with GNU Coreutils, you might consider using numfmt e.g.



$ numfmt --delimiter=, --header --field=2- --format='%.2f' --round=down < file.csv 
Product,Base_Price,Promotion_Price,Discount
Shampoo,1.99,1.40,15.99
Biscuit,2.99,2.40,15.99


If you want conventional IEEE from-zero rounding, omit the --round=down directive.






share|improve this answer






























    5














    This does what you asked for:



    $ sed -E 's/([0-9]+.[0-9]2)[0-9]*/1/g' file.csv 
    Product,Base_Price,Promotion_Price,Discount
    Shampoo,1.99,1.40,15.99
    Biscuit,2.99,2.40,15.99


    Or, if you want to round the numbers instead of just removing the extra digits, you could try:



    $ perl -pe 's/(d+.d+)/sprintf("%0.2f",$1)/ge' file.csv 
    Product,Base_Price,Promotion_Price,Discount
    Shampoo,2.00,1.41,16.00
    Biscuit,3.00,2.41,16.00





    share|improve this answer






























      1














       awk -F "," 'NR>1print $1,$2,$3,$4' y.txt |awk 'print $1,substr($2,1,4),substr($3,1,4),substr($4,1,5)'|awk 'OFS="," print $1,$2,$3,$41' filename

      Product,Base_Price,Promotion_Price,Discount
      Shampoo,1.99,1.40,15.99
      Biscuit,2.99,2.40,15.99





      share|improve this answer























      • I'd advise caution here, as it assumes that the numbers will always be the same scale (as it hard-codes the substring length to prune).

        – Jeff Schaller
        3 hours ago











      • This seems overly complicated, not to mention that the last awk is given an input file and ignores the pipe.

        – RalfFriedl
        2 hours ago











      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
      );



      );






      Ashish is a new contributor. Be nice, and check out our Code of Conduct.









      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f523314%2fcsv-how-to-trim-values-to-2-places-in-multiple-columns-using-unix%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      6














      On systems with GNU Coreutils, you might consider using numfmt e.g.



      $ numfmt --delimiter=, --header --field=2- --format='%.2f' --round=down < file.csv 
      Product,Base_Price,Promotion_Price,Discount
      Shampoo,1.99,1.40,15.99
      Biscuit,2.99,2.40,15.99


      If you want conventional IEEE from-zero rounding, omit the --round=down directive.






      share|improve this answer



























        6














        On systems with GNU Coreutils, you might consider using numfmt e.g.



        $ numfmt --delimiter=, --header --field=2- --format='%.2f' --round=down < file.csv 
        Product,Base_Price,Promotion_Price,Discount
        Shampoo,1.99,1.40,15.99
        Biscuit,2.99,2.40,15.99


        If you want conventional IEEE from-zero rounding, omit the --round=down directive.






        share|improve this answer

























          6












          6








          6







          On systems with GNU Coreutils, you might consider using numfmt e.g.



          $ numfmt --delimiter=, --header --field=2- --format='%.2f' --round=down < file.csv 
          Product,Base_Price,Promotion_Price,Discount
          Shampoo,1.99,1.40,15.99
          Biscuit,2.99,2.40,15.99


          If you want conventional IEEE from-zero rounding, omit the --round=down directive.






          share|improve this answer













          On systems with GNU Coreutils, you might consider using numfmt e.g.



          $ numfmt --delimiter=, --header --field=2- --format='%.2f' --round=down < file.csv 
          Product,Base_Price,Promotion_Price,Discount
          Shampoo,1.99,1.40,15.99
          Biscuit,2.99,2.40,15.99


          If you want conventional IEEE from-zero rounding, omit the --round=down directive.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 9 hours ago









          steeldriversteeldriver

          39.6k45492




          39.6k45492























              5














              This does what you asked for:



              $ sed -E 's/([0-9]+.[0-9]2)[0-9]*/1/g' file.csv 
              Product,Base_Price,Promotion_Price,Discount
              Shampoo,1.99,1.40,15.99
              Biscuit,2.99,2.40,15.99


              Or, if you want to round the numbers instead of just removing the extra digits, you could try:



              $ perl -pe 's/(d+.d+)/sprintf("%0.2f",$1)/ge' file.csv 
              Product,Base_Price,Promotion_Price,Discount
              Shampoo,2.00,1.41,16.00
              Biscuit,3.00,2.41,16.00





              share|improve this answer



























                5














                This does what you asked for:



                $ sed -E 's/([0-9]+.[0-9]2)[0-9]*/1/g' file.csv 
                Product,Base_Price,Promotion_Price,Discount
                Shampoo,1.99,1.40,15.99
                Biscuit,2.99,2.40,15.99


                Or, if you want to round the numbers instead of just removing the extra digits, you could try:



                $ perl -pe 's/(d+.d+)/sprintf("%0.2f",$1)/ge' file.csv 
                Product,Base_Price,Promotion_Price,Discount
                Shampoo,2.00,1.41,16.00
                Biscuit,3.00,2.41,16.00





                share|improve this answer

























                  5












                  5








                  5







                  This does what you asked for:



                  $ sed -E 's/([0-9]+.[0-9]2)[0-9]*/1/g' file.csv 
                  Product,Base_Price,Promotion_Price,Discount
                  Shampoo,1.99,1.40,15.99
                  Biscuit,2.99,2.40,15.99


                  Or, if you want to round the numbers instead of just removing the extra digits, you could try:



                  $ perl -pe 's/(d+.d+)/sprintf("%0.2f",$1)/ge' file.csv 
                  Product,Base_Price,Promotion_Price,Discount
                  Shampoo,2.00,1.41,16.00
                  Biscuit,3.00,2.41,16.00





                  share|improve this answer













                  This does what you asked for:



                  $ sed -E 's/([0-9]+.[0-9]2)[0-9]*/1/g' file.csv 
                  Product,Base_Price,Promotion_Price,Discount
                  Shampoo,1.99,1.40,15.99
                  Biscuit,2.99,2.40,15.99


                  Or, if you want to round the numbers instead of just removing the extra digits, you could try:



                  $ perl -pe 's/(d+.d+)/sprintf("%0.2f",$1)/ge' file.csv 
                  Product,Base_Price,Promotion_Price,Discount
                  Shampoo,2.00,1.41,16.00
                  Biscuit,3.00,2.41,16.00






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 9 hours ago









                  terdonterdon

                  136k33276457




                  136k33276457





















                      1














                       awk -F "," 'NR>1print $1,$2,$3,$4' y.txt |awk 'print $1,substr($2,1,4),substr($3,1,4),substr($4,1,5)'|awk 'OFS="," print $1,$2,$3,$41' filename

                      Product,Base_Price,Promotion_Price,Discount
                      Shampoo,1.99,1.40,15.99
                      Biscuit,2.99,2.40,15.99





                      share|improve this answer























                      • I'd advise caution here, as it assumes that the numbers will always be the same scale (as it hard-codes the substring length to prune).

                        – Jeff Schaller
                        3 hours ago











                      • This seems overly complicated, not to mention that the last awk is given an input file and ignores the pipe.

                        – RalfFriedl
                        2 hours ago















                      1














                       awk -F "," 'NR>1print $1,$2,$3,$4' y.txt |awk 'print $1,substr($2,1,4),substr($3,1,4),substr($4,1,5)'|awk 'OFS="," print $1,$2,$3,$41' filename

                      Product,Base_Price,Promotion_Price,Discount
                      Shampoo,1.99,1.40,15.99
                      Biscuit,2.99,2.40,15.99





                      share|improve this answer























                      • I'd advise caution here, as it assumes that the numbers will always be the same scale (as it hard-codes the substring length to prune).

                        – Jeff Schaller
                        3 hours ago











                      • This seems overly complicated, not to mention that the last awk is given an input file and ignores the pipe.

                        – RalfFriedl
                        2 hours ago













                      1












                      1








                      1







                       awk -F "," 'NR>1print $1,$2,$3,$4' y.txt |awk 'print $1,substr($2,1,4),substr($3,1,4),substr($4,1,5)'|awk 'OFS="," print $1,$2,$3,$41' filename

                      Product,Base_Price,Promotion_Price,Discount
                      Shampoo,1.99,1.40,15.99
                      Biscuit,2.99,2.40,15.99





                      share|improve this answer













                       awk -F "," 'NR>1print $1,$2,$3,$4' y.txt |awk 'print $1,substr($2,1,4),substr($3,1,4),substr($4,1,5)'|awk 'OFS="," print $1,$2,$3,$41' filename

                      Product,Base_Price,Promotion_Price,Discount
                      Shampoo,1.99,1.40,15.99
                      Biscuit,2.99,2.40,15.99






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered 4 hours ago









                      Praveen Kumar BSPraveen Kumar BS

                      2,0432311




                      2,0432311












                      • I'd advise caution here, as it assumes that the numbers will always be the same scale (as it hard-codes the substring length to prune).

                        – Jeff Schaller
                        3 hours ago











                      • This seems overly complicated, not to mention that the last awk is given an input file and ignores the pipe.

                        – RalfFriedl
                        2 hours ago

















                      • I'd advise caution here, as it assumes that the numbers will always be the same scale (as it hard-codes the substring length to prune).

                        – Jeff Schaller
                        3 hours ago











                      • This seems overly complicated, not to mention that the last awk is given an input file and ignores the pipe.

                        – RalfFriedl
                        2 hours ago
















                      I'd advise caution here, as it assumes that the numbers will always be the same scale (as it hard-codes the substring length to prune).

                      – Jeff Schaller
                      3 hours ago





                      I'd advise caution here, as it assumes that the numbers will always be the same scale (as it hard-codes the substring length to prune).

                      – Jeff Schaller
                      3 hours ago













                      This seems overly complicated, not to mention that the last awk is given an input file and ignores the pipe.

                      – RalfFriedl
                      2 hours ago





                      This seems overly complicated, not to mention that the last awk is given an input file and ignores the pipe.

                      – RalfFriedl
                      2 hours ago










                      Ashish is a new contributor. Be nice, and check out our Code of Conduct.









                      draft saved

                      draft discarded


















                      Ashish is a new contributor. Be nice, and check out our Code of Conduct.












                      Ashish is a new contributor. Be nice, and check out our Code of Conduct.











                      Ashish is a new contributor. Be nice, and check out our Code of Conduct.














                      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%2f523314%2fcsv-how-to-trim-values-to-2-places-in-multiple-columns-using-unix%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

                      Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

                      François Viète Contents Biography Work and thought Bibliography See also Notes Further reading External links Navigation menup. 21Google Bookspp. 75–77Google BooksDe thou (from University of Saint Andrews)ArchivedGoogle BooksGoogle BooksGoogle BooksGoogle booksGoogle Bookscc-parthenay.frL'histoire universelle (fr)Universal History (en)ArchivedAdsabs.harvard.eduPagesperso-orange.frArchive.orgChikara Sasaki. Descartes' mathematical thought p.259Google BooksGoogle BooksGoogle Bookspp. 152 and onwardGoogle BooksGoogle BooksScribd.comGoogle Books1257-7979Google BooksGoogle BooksGoogle BooksGoogle BooksGoogle BooksGoogle BooksGallica.bnf.frGoogle BooksGoogle Books"François Viète"Francois Viète: Father of Modern Algebraic NotationThe Lawyer and the GamblerAbout TarporleySite de Jean-Paul GuichardL'algèbre nouvelle"About the Harmonicon"cb120511976(data)1188044800000 0001 0913 5903n82164680ola2013766880073431702w6vt1sb70287374827140948071409480