CSV Create a new column removing spaces












1















I have a large csv file which contains multiple columns. I want the spaces to be removed from the first column and want to create that as a new column.



Example...



Input:



a b,xyz,d e f    
a b c,xyz,d e f
a b c d,xyz,d e f


Output:



ab,a b,xyz,d e f   
abc,a b c,xyz,d e f
abcd,a b c d,xyz,d e f









share|improve this question





























    1















    I have a large csv file which contains multiple columns. I want the spaces to be removed from the first column and want to create that as a new column.



    Example...



    Input:



    a b,xyz,d e f    
    a b c,xyz,d e f
    a b c d,xyz,d e f


    Output:



    ab,a b,xyz,d e f   
    abc,a b c,xyz,d e f
    abcd,a b c d,xyz,d e f









    share|improve this question



























      1












      1








      1








      I have a large csv file which contains multiple columns. I want the spaces to be removed from the first column and want to create that as a new column.



      Example...



      Input:



      a b,xyz,d e f    
      a b c,xyz,d e f
      a b c d,xyz,d e f


      Output:



      ab,a b,xyz,d e f   
      abc,a b c,xyz,d e f
      abcd,a b c d,xyz,d e f









      share|improve this question
















      I have a large csv file which contains multiple columns. I want the spaces to be removed from the first column and want to create that as a new column.



      Example...



      Input:



      a b,xyz,d e f    
      a b c,xyz,d e f
      a b c d,xyz,d e f


      Output:



      ab,a b,xyz,d e f   
      abc,a b c,xyz,d e f
      abcd,a b c d,xyz,d e f






      shell-script






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 26 '18 at 23:05









      ctrl-alt-delor

      11.1k42058




      11.1k42058










      asked Nov 13 '17 at 3:22









      ThomasThomas

      193




      193






















          5 Answers
          5






          active

          oldest

          votes


















          1














          Using sed:



          sed 'h;s/,.*/,/;s/ //g;G;s/n//' data.csv


          Explained:



          h - stash current line to the hold space
          s/,.*/,/ - remove everything after the first comma
          s/ //g - remove spaces
          G - append the line from the hold space back to the pattern space
          s/n// - remove extra newline, as left by G





          share|improve this answer































            1














            Using bash with standard tools:



            $ paste -d ',' <( cut -d ',' -f 1 file | tr -d ' ' ) file
            ab,a b,xyz,d e f
            abc,a b c,xyz,d e f
            abcd,a b c d,xyz,d e f


            paste combines the input from the two given files with a comma as the delimiter.



            The first file is produce by a process substitution which extracts the first comma-delimited column of the file and removes its spaces.



            The second file is simply the original unmodified file.






            share|improve this answer
























            • For plain POSIX shells this also works: cut -d, -f1 file | tr -d ' ' | paste -d, - file

              – agc
              Jan 16 at 19:58



















            0














            bash solution assuming a file test.txt:



            #!/bin/bash

            while read -r line; do
            IFS=',' read -ra fields <<<"$line"
            (IFS=','; echo "${fields[0]// /},${fields[*]}")
            done <"test.txt"

            exit


            This takes advantage of IFS to read the values of the csv into an array and output them. Read each line of test.txt into a string, then read this line into an array by splitting on commas. Print the first element of this array with spaces removed, then the entire array. A quoted array using * will output the elements separated by IFS, which we previously set to a comma. The inline declaration of IFS in the read statement and inside a subshell () will preserve the value of IFS for the rest of the script's execution.






            share|improve this answer































              0














              awk 'BEGIN { FS=","; OFS="," } { col=$1; gsub(/ /, "", col); print col,$0 }' file


              Use commas as input and output column separators. Extract first column and sub out spaces. Output the modified first column followed by the original line.






              share|improve this answer

































                0














                with the great Miller is:




                mlr --csv --implicit-csv-header --headerless-csv-output
                put '$newField=gsub($1," +","")'
                then reorder -f newField input.csv



                $1 is the first field. Than I apply a regex search and replace to obtain the new field.






                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%2f404148%2fcsv-create-a-new-column-removing-spaces%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









                  1














                  Using sed:



                  sed 'h;s/,.*/,/;s/ //g;G;s/n//' data.csv


                  Explained:



                  h - stash current line to the hold space
                  s/,.*/,/ - remove everything after the first comma
                  s/ //g - remove spaces
                  G - append the line from the hold space back to the pattern space
                  s/n// - remove extra newline, as left by G





                  share|improve this answer




























                    1














                    Using sed:



                    sed 'h;s/,.*/,/;s/ //g;G;s/n//' data.csv


                    Explained:



                    h - stash current line to the hold space
                    s/,.*/,/ - remove everything after the first comma
                    s/ //g - remove spaces
                    G - append the line from the hold space back to the pattern space
                    s/n// - remove extra newline, as left by G





                    share|improve this answer


























                      1












                      1








                      1







                      Using sed:



                      sed 'h;s/,.*/,/;s/ //g;G;s/n//' data.csv


                      Explained:



                      h - stash current line to the hold space
                      s/,.*/,/ - remove everything after the first comma
                      s/ //g - remove spaces
                      G - append the line from the hold space back to the pattern space
                      s/n// - remove extra newline, as left by G





                      share|improve this answer













                      Using sed:



                      sed 'h;s/,.*/,/;s/ //g;G;s/n//' data.csv


                      Explained:



                      h - stash current line to the hold space
                      s/,.*/,/ - remove everything after the first comma
                      s/ //g - remove spaces
                      G - append the line from the hold space back to the pattern space
                      s/n// - remove extra newline, as left by G






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 15 '17 at 19:06









                      zeppelinzeppelin

                      3,130416




                      3,130416

























                          1














                          Using bash with standard tools:



                          $ paste -d ',' <( cut -d ',' -f 1 file | tr -d ' ' ) file
                          ab,a b,xyz,d e f
                          abc,a b c,xyz,d e f
                          abcd,a b c d,xyz,d e f


                          paste combines the input from the two given files with a comma as the delimiter.



                          The first file is produce by a process substitution which extracts the first comma-delimited column of the file and removes its spaces.



                          The second file is simply the original unmodified file.






                          share|improve this answer
























                          • For plain POSIX shells this also works: cut -d, -f1 file | tr -d ' ' | paste -d, - file

                            – agc
                            Jan 16 at 19:58
















                          1














                          Using bash with standard tools:



                          $ paste -d ',' <( cut -d ',' -f 1 file | tr -d ' ' ) file
                          ab,a b,xyz,d e f
                          abc,a b c,xyz,d e f
                          abcd,a b c d,xyz,d e f


                          paste combines the input from the two given files with a comma as the delimiter.



                          The first file is produce by a process substitution which extracts the first comma-delimited column of the file and removes its spaces.



                          The second file is simply the original unmodified file.






                          share|improve this answer
























                          • For plain POSIX shells this also works: cut -d, -f1 file | tr -d ' ' | paste -d, - file

                            – agc
                            Jan 16 at 19:58














                          1












                          1








                          1







                          Using bash with standard tools:



                          $ paste -d ',' <( cut -d ',' -f 1 file | tr -d ' ' ) file
                          ab,a b,xyz,d e f
                          abc,a b c,xyz,d e f
                          abcd,a b c d,xyz,d e f


                          paste combines the input from the two given files with a comma as the delimiter.



                          The first file is produce by a process substitution which extracts the first comma-delimited column of the file and removes its spaces.



                          The second file is simply the original unmodified file.






                          share|improve this answer













                          Using bash with standard tools:



                          $ paste -d ',' <( cut -d ',' -f 1 file | tr -d ' ' ) file
                          ab,a b,xyz,d e f
                          abc,a b c,xyz,d e f
                          abcd,a b c d,xyz,d e f


                          paste combines the input from the two given files with a comma as the delimiter.



                          The first file is produce by a process substitution which extracts the first comma-delimited column of the file and removes its spaces.



                          The second file is simply the original unmodified file.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Feb 10 '18 at 10:02









                          KusalanandaKusalananda

                          126k16239393




                          126k16239393













                          • For plain POSIX shells this also works: cut -d, -f1 file | tr -d ' ' | paste -d, - file

                            – agc
                            Jan 16 at 19:58



















                          • For plain POSIX shells this also works: cut -d, -f1 file | tr -d ' ' | paste -d, - file

                            – agc
                            Jan 16 at 19:58

















                          For plain POSIX shells this also works: cut -d, -f1 file | tr -d ' ' | paste -d, - file

                          – agc
                          Jan 16 at 19:58





                          For plain POSIX shells this also works: cut -d, -f1 file | tr -d ' ' | paste -d, - file

                          – agc
                          Jan 16 at 19:58











                          0














                          bash solution assuming a file test.txt:



                          #!/bin/bash

                          while read -r line; do
                          IFS=',' read -ra fields <<<"$line"
                          (IFS=','; echo "${fields[0]// /},${fields[*]}")
                          done <"test.txt"

                          exit


                          This takes advantage of IFS to read the values of the csv into an array and output them. Read each line of test.txt into a string, then read this line into an array by splitting on commas. Print the first element of this array with spaces removed, then the entire array. A quoted array using * will output the elements separated by IFS, which we previously set to a comma. The inline declaration of IFS in the read statement and inside a subshell () will preserve the value of IFS for the rest of the script's execution.






                          share|improve this answer




























                            0














                            bash solution assuming a file test.txt:



                            #!/bin/bash

                            while read -r line; do
                            IFS=',' read -ra fields <<<"$line"
                            (IFS=','; echo "${fields[0]// /},${fields[*]}")
                            done <"test.txt"

                            exit


                            This takes advantage of IFS to read the values of the csv into an array and output them. Read each line of test.txt into a string, then read this line into an array by splitting on commas. Print the first element of this array with spaces removed, then the entire array. A quoted array using * will output the elements separated by IFS, which we previously set to a comma. The inline declaration of IFS in the read statement and inside a subshell () will preserve the value of IFS for the rest of the script's execution.






                            share|improve this answer


























                              0












                              0








                              0







                              bash solution assuming a file test.txt:



                              #!/bin/bash

                              while read -r line; do
                              IFS=',' read -ra fields <<<"$line"
                              (IFS=','; echo "${fields[0]// /},${fields[*]}")
                              done <"test.txt"

                              exit


                              This takes advantage of IFS to read the values of the csv into an array and output them. Read each line of test.txt into a string, then read this line into an array by splitting on commas. Print the first element of this array with spaces removed, then the entire array. A quoted array using * will output the elements separated by IFS, which we previously set to a comma. The inline declaration of IFS in the read statement and inside a subshell () will preserve the value of IFS for the rest of the script's execution.






                              share|improve this answer













                              bash solution assuming a file test.txt:



                              #!/bin/bash

                              while read -r line; do
                              IFS=',' read -ra fields <<<"$line"
                              (IFS=','; echo "${fields[0]// /},${fields[*]}")
                              done <"test.txt"

                              exit


                              This takes advantage of IFS to read the values of the csv into an array and output them. Read each line of test.txt into a string, then read this line into an array by splitting on commas. Print the first element of this array with spaces removed, then the entire array. A quoted array using * will output the elements separated by IFS, which we previously set to a comma. The inline declaration of IFS in the read statement and inside a subshell () will preserve the value of IFS for the rest of the script's execution.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 15 '17 at 18:42









                              m0dularm0dular

                              70115




                              70115























                                  0














                                  awk 'BEGIN { FS=","; OFS="," } { col=$1; gsub(/ /, "", col); print col,$0 }' file


                                  Use commas as input and output column separators. Extract first column and sub out spaces. Output the modified first column followed by the original line.






                                  share|improve this answer






























                                    0














                                    awk 'BEGIN { FS=","; OFS="," } { col=$1; gsub(/ /, "", col); print col,$0 }' file


                                    Use commas as input and output column separators. Extract first column and sub out spaces. Output the modified first column followed by the original line.






                                    share|improve this answer




























                                      0












                                      0








                                      0







                                      awk 'BEGIN { FS=","; OFS="," } { col=$1; gsub(/ /, "", col); print col,$0 }' file


                                      Use commas as input and output column separators. Extract first column and sub out spaces. Output the modified first column followed by the original line.






                                      share|improve this answer















                                      awk 'BEGIN { FS=","; OFS="," } { col=$1; gsub(/ /, "", col); print col,$0 }' file


                                      Use commas as input and output column separators. Extract first column and sub out spaces. Output the modified first column followed by the original line.







                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Dec 26 '18 at 22:55









                                      Rui F Ribeiro

                                      39.6k1479132




                                      39.6k1479132










                                      answered Nov 13 '17 at 3:50









                                      B LayerB Layer

                                      4,0241525




                                      4,0241525























                                          0














                                          with the great Miller is:




                                          mlr --csv --implicit-csv-header --headerless-csv-output
                                          put '$newField=gsub($1," +","")'
                                          then reorder -f newField input.csv



                                          $1 is the first field. Than I apply a regex search and replace to obtain the new field.






                                          share|improve this answer




























                                            0














                                            with the great Miller is:




                                            mlr --csv --implicit-csv-header --headerless-csv-output
                                            put '$newField=gsub($1," +","")'
                                            then reorder -f newField input.csv



                                            $1 is the first field. Than I apply a regex search and replace to obtain the new field.






                                            share|improve this answer


























                                              0












                                              0








                                              0







                                              with the great Miller is:




                                              mlr --csv --implicit-csv-header --headerless-csv-output
                                              put '$newField=gsub($1," +","")'
                                              then reorder -f newField input.csv



                                              $1 is the first field. Than I apply a regex search and replace to obtain the new field.






                                              share|improve this answer













                                              with the great Miller is:




                                              mlr --csv --implicit-csv-header --headerless-csv-output
                                              put '$newField=gsub($1," +","")'
                                              then reorder -f newField input.csv



                                              $1 is the first field. Than I apply a regex search and replace to obtain the new field.







                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered Dec 28 '18 at 7:13









                                              aborrusoaborruso

                                              20619




                                              20619






























                                                  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%2f404148%2fcsv-create-a-new-column-removing-spaces%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

                                                  How to reconfigure Docker Trusted Registry 2.x.x to use CEPH FS mount instead of NFS and other traditional...

                                                  is 'sed' thread safe

                                                  How to make a Squid Proxy server?