Remove comma outside quotes

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
3
down vote

favorite












I have a input file delimited with commas (,). There are some fields enclosed in double quotes that are having a comma in them. Here is the sample row



123,"ABC, DEV 23",345,534.202,NAME


I need to remove all the comma's that are not occuring within the double quotes by a ~
So the output should be like:



123~"ABC, DEV 23"~345~534.202~NAME


I have tried this, but it gives me reverse output:



awk -F '"' -v OFS='' ' for (i=0; i<= NF; ++i) gsub(",","~",$i) 1' test.txt
123,ABC~ DEV 23,345,534.202,NAME






share|improve this question






















  • You will very unlikely have an answer. Best pratice on stack exchange is to show some work and some of your try. You should consult unix.stackexchange.com/tour You can edit your question to show what you've try so far.
    – Kiwy
    Feb 7 at 12:45















up vote
3
down vote

favorite












I have a input file delimited with commas (,). There are some fields enclosed in double quotes that are having a comma in them. Here is the sample row



123,"ABC, DEV 23",345,534.202,NAME


I need to remove all the comma's that are not occuring within the double quotes by a ~
So the output should be like:



123~"ABC, DEV 23"~345~534.202~NAME


I have tried this, but it gives me reverse output:



awk -F '"' -v OFS='' ' for (i=0; i<= NF; ++i) gsub(",","~",$i) 1' test.txt
123,ABC~ DEV 23,345,534.202,NAME






share|improve this question






















  • You will very unlikely have an answer. Best pratice on stack exchange is to show some work and some of your try. You should consult unix.stackexchange.com/tour You can edit your question to show what you've try so far.
    – Kiwy
    Feb 7 at 12:45













up vote
3
down vote

favorite









up vote
3
down vote

favorite











I have a input file delimited with commas (,). There are some fields enclosed in double quotes that are having a comma in them. Here is the sample row



123,"ABC, DEV 23",345,534.202,NAME


I need to remove all the comma's that are not occuring within the double quotes by a ~
So the output should be like:



123~"ABC, DEV 23"~345~534.202~NAME


I have tried this, but it gives me reverse output:



awk -F '"' -v OFS='' ' for (i=0; i<= NF; ++i) gsub(",","~",$i) 1' test.txt
123,ABC~ DEV 23,345,534.202,NAME






share|improve this question














I have a input file delimited with commas (,). There are some fields enclosed in double quotes that are having a comma in them. Here is the sample row



123,"ABC, DEV 23",345,534.202,NAME


I need to remove all the comma's that are not occuring within the double quotes by a ~
So the output should be like:



123~"ABC, DEV 23"~345~534.202~NAME


I have tried this, but it gives me reverse output:



awk -F '"' -v OFS='' ' for (i=0; i<= NF; ++i) gsub(",","~",$i) 1' test.txt
123,ABC~ DEV 23,345,534.202,NAME








share|improve this question













share|improve this question




share|improve this question








edited Feb 7 at 17:09









αғsнιη

14.9k82462




14.9k82462










asked Feb 7 at 12:34









Sudeep Adhya

162




162











  • You will very unlikely have an answer. Best pratice on stack exchange is to show some work and some of your try. You should consult unix.stackexchange.com/tour You can edit your question to show what you've try so far.
    – Kiwy
    Feb 7 at 12:45

















  • You will very unlikely have an answer. Best pratice on stack exchange is to show some work and some of your try. You should consult unix.stackexchange.com/tour You can edit your question to show what you've try so far.
    – Kiwy
    Feb 7 at 12:45
















You will very unlikely have an answer. Best pratice on stack exchange is to show some work and some of your try. You should consult unix.stackexchange.com/tour You can edit your question to show what you've try so far.
– Kiwy
Feb 7 at 12:45





You will very unlikely have an answer. Best pratice on stack exchange is to show some work and some of your try. You should consult unix.stackexchange.com/tour You can edit your question to show what you've try so far.
– Kiwy
Feb 7 at 12:45











7 Answers
7






active

oldest

votes

















up vote
5
down vote













You basically have a CSV file that you would like to replace the delimiter in, from , to ~.



Using csvkit:



$ csvformat -D '~' file.csv >newfile.csv

$ cat newfile.csv
123~ABC, DEV 23~345~534.202~NAME


cvsformat removes the quotation marks that are not needed. To add quotation marks:



$ csvformat -U 1 -D '~' file.csv
"123"~"ABC, DEV 23"~"345"~"534.202"~"NAME"


See csvformat --help for usage info.






share|improve this answer





























    up vote
    2
    down vote













    GNU awk solution:



    awk -v FPAT='[^,]+|"[^"]+"' ' for(i=1;i<=NF;i++) printf "%s%s",$i,(i<NF? "~" : ORS) ' file



    • FPAT='[^,]+|"[^"]+"' - regex pattern describing each field is either “anything that is not a comma,” or “a double quote, anything that is not a double quote, and a closing double quote.”

    The output:



    123~"ABC, DEV 23"~345~534.202~NAME





    share|improve this answer





























      up vote
      0
      down vote













      I have done by below 2 methods for above example . Tested



      Method1



      for (( i=1;i<6;i++)); do awk -F "," -v i="$i" '$i ~ /"/gsub(" ",",",$2);print ' inputfile;done| tail -1| sed 's/,/~/3g'| sed 's/,/~/1'


      output



      123~"ABC, DEV 23"~345~534.202~NAME'


      Method2



       sed "s/,/~/3g" inputfile| sed 's/,/~/1'


      Output



      123~"ABC, DEV 23"~345~534.202~NAME'





      share|improve this answer



























        up vote
        0
        down vote













        You can try this awk



        awk 'NR%2==1gsub(",","~")1' RS='"' ORS='"' infile





        share|improve this answer



























          up vote
          0
          down vote













          In case you don't want to install a special package, you can probably the csv parser of a preinstalled ruby:



          ruby -e 'require "csv"; CSV.filter(output_col_sep: "~") '


          Examples:



          1,"2,3"


          results in



          1~2,3


          and



          1,"
          2,3
          ",4


          results in



          1~"
          2,3
          "~4





          share|improve this answer



























            up vote
            0
            down vote













            This is opposite of this question that I had answer there too, it's can be done by starting loop step value from 1.



            awk -F" 'for (i=1;i<=NF;i+=2)gsub(",","~",$i)1' OFS=" infile


            Output is:



            123~"ABC, DEV 23"~345~534.202~NAME





            share|improve this answer





























              up vote
              0
              down vote













              awk 'sub(/,/,"~")gsub(/,/,"~",$3)1' file

              output
              123~"ABC, DEV 23"~345~534.202~NAME


              The very first comma is replaced by sub and the rest by gsub in third field.






              share|improve this answer






















              • The code may provide answer to the question, but any explanations will be highly appretiated
                – Romeo Ninov
                Feb 23 at 7:40










              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',
              convertImagesToLinks: false,
              noModals: false,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              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%2f422526%2fremove-comma-outside-quotes%23new-answer', 'question_page');

              );

              Post as a guest






























              7 Answers
              7






              active

              oldest

              votes








              7 Answers
              7






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              5
              down vote













              You basically have a CSV file that you would like to replace the delimiter in, from , to ~.



              Using csvkit:



              $ csvformat -D '~' file.csv >newfile.csv

              $ cat newfile.csv
              123~ABC, DEV 23~345~534.202~NAME


              cvsformat removes the quotation marks that are not needed. To add quotation marks:



              $ csvformat -U 1 -D '~' file.csv
              "123"~"ABC, DEV 23"~"345"~"534.202"~"NAME"


              See csvformat --help for usage info.






              share|improve this answer


























                up vote
                5
                down vote













                You basically have a CSV file that you would like to replace the delimiter in, from , to ~.



                Using csvkit:



                $ csvformat -D '~' file.csv >newfile.csv

                $ cat newfile.csv
                123~ABC, DEV 23~345~534.202~NAME


                cvsformat removes the quotation marks that are not needed. To add quotation marks:



                $ csvformat -U 1 -D '~' file.csv
                "123"~"ABC, DEV 23"~"345"~"534.202"~"NAME"


                See csvformat --help for usage info.






                share|improve this answer
























                  up vote
                  5
                  down vote










                  up vote
                  5
                  down vote









                  You basically have a CSV file that you would like to replace the delimiter in, from , to ~.



                  Using csvkit:



                  $ csvformat -D '~' file.csv >newfile.csv

                  $ cat newfile.csv
                  123~ABC, DEV 23~345~534.202~NAME


                  cvsformat removes the quotation marks that are not needed. To add quotation marks:



                  $ csvformat -U 1 -D '~' file.csv
                  "123"~"ABC, DEV 23"~"345"~"534.202"~"NAME"


                  See csvformat --help for usage info.






                  share|improve this answer














                  You basically have a CSV file that you would like to replace the delimiter in, from , to ~.



                  Using csvkit:



                  $ csvformat -D '~' file.csv >newfile.csv

                  $ cat newfile.csv
                  123~ABC, DEV 23~345~534.202~NAME


                  cvsformat removes the quotation marks that are not needed. To add quotation marks:



                  $ csvformat -U 1 -D '~' file.csv
                  "123"~"ABC, DEV 23"~"345"~"534.202"~"NAME"


                  See csvformat --help for usage info.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Feb 23 at 12:21

























                  answered Feb 7 at 12:52









                  Kusalananda

                  103k13202318




                  103k13202318






















                      up vote
                      2
                      down vote













                      GNU awk solution:



                      awk -v FPAT='[^,]+|"[^"]+"' ' for(i=1;i<=NF;i++) printf "%s%s",$i,(i<NF? "~" : ORS) ' file



                      • FPAT='[^,]+|"[^"]+"' - regex pattern describing each field is either “anything that is not a comma,” or “a double quote, anything that is not a double quote, and a closing double quote.”

                      The output:



                      123~"ABC, DEV 23"~345~534.202~NAME





                      share|improve this answer


























                        up vote
                        2
                        down vote













                        GNU awk solution:



                        awk -v FPAT='[^,]+|"[^"]+"' ' for(i=1;i<=NF;i++) printf "%s%s",$i,(i<NF? "~" : ORS) ' file



                        • FPAT='[^,]+|"[^"]+"' - regex pattern describing each field is either “anything that is not a comma,” or “a double quote, anything that is not a double quote, and a closing double quote.”

                        The output:



                        123~"ABC, DEV 23"~345~534.202~NAME





                        share|improve this answer
























                          up vote
                          2
                          down vote










                          up vote
                          2
                          down vote









                          GNU awk solution:



                          awk -v FPAT='[^,]+|"[^"]+"' ' for(i=1;i<=NF;i++) printf "%s%s",$i,(i<NF? "~" : ORS) ' file



                          • FPAT='[^,]+|"[^"]+"' - regex pattern describing each field is either “anything that is not a comma,” or “a double quote, anything that is not a double quote, and a closing double quote.”

                          The output:



                          123~"ABC, DEV 23"~345~534.202~NAME





                          share|improve this answer














                          GNU awk solution:



                          awk -v FPAT='[^,]+|"[^"]+"' ' for(i=1;i<=NF;i++) printf "%s%s",$i,(i<NF? "~" : ORS) ' file



                          • FPAT='[^,]+|"[^"]+"' - regex pattern describing each field is either “anything that is not a comma,” or “a double quote, anything that is not a double quote, and a closing double quote.”

                          The output:



                          123~"ABC, DEV 23"~345~534.202~NAME






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Feb 7 at 13:09

























                          answered Feb 7 at 13:03









                          RomanPerekhrest

                          22.4k12144




                          22.4k12144




















                              up vote
                              0
                              down vote













                              I have done by below 2 methods for above example . Tested



                              Method1



                              for (( i=1;i<6;i++)); do awk -F "," -v i="$i" '$i ~ /"/gsub(" ",",",$2);print ' inputfile;done| tail -1| sed 's/,/~/3g'| sed 's/,/~/1'


                              output



                              123~"ABC, DEV 23"~345~534.202~NAME'


                              Method2



                               sed "s/,/~/3g" inputfile| sed 's/,/~/1'


                              Output



                              123~"ABC, DEV 23"~345~534.202~NAME'





                              share|improve this answer
























                                up vote
                                0
                                down vote













                                I have done by below 2 methods for above example . Tested



                                Method1



                                for (( i=1;i<6;i++)); do awk -F "," -v i="$i" '$i ~ /"/gsub(" ",",",$2);print ' inputfile;done| tail -1| sed 's/,/~/3g'| sed 's/,/~/1'


                                output



                                123~"ABC, DEV 23"~345~534.202~NAME'


                                Method2



                                 sed "s/,/~/3g" inputfile| sed 's/,/~/1'


                                Output



                                123~"ABC, DEV 23"~345~534.202~NAME'





                                share|improve this answer






















                                  up vote
                                  0
                                  down vote










                                  up vote
                                  0
                                  down vote









                                  I have done by below 2 methods for above example . Tested



                                  Method1



                                  for (( i=1;i<6;i++)); do awk -F "," -v i="$i" '$i ~ /"/gsub(" ",",",$2);print ' inputfile;done| tail -1| sed 's/,/~/3g'| sed 's/,/~/1'


                                  output



                                  123~"ABC, DEV 23"~345~534.202~NAME'


                                  Method2



                                   sed "s/,/~/3g" inputfile| sed 's/,/~/1'


                                  Output



                                  123~"ABC, DEV 23"~345~534.202~NAME'





                                  share|improve this answer












                                  I have done by below 2 methods for above example . Tested



                                  Method1



                                  for (( i=1;i<6;i++)); do awk -F "," -v i="$i" '$i ~ /"/gsub(" ",",",$2);print ' inputfile;done| tail -1| sed 's/,/~/3g'| sed 's/,/~/1'


                                  output



                                  123~"ABC, DEV 23"~345~534.202~NAME'


                                  Method2



                                   sed "s/,/~/3g" inputfile| sed 's/,/~/1'


                                  Output



                                  123~"ABC, DEV 23"~345~534.202~NAME'






                                  share|improve this answer












                                  share|improve this answer



                                  share|improve this answer










                                  answered Feb 7 at 16:46









                                  Praveen Kumar BS

                                  1,010128




                                  1,010128




















                                      up vote
                                      0
                                      down vote













                                      You can try this awk



                                      awk 'NR%2==1gsub(",","~")1' RS='"' ORS='"' infile





                                      share|improve this answer
























                                        up vote
                                        0
                                        down vote













                                        You can try this awk



                                        awk 'NR%2==1gsub(",","~")1' RS='"' ORS='"' infile





                                        share|improve this answer






















                                          up vote
                                          0
                                          down vote










                                          up vote
                                          0
                                          down vote









                                          You can try this awk



                                          awk 'NR%2==1gsub(",","~")1' RS='"' ORS='"' infile





                                          share|improve this answer












                                          You can try this awk



                                          awk 'NR%2==1gsub(",","~")1' RS='"' ORS='"' infile






                                          share|improve this answer












                                          share|improve this answer



                                          share|improve this answer










                                          answered Feb 7 at 20:54









                                          ctac_

                                          1,016116




                                          1,016116




















                                              up vote
                                              0
                                              down vote













                                              In case you don't want to install a special package, you can probably the csv parser of a preinstalled ruby:



                                              ruby -e 'require "csv"; CSV.filter(output_col_sep: "~") '


                                              Examples:



                                              1,"2,3"


                                              results in



                                              1~2,3


                                              and



                                              1,"
                                              2,3
                                              ",4


                                              results in



                                              1~"
                                              2,3
                                              "~4





                                              share|improve this answer
























                                                up vote
                                                0
                                                down vote













                                                In case you don't want to install a special package, you can probably the csv parser of a preinstalled ruby:



                                                ruby -e 'require "csv"; CSV.filter(output_col_sep: "~") '


                                                Examples:



                                                1,"2,3"


                                                results in



                                                1~2,3


                                                and



                                                1,"
                                                2,3
                                                ",4


                                                results in



                                                1~"
                                                2,3
                                                "~4





                                                share|improve this answer






















                                                  up vote
                                                  0
                                                  down vote










                                                  up vote
                                                  0
                                                  down vote









                                                  In case you don't want to install a special package, you can probably the csv parser of a preinstalled ruby:



                                                  ruby -e 'require "csv"; CSV.filter(output_col_sep: "~") '


                                                  Examples:



                                                  1,"2,3"


                                                  results in



                                                  1~2,3


                                                  and



                                                  1,"
                                                  2,3
                                                  ",4


                                                  results in



                                                  1~"
                                                  2,3
                                                  "~4





                                                  share|improve this answer












                                                  In case you don't want to install a special package, you can probably the csv parser of a preinstalled ruby:



                                                  ruby -e 'require "csv"; CSV.filter(output_col_sep: "~") '


                                                  Examples:



                                                  1,"2,3"


                                                  results in



                                                  1~2,3


                                                  and



                                                  1,"
                                                  2,3
                                                  ",4


                                                  results in



                                                  1~"
                                                  2,3
                                                  "~4






                                                  share|improve this answer












                                                  share|improve this answer



                                                  share|improve this answer










                                                  answered Feb 8 at 17:45









                                                  JoL

                                                  68819




                                                  68819




















                                                      up vote
                                                      0
                                                      down vote













                                                      This is opposite of this question that I had answer there too, it's can be done by starting loop step value from 1.



                                                      awk -F" 'for (i=1;i<=NF;i+=2)gsub(",","~",$i)1' OFS=" infile


                                                      Output is:



                                                      123~"ABC, DEV 23"~345~534.202~NAME





                                                      share|improve this answer


























                                                        up vote
                                                        0
                                                        down vote













                                                        This is opposite of this question that I had answer there too, it's can be done by starting loop step value from 1.



                                                        awk -F" 'for (i=1;i<=NF;i+=2)gsub(",","~",$i)1' OFS=" infile


                                                        Output is:



                                                        123~"ABC, DEV 23"~345~534.202~NAME





                                                        share|improve this answer
























                                                          up vote
                                                          0
                                                          down vote










                                                          up vote
                                                          0
                                                          down vote









                                                          This is opposite of this question that I had answer there too, it's can be done by starting loop step value from 1.



                                                          awk -F" 'for (i=1;i<=NF;i+=2)gsub(",","~",$i)1' OFS=" infile


                                                          Output is:



                                                          123~"ABC, DEV 23"~345~534.202~NAME





                                                          share|improve this answer














                                                          This is opposite of this question that I had answer there too, it's can be done by starting loop step value from 1.



                                                          awk -F" 'for (i=1;i<=NF;i+=2)gsub(",","~",$i)1' OFS=" infile


                                                          Output is:



                                                          123~"ABC, DEV 23"~345~534.202~NAME






                                                          share|improve this answer














                                                          share|improve this answer



                                                          share|improve this answer








                                                          edited Feb 8 at 18:02

























                                                          answered Feb 7 at 17:09









                                                          αғsнιη

                                                          14.9k82462




                                                          14.9k82462




















                                                              up vote
                                                              0
                                                              down vote













                                                              awk 'sub(/,/,"~")gsub(/,/,"~",$3)1' file

                                                              output
                                                              123~"ABC, DEV 23"~345~534.202~NAME


                                                              The very first comma is replaced by sub and the rest by gsub in third field.






                                                              share|improve this answer






















                                                              • The code may provide answer to the question, but any explanations will be highly appretiated
                                                                – Romeo Ninov
                                                                Feb 23 at 7:40














                                                              up vote
                                                              0
                                                              down vote













                                                              awk 'sub(/,/,"~")gsub(/,/,"~",$3)1' file

                                                              output
                                                              123~"ABC, DEV 23"~345~534.202~NAME


                                                              The very first comma is replaced by sub and the rest by gsub in third field.






                                                              share|improve this answer






















                                                              • The code may provide answer to the question, but any explanations will be highly appretiated
                                                                – Romeo Ninov
                                                                Feb 23 at 7:40












                                                              up vote
                                                              0
                                                              down vote










                                                              up vote
                                                              0
                                                              down vote









                                                              awk 'sub(/,/,"~")gsub(/,/,"~",$3)1' file

                                                              output
                                                              123~"ABC, DEV 23"~345~534.202~NAME


                                                              The very first comma is replaced by sub and the rest by gsub in third field.






                                                              share|improve this answer














                                                              awk 'sub(/,/,"~")gsub(/,/,"~",$3)1' file

                                                              output
                                                              123~"ABC, DEV 23"~345~534.202~NAME


                                                              The very first comma is replaced by sub and the rest by gsub in third field.







                                                              share|improve this answer














                                                              share|improve this answer



                                                              share|improve this answer








                                                              edited Feb 23 at 12:18

























                                                              answered Feb 23 at 3:14









                                                              Claes Wikner

                                                              11713




                                                              11713











                                                              • The code may provide answer to the question, but any explanations will be highly appretiated
                                                                – Romeo Ninov
                                                                Feb 23 at 7:40
















                                                              • The code may provide answer to the question, but any explanations will be highly appretiated
                                                                – Romeo Ninov
                                                                Feb 23 at 7:40















                                                              The code may provide answer to the question, but any explanations will be highly appretiated
                                                              – Romeo Ninov
                                                              Feb 23 at 7:40




                                                              The code may provide answer to the question, but any explanations will be highly appretiated
                                                              – Romeo Ninov
                                                              Feb 23 at 7:40












                                                               

                                                              draft saved


                                                              draft discarded


























                                                               


                                                              draft saved


                                                              draft discarded














                                                              StackExchange.ready(
                                                              function ()
                                                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f422526%2fremove-comma-outside-quotes%23new-answer', 'question_page');

                                                              );

                                                              Post as a guest













































































                                                              Popular posts from this blog

                                                              How to check contact read email or not when send email to Individual?

                                                              Bahrain

                                                              Postfix configuration issue with fips on centos 7; mailgun relay