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?

How do I turn off a repeating trade?

Set multicolumn to a exact width

What are the penalties for overstaying in USA?

How would a drone work in centrifugal force generated "gravity"?

Does squid ink pasta bleed?

Can White Castle?

Unusual mail headers, evidence of an attempted attack. Have I been pwned?

Hand soldering SMD 1206 components

Swapping rooks in a 4x4 board

Should I prioritize my 401(k) over my student loans?

Why aren't (poly-)cotton tents more popular?

Is this one of the engines from the 9/11 aircraft?

STM Microcontroller burns every time

What is the origin of Scooby-Doo's name?

What reason would an alien civilization have for building a Dyson Sphere (or Swarm) if cheap Nuclear fusion is available?

If the world have massive single giant world tree can it stop earthquake?

Cascading Repair Costs following Blown Head Gasket on a 2004 Subaru Outback

Folding basket - is there such a thing?

What is the legal status of travelling with methadone in your carry-on?

Is adding a new player (or players) a DM decision, or a group decision?

Why do textbooks often include the solutions to odd or even numbered problems but not both?

Fill NAs in R with zero if the next valid data point is more than 2 intervals away

Why do some professors with PhDs leave their professorships to teach high school?

Has there been any indication at all that further negotiation between the UK and EU is possible?



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
























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

    – terdon
    Jun 6 at 13:09

















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
























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

    – terdon
    Jun 6 at 13:09













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
















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















share|improve this question













share|improve this question




share|improve this question








edited Jun 6 at 16:47









K7AAY

2,0951 gold badge10 silver badges29 bronze badges




2,0951 gold badge10 silver badges29 bronze badges










asked Jun 6 at 13:03









AshishAshish

161 bronze badge




161 bronze badge












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

    – terdon
    Jun 6 at 13:09

















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

    – terdon
    Jun 6 at 13:09
















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

– terdon
Jun 6 at 13:09





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

– terdon
Jun 6 at 13:09










3 Answers
3






active

oldest

votes


















8














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























    • This works for me. Thanks a lot everyone for help.

      – Ashish
      Jun 7 at 6:02


















    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
      Jun 6 at 18:56











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

      – RalfFriedl
      Jun 6 at 20:02













    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%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









    8














    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



























      8














      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

























        8












        8








        8







        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 Jun 6 at 13:27









        steeldriversteeldriver

        40k4 gold badges54 silver badges93 bronze badges




        40k4 gold badges54 silver badges93 bronze badges























            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 works for me. Thanks a lot everyone for help.

              – Ashish
              Jun 7 at 6:02















            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 works for me. Thanks a lot everyone for help.

              – Ashish
              Jun 7 at 6:02













            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 Jun 6 at 13:14









            terdonterdon

            137k33 gold badges283 silver badges459 bronze badges




            137k33 gold badges283 silver badges459 bronze badges












            • This works for me. Thanks a lot everyone for help.

              – Ashish
              Jun 7 at 6:02

















            • This works for me. Thanks a lot everyone for help.

              – Ashish
              Jun 7 at 6:02
















            This works for me. Thanks a lot everyone for help.

            – Ashish
            Jun 7 at 6:02





            This works for me. Thanks a lot everyone for help.

            – Ashish
            Jun 7 at 6:02











            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
              Jun 6 at 18:56











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

              – RalfFriedl
              Jun 6 at 20:02















            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
              Jun 6 at 18:56











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

              – RalfFriedl
              Jun 6 at 20:02













            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 Jun 6 at 18:22









            Praveen Kumar BSPraveen Kumar BS

            2,1282 gold badges3 silver badges11 bronze badges




            2,1282 gold badges3 silver badges11 bronze badges












            • 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
              Jun 6 at 18:56











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

              – RalfFriedl
              Jun 6 at 20:02

















            • 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
              Jun 6 at 18:56











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

              – RalfFriedl
              Jun 6 at 20:02
















            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
            Jun 6 at 18:56





            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
            Jun 6 at 18:56













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

            – RalfFriedl
            Jun 6 at 20:02





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

            – RalfFriedl
            Jun 6 at 20:02

















            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%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

            Wikipedia:Vital articles Мазмуну Biography - Өмүр баян Philosophy and psychology - Философия жана психология Religion - Дин Social sciences - Коомдук илимдер Language and literature - Тил жана адабият Science - Илим Technology - Технология Arts and recreation - Искусство жана эс алуу History and geography - Тарых жана география Навигация менюсу

            Bruxelas-Capital Índice Historia | Composición | Situación lingüística | Clima | Cidades irmandadas | Notas | Véxase tamén | Menú de navegacióneO uso das linguas en Bruxelas e a situación do neerlandés"Rexión de Bruxelas Capital"o orixinalSitio da rexiónPáxina de Bruselas no sitio da Oficina de Promoción Turística de Valonia e BruxelasMapa Interactivo da Rexión de Bruxelas-CapitaleeWorldCat332144929079854441105155190212ID28008674080552-90000 0001 0666 3698n94104302ID540940339365017018237

            What should I write in an apology letter, since I have decided not to join a company after accepting an offer letterShould I keep looking after accepting a job offer?What should I do when I've been verbally told I would get an offer letter, but still haven't gotten one after 4 weeks?Do I accept an offer from a company that I am not likely to join?New job hasn't confirmed starting date and I want to give current employer as much notice as possibleHow should I address my manager in my resignation letter?HR delayed background verification, now jobless as resignedNo email communication after accepting a formal written offer. How should I phrase the call?What should I do if after receiving a verbal offer letter I am informed that my written job offer is put on hold due to some internal issues?Should I inform the current employer that I am about to resign within 1-2 weeks since I have signed the offer letter and waiting for visa?What company will do, if I send their offer letter to another company