replace space and empty cells in csv file












0















I have a csv file that has 7 columns. It has empty cells and some spaces between cells. How can I replace the empty cells with NA and remove extra spaces? Thank you very much!



Here is what my file looks like, but here it seems to warp around when I copy and past it.



130070078,PPW0001,1,4,4HW             ,2,15.61943874
120040039,PPW0002,0,0, ,0,0
120040043,PPW0003,1,3,3WE ,1,14.43394935









share|improve this question

























  • Please be careful: if your CSV file contains spaces and commas (for example foo,"bar, baz",bar - it has two cells: foo, bar, baz and bar) it isn't easy to parse (and change) with sed or awk.

    – uzsolt
    Mar 12 '17 at 17:49











  • Thank you. based on @Cyrus previous comment (which now seems to have been removed). I did this followed by replace empty space with NA and it worked: sed 's/ *,/,/g' file1 | sed 's/,,/,NA,/g' > file2

    – Elham
    Mar 12 '17 at 20:24













  • @uzsolt2, how can I know if my file has this problem and how do I resolve it. Because I think one of my other files has this problem, so when I use awk to get one column printed (the last one in the file), it returns an empty column.

    – Elham
    Mar 15 '17 at 14:02











  • if the count of commas is greater then your number of columns. Or... many cases. The other question (how do resolve): I'm using "psv", "pipe separated values", the separator character is "|". It's rarely used character in texts or numbers :)

    – uzsolt
    Mar 15 '17 at 17:19
















0















I have a csv file that has 7 columns. It has empty cells and some spaces between cells. How can I replace the empty cells with NA and remove extra spaces? Thank you very much!



Here is what my file looks like, but here it seems to warp around when I copy and past it.



130070078,PPW0001,1,4,4HW             ,2,15.61943874
120040039,PPW0002,0,0, ,0,0
120040043,PPW0003,1,3,3WE ,1,14.43394935









share|improve this question

























  • Please be careful: if your CSV file contains spaces and commas (for example foo,"bar, baz",bar - it has two cells: foo, bar, baz and bar) it isn't easy to parse (and change) with sed or awk.

    – uzsolt
    Mar 12 '17 at 17:49











  • Thank you. based on @Cyrus previous comment (which now seems to have been removed). I did this followed by replace empty space with NA and it worked: sed 's/ *,/,/g' file1 | sed 's/,,/,NA,/g' > file2

    – Elham
    Mar 12 '17 at 20:24













  • @uzsolt2, how can I know if my file has this problem and how do I resolve it. Because I think one of my other files has this problem, so when I use awk to get one column printed (the last one in the file), it returns an empty column.

    – Elham
    Mar 15 '17 at 14:02











  • if the count of commas is greater then your number of columns. Or... many cases. The other question (how do resolve): I'm using "psv", "pipe separated values", the separator character is "|". It's rarely used character in texts or numbers :)

    – uzsolt
    Mar 15 '17 at 17:19














0












0








0








I have a csv file that has 7 columns. It has empty cells and some spaces between cells. How can I replace the empty cells with NA and remove extra spaces? Thank you very much!



Here is what my file looks like, but here it seems to warp around when I copy and past it.



130070078,PPW0001,1,4,4HW             ,2,15.61943874
120040039,PPW0002,0,0, ,0,0
120040043,PPW0003,1,3,3WE ,1,14.43394935









share|improve this question
















I have a csv file that has 7 columns. It has empty cells and some spaces between cells. How can I replace the empty cells with NA and remove extra spaces? Thank you very much!



Here is what my file looks like, but here it seems to warp around when I copy and past it.



130070078,PPW0001,1,4,4HW             ,2,15.61943874
120040039,PPW0002,0,0, ,0,0
120040043,PPW0003,1,3,3WE ,1,14.43394935






sed csv awk






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 12 '17 at 17:22









Cyrus

3,79611024




3,79611024










asked Mar 12 '17 at 16:45









ElhamElham

13




13













  • Please be careful: if your CSV file contains spaces and commas (for example foo,"bar, baz",bar - it has two cells: foo, bar, baz and bar) it isn't easy to parse (and change) with sed or awk.

    – uzsolt
    Mar 12 '17 at 17:49











  • Thank you. based on @Cyrus previous comment (which now seems to have been removed). I did this followed by replace empty space with NA and it worked: sed 's/ *,/,/g' file1 | sed 's/,,/,NA,/g' > file2

    – Elham
    Mar 12 '17 at 20:24













  • @uzsolt2, how can I know if my file has this problem and how do I resolve it. Because I think one of my other files has this problem, so when I use awk to get one column printed (the last one in the file), it returns an empty column.

    – Elham
    Mar 15 '17 at 14:02











  • if the count of commas is greater then your number of columns. Or... many cases. The other question (how do resolve): I'm using "psv", "pipe separated values", the separator character is "|". It's rarely used character in texts or numbers :)

    – uzsolt
    Mar 15 '17 at 17:19



















  • Please be careful: if your CSV file contains spaces and commas (for example foo,"bar, baz",bar - it has two cells: foo, bar, baz and bar) it isn't easy to parse (and change) with sed or awk.

    – uzsolt
    Mar 12 '17 at 17:49











  • Thank you. based on @Cyrus previous comment (which now seems to have been removed). I did this followed by replace empty space with NA and it worked: sed 's/ *,/,/g' file1 | sed 's/,,/,NA,/g' > file2

    – Elham
    Mar 12 '17 at 20:24













  • @uzsolt2, how can I know if my file has this problem and how do I resolve it. Because I think one of my other files has this problem, so when I use awk to get one column printed (the last one in the file), it returns an empty column.

    – Elham
    Mar 15 '17 at 14:02











  • if the count of commas is greater then your number of columns. Or... many cases. The other question (how do resolve): I'm using "psv", "pipe separated values", the separator character is "|". It's rarely used character in texts or numbers :)

    – uzsolt
    Mar 15 '17 at 17:19

















Please be careful: if your CSV file contains spaces and commas (for example foo,"bar, baz",bar - it has two cells: foo, bar, baz and bar) it isn't easy to parse (and change) with sed or awk.

– uzsolt
Mar 12 '17 at 17:49





Please be careful: if your CSV file contains spaces and commas (for example foo,"bar, baz",bar - it has two cells: foo, bar, baz and bar) it isn't easy to parse (and change) with sed or awk.

– uzsolt
Mar 12 '17 at 17:49













Thank you. based on @Cyrus previous comment (which now seems to have been removed). I did this followed by replace empty space with NA and it worked: sed 's/ *,/,/g' file1 | sed 's/,,/,NA,/g' > file2

– Elham
Mar 12 '17 at 20:24







Thank you. based on @Cyrus previous comment (which now seems to have been removed). I did this followed by replace empty space with NA and it worked: sed 's/ *,/,/g' file1 | sed 's/,,/,NA,/g' > file2

– Elham
Mar 12 '17 at 20:24















@uzsolt2, how can I know if my file has this problem and how do I resolve it. Because I think one of my other files has this problem, so when I use awk to get one column printed (the last one in the file), it returns an empty column.

– Elham
Mar 15 '17 at 14:02





@uzsolt2, how can I know if my file has this problem and how do I resolve it. Because I think one of my other files has this problem, so when I use awk to get one column printed (the last one in the file), it returns an empty column.

– Elham
Mar 15 '17 at 14:02













if the count of commas is greater then your number of columns. Or... many cases. The other question (how do resolve): I'm using "psv", "pipe separated values", the separator character is "|". It's rarely used character in texts or numbers :)

– uzsolt
Mar 15 '17 at 17:19





if the count of commas is greater then your number of columns. Or... many cases. The other question (how do resolve): I'm using "psv", "pipe separated values", the separator character is "|". It's rarely used character in texts or numbers :)

– uzsolt
Mar 15 '17 at 17:19










3 Answers
3






active

oldest

votes


















1














Using sed that will work for repeated empty fields as well:



sed ':l;s/,,/,NA,/;tl; s/[[:blank:]]*//g'


Or using awk:



awk '{i=0;while(i++<2){gsub(/,,/,",na,");gsub(/ /, "")}}1'



Input:



130070078,PPW0001,1,4,4HW         ,2,15.61943874
120040039,PPW0002,0,0, ,0,0
120040043,PPW0003,1,3, 3WE ,1,14.43394935
120040043,PPW0003,1 ,3,3WE ,1,14.43,,,3,,94,,,,9,,,,,35
120040043,PPW0003,0, 2, 3WE ,1,14.43,,,3,,94,,,,9,,,,,35






Output:



130070078,PPW0001,1,4,4HW,2,15.61943874
120040039,PPW0002,0,0,,0,0
120040043,PPW0003,1,3,3WE,1,14.43394935
120040043,PPW0003,1,3,3WE,1,14.43,NA,NA,3,NA,94,NA,NA,NA,9,NA,NA,NA,NA,35
120040043,PPW0003,0,2,3WE,1,14.43,NA,NA,3,NA,94,NA,NA,NA,9,NA,NA,NA,NA,35






share|improve this answer































    0














    Your answer:



    sed 's/ *,/,/g' file1 | sed 's/,,/,NA,/g' > file2


    To get 'NA' in the last field if blank:



    sed 's/ *,/,/g' file1 | sed 's/,,/,NA,/g' | awk -F, 'OFS="," {if ($NF == "") $NF = "NA"; print}'> file2


    You could also use :



    sed 's/,,/,NA,/g' file1 | tr -d ' ' | awk -F, 'OFS="," {if ($NF == "") $NF = "NA"; print}'





    share|improve this answer

































      0














      αғsнιη's answer worked for me, but I'd just like to explain it a bit.



      I was trying something like this:



      echo "1,,2,,,3,,,4,,,,5,,,,,,,,,,6" | sed 's/,,/,-,/g'


      Which outputs



      1,-,2,-,,3,-,,4,-,,-,5,-,,-,,-,,-,,-,6


      Because of the repeated empty fields the last comma is part of the first replacement and the start of the next desired replacement, so you just get every second empty field replaced.



      Now you could do something like:



      echo "1,,2,,,3,,,4,,,,5,,,,,,,,,,6" | sed -e 's/,,/,-,/g' -e 's/,,/,-,/g'


      or



      sed 's/,,/,-,/g;s/,,/,-,/g'


      Which will replace all the cells, as the second command will get the ones that are missed, but it's a bit messy.



      αғsнιη's command does essentially the same thing, using a label and a jump, which I was not aware you could do.



      sed ':MYLABEL; s/,,/,-,/g; t MYLABEL;'


      output:



      1,-,2,-,-,3,-,-,4,-,-,-,5,-,-,-,-,-,-,-,-,-,6


      So the first part of the command creates a label.



      Then we have the same substitution.



      Then we have the t command which means jump to label if the previous substitution command was successful.



      More information: http://www.grymoire.com/Unix/Sed.html#uh-59






      share|improve this answer























        Your Answer








        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "3"
        };
        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: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        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%2fsuperuser.com%2fquestions%2f1187941%2freplace-space-and-empty-cells-in-csv-file%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









        1














        Using sed that will work for repeated empty fields as well:



        sed ':l;s/,,/,NA,/;tl; s/[[:blank:]]*//g'


        Or using awk:



        awk '{i=0;while(i++<2){gsub(/,,/,",na,");gsub(/ /, "")}}1'



        Input:



        130070078,PPW0001,1,4,4HW         ,2,15.61943874
        120040039,PPW0002,0,0, ,0,0
        120040043,PPW0003,1,3, 3WE ,1,14.43394935
        120040043,PPW0003,1 ,3,3WE ,1,14.43,,,3,,94,,,,9,,,,,35
        120040043,PPW0003,0, 2, 3WE ,1,14.43,,,3,,94,,,,9,,,,,35






        Output:



        130070078,PPW0001,1,4,4HW,2,15.61943874
        120040039,PPW0002,0,0,,0,0
        120040043,PPW0003,1,3,3WE,1,14.43394935
        120040043,PPW0003,1,3,3WE,1,14.43,NA,NA,3,NA,94,NA,NA,NA,9,NA,NA,NA,NA,35
        120040043,PPW0003,0,2,3WE,1,14.43,NA,NA,3,NA,94,NA,NA,NA,9,NA,NA,NA,NA,35






        share|improve this answer




























          1














          Using sed that will work for repeated empty fields as well:



          sed ':l;s/,,/,NA,/;tl; s/[[:blank:]]*//g'


          Or using awk:



          awk '{i=0;while(i++<2){gsub(/,,/,",na,");gsub(/ /, "")}}1'



          Input:



          130070078,PPW0001,1,4,4HW         ,2,15.61943874
          120040039,PPW0002,0,0, ,0,0
          120040043,PPW0003,1,3, 3WE ,1,14.43394935
          120040043,PPW0003,1 ,3,3WE ,1,14.43,,,3,,94,,,,9,,,,,35
          120040043,PPW0003,0, 2, 3WE ,1,14.43,,,3,,94,,,,9,,,,,35






          Output:



          130070078,PPW0001,1,4,4HW,2,15.61943874
          120040039,PPW0002,0,0,,0,0
          120040043,PPW0003,1,3,3WE,1,14.43394935
          120040043,PPW0003,1,3,3WE,1,14.43,NA,NA,3,NA,94,NA,NA,NA,9,NA,NA,NA,NA,35
          120040043,PPW0003,0,2,3WE,1,14.43,NA,NA,3,NA,94,NA,NA,NA,9,NA,NA,NA,NA,35






          share|improve this answer


























            1












            1








            1







            Using sed that will work for repeated empty fields as well:



            sed ':l;s/,,/,NA,/;tl; s/[[:blank:]]*//g'


            Or using awk:



            awk '{i=0;while(i++<2){gsub(/,,/,",na,");gsub(/ /, "")}}1'



            Input:



            130070078,PPW0001,1,4,4HW         ,2,15.61943874
            120040039,PPW0002,0,0, ,0,0
            120040043,PPW0003,1,3, 3WE ,1,14.43394935
            120040043,PPW0003,1 ,3,3WE ,1,14.43,,,3,,94,,,,9,,,,,35
            120040043,PPW0003,0, 2, 3WE ,1,14.43,,,3,,94,,,,9,,,,,35






            Output:



            130070078,PPW0001,1,4,4HW,2,15.61943874
            120040039,PPW0002,0,0,,0,0
            120040043,PPW0003,1,3,3WE,1,14.43394935
            120040043,PPW0003,1,3,3WE,1,14.43,NA,NA,3,NA,94,NA,NA,NA,9,NA,NA,NA,NA,35
            120040043,PPW0003,0,2,3WE,1,14.43,NA,NA,3,NA,94,NA,NA,NA,9,NA,NA,NA,NA,35






            share|improve this answer













            Using sed that will work for repeated empty fields as well:



            sed ':l;s/,,/,NA,/;tl; s/[[:blank:]]*//g'


            Or using awk:



            awk '{i=0;while(i++<2){gsub(/,,/,",na,");gsub(/ /, "")}}1'



            Input:



            130070078,PPW0001,1,4,4HW         ,2,15.61943874
            120040039,PPW0002,0,0, ,0,0
            120040043,PPW0003,1,3, 3WE ,1,14.43394935
            120040043,PPW0003,1 ,3,3WE ,1,14.43,,,3,,94,,,,9,,,,,35
            120040043,PPW0003,0, 2, 3WE ,1,14.43,,,3,,94,,,,9,,,,,35






            Output:



            130070078,PPW0001,1,4,4HW,2,15.61943874
            120040039,PPW0002,0,0,,0,0
            120040043,PPW0003,1,3,3WE,1,14.43394935
            120040043,PPW0003,1,3,3WE,1,14.43,NA,NA,3,NA,94,NA,NA,NA,9,NA,NA,NA,NA,35
            120040043,PPW0003,0,2,3WE,1,14.43,NA,NA,3,NA,94,NA,NA,NA,9,NA,NA,NA,NA,35







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Oct 20 '17 at 7:42









            αғsнιηαғsнιη

            3841217




            3841217

























                0














                Your answer:



                sed 's/ *,/,/g' file1 | sed 's/,,/,NA,/g' > file2


                To get 'NA' in the last field if blank:



                sed 's/ *,/,/g' file1 | sed 's/,,/,NA,/g' | awk -F, 'OFS="," {if ($NF == "") $NF = "NA"; print}'> file2


                You could also use :



                sed 's/,,/,NA,/g' file1 | tr -d ' ' | awk -F, 'OFS="," {if ($NF == "") $NF = "NA"; print}'





                share|improve this answer






























                  0














                  Your answer:



                  sed 's/ *,/,/g' file1 | sed 's/,,/,NA,/g' > file2


                  To get 'NA' in the last field if blank:



                  sed 's/ *,/,/g' file1 | sed 's/,,/,NA,/g' | awk -F, 'OFS="," {if ($NF == "") $NF = "NA"; print}'> file2


                  You could also use :



                  sed 's/,,/,NA,/g' file1 | tr -d ' ' | awk -F, 'OFS="," {if ($NF == "") $NF = "NA"; print}'





                  share|improve this answer




























                    0












                    0








                    0







                    Your answer:



                    sed 's/ *,/,/g' file1 | sed 's/,,/,NA,/g' > file2


                    To get 'NA' in the last field if blank:



                    sed 's/ *,/,/g' file1 | sed 's/,,/,NA,/g' | awk -F, 'OFS="," {if ($NF == "") $NF = "NA"; print}'> file2


                    You could also use :



                    sed 's/,,/,NA,/g' file1 | tr -d ' ' | awk -F, 'OFS="," {if ($NF == "") $NF = "NA"; print}'





                    share|improve this answer















                    Your answer:



                    sed 's/ *,/,/g' file1 | sed 's/,,/,NA,/g' > file2


                    To get 'NA' in the last field if blank:



                    sed 's/ *,/,/g' file1 | sed 's/,,/,NA,/g' | awk -F, 'OFS="," {if ($NF == "") $NF = "NA"; print}'> file2


                    You could also use :



                    sed 's/,,/,NA,/g' file1 | tr -d ' ' | awk -F, 'OFS="," {if ($NF == "") $NF = "NA"; print}'






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Apr 29 '17 at 23:05









                    Stephen Rauch

                    2,27581725




                    2,27581725










                    answered Apr 29 '17 at 22:16









                    bstipebstipe

                    1113




                    1113























                        0














                        αғsнιη's answer worked for me, but I'd just like to explain it a bit.



                        I was trying something like this:



                        echo "1,,2,,,3,,,4,,,,5,,,,,,,,,,6" | sed 's/,,/,-,/g'


                        Which outputs



                        1,-,2,-,,3,-,,4,-,,-,5,-,,-,,-,,-,,-,6


                        Because of the repeated empty fields the last comma is part of the first replacement and the start of the next desired replacement, so you just get every second empty field replaced.



                        Now you could do something like:



                        echo "1,,2,,,3,,,4,,,,5,,,,,,,,,,6" | sed -e 's/,,/,-,/g' -e 's/,,/,-,/g'


                        or



                        sed 's/,,/,-,/g;s/,,/,-,/g'


                        Which will replace all the cells, as the second command will get the ones that are missed, but it's a bit messy.



                        αғsнιη's command does essentially the same thing, using a label and a jump, which I was not aware you could do.



                        sed ':MYLABEL; s/,,/,-,/g; t MYLABEL;'


                        output:



                        1,-,2,-,-,3,-,-,4,-,-,-,5,-,-,-,-,-,-,-,-,-,6


                        So the first part of the command creates a label.



                        Then we have the same substitution.



                        Then we have the t command which means jump to label if the previous substitution command was successful.



                        More information: http://www.grymoire.com/Unix/Sed.html#uh-59






                        share|improve this answer




























                          0














                          αғsнιη's answer worked for me, but I'd just like to explain it a bit.



                          I was trying something like this:



                          echo "1,,2,,,3,,,4,,,,5,,,,,,,,,,6" | sed 's/,,/,-,/g'


                          Which outputs



                          1,-,2,-,,3,-,,4,-,,-,5,-,,-,,-,,-,,-,6


                          Because of the repeated empty fields the last comma is part of the first replacement and the start of the next desired replacement, so you just get every second empty field replaced.



                          Now you could do something like:



                          echo "1,,2,,,3,,,4,,,,5,,,,,,,,,,6" | sed -e 's/,,/,-,/g' -e 's/,,/,-,/g'


                          or



                          sed 's/,,/,-,/g;s/,,/,-,/g'


                          Which will replace all the cells, as the second command will get the ones that are missed, but it's a bit messy.



                          αғsнιη's command does essentially the same thing, using a label and a jump, which I was not aware you could do.



                          sed ':MYLABEL; s/,,/,-,/g; t MYLABEL;'


                          output:



                          1,-,2,-,-,3,-,-,4,-,-,-,5,-,-,-,-,-,-,-,-,-,6


                          So the first part of the command creates a label.



                          Then we have the same substitution.



                          Then we have the t command which means jump to label if the previous substitution command was successful.



                          More information: http://www.grymoire.com/Unix/Sed.html#uh-59






                          share|improve this answer


























                            0












                            0








                            0







                            αғsнιη's answer worked for me, but I'd just like to explain it a bit.



                            I was trying something like this:



                            echo "1,,2,,,3,,,4,,,,5,,,,,,,,,,6" | sed 's/,,/,-,/g'


                            Which outputs



                            1,-,2,-,,3,-,,4,-,,-,5,-,,-,,-,,-,,-,6


                            Because of the repeated empty fields the last comma is part of the first replacement and the start of the next desired replacement, so you just get every second empty field replaced.



                            Now you could do something like:



                            echo "1,,2,,,3,,,4,,,,5,,,,,,,,,,6" | sed -e 's/,,/,-,/g' -e 's/,,/,-,/g'


                            or



                            sed 's/,,/,-,/g;s/,,/,-,/g'


                            Which will replace all the cells, as the second command will get the ones that are missed, but it's a bit messy.



                            αғsнιη's command does essentially the same thing, using a label and a jump, which I was not aware you could do.



                            sed ':MYLABEL; s/,,/,-,/g; t MYLABEL;'


                            output:



                            1,-,2,-,-,3,-,-,4,-,-,-,5,-,-,-,-,-,-,-,-,-,6


                            So the first part of the command creates a label.



                            Then we have the same substitution.



                            Then we have the t command which means jump to label if the previous substitution command was successful.



                            More information: http://www.grymoire.com/Unix/Sed.html#uh-59






                            share|improve this answer













                            αғsнιη's answer worked for me, but I'd just like to explain it a bit.



                            I was trying something like this:



                            echo "1,,2,,,3,,,4,,,,5,,,,,,,,,,6" | sed 's/,,/,-,/g'


                            Which outputs



                            1,-,2,-,,3,-,,4,-,,-,5,-,,-,,-,,-,,-,6


                            Because of the repeated empty fields the last comma is part of the first replacement and the start of the next desired replacement, so you just get every second empty field replaced.



                            Now you could do something like:



                            echo "1,,2,,,3,,,4,,,,5,,,,,,,,,,6" | sed -e 's/,,/,-,/g' -e 's/,,/,-,/g'


                            or



                            sed 's/,,/,-,/g;s/,,/,-,/g'


                            Which will replace all the cells, as the second command will get the ones that are missed, but it's a bit messy.



                            αғsнιη's command does essentially the same thing, using a label and a jump, which I was not aware you could do.



                            sed ':MYLABEL; s/,,/,-,/g; t MYLABEL;'


                            output:



                            1,-,2,-,-,3,-,-,4,-,-,-,5,-,-,-,-,-,-,-,-,-,6


                            So the first part of the command creates a label.



                            Then we have the same substitution.



                            Then we have the t command which means jump to label if the previous substitution command was successful.



                            More information: http://www.grymoire.com/Unix/Sed.html#uh-59







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Dec 18 '18 at 10:36









                            Jack CulhaneJack Culhane

                            1011




                            1011






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Super User!


                                • 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%2fsuperuser.com%2fquestions%2f1187941%2freplace-space-and-empty-cells-in-csv-file%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Сан-Квентин

                                8-я гвардейская общевойсковая армия

                                Алькесар