Is it possible to put double quotes in just a few columns using sed?

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











up vote
1
down vote

favorite












I'm doing my course completion work where I need to manipulate some columns of a .csv file that has six hundred thousand lines. I have already searched in several forums on sed and awk and I did not find anything like it (and unfortunately my knowledge about Linux is not very deep). What I found generally handles only one column or all ... what I need is to put double quotes only in the first, fifth, and sixth columns.



For example:



2018-03-18 4:56:17,255.255.255.255,00,ssh,admin,123


To be



"2018-03-18 4:56:17",255.255.255.255,00,ssh,"admin","123"


Still, maybe I still change some column since I'm just starting in data mining and I do not know what the configuration will be like in a few weeks, so if you can do a brief syntax of logic, I'll be eternally grateful.







share|improve this question






















  • What trouble did you have with extending a solution for one column?
    – muru
    Apr 12 at 1:02











  • What I found generally handles only one column or all please add those to question.. it will show your research effort..
    – Sundeep
    Apr 12 at 4:07










  • Is the reason that you need to quote these column that they may contain commas? This matters for most answers written so far.
    – Kusalananda
    Apr 12 at 6:28







  • 2




    To be more precise: If the reason that the fields needs to be quoted is that some of the data in those fields contain commas, then that would invalidate every answer written so far.
    – Kusalananda
    Apr 12 at 7:24










  • I agree with Kusalananda. I have to ask: why are you trying to do this? This smells like an XY problem to me.
    – glenn jackman
    Apr 12 at 14:09















up vote
1
down vote

favorite












I'm doing my course completion work where I need to manipulate some columns of a .csv file that has six hundred thousand lines. I have already searched in several forums on sed and awk and I did not find anything like it (and unfortunately my knowledge about Linux is not very deep). What I found generally handles only one column or all ... what I need is to put double quotes only in the first, fifth, and sixth columns.



For example:



2018-03-18 4:56:17,255.255.255.255,00,ssh,admin,123


To be



"2018-03-18 4:56:17",255.255.255.255,00,ssh,"admin","123"


Still, maybe I still change some column since I'm just starting in data mining and I do not know what the configuration will be like in a few weeks, so if you can do a brief syntax of logic, I'll be eternally grateful.







share|improve this question






















  • What trouble did you have with extending a solution for one column?
    – muru
    Apr 12 at 1:02











  • What I found generally handles only one column or all please add those to question.. it will show your research effort..
    – Sundeep
    Apr 12 at 4:07










  • Is the reason that you need to quote these column that they may contain commas? This matters for most answers written so far.
    – Kusalananda
    Apr 12 at 6:28







  • 2




    To be more precise: If the reason that the fields needs to be quoted is that some of the data in those fields contain commas, then that would invalidate every answer written so far.
    – Kusalananda
    Apr 12 at 7:24










  • I agree with Kusalananda. I have to ask: why are you trying to do this? This smells like an XY problem to me.
    – glenn jackman
    Apr 12 at 14:09













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I'm doing my course completion work where I need to manipulate some columns of a .csv file that has six hundred thousand lines. I have already searched in several forums on sed and awk and I did not find anything like it (and unfortunately my knowledge about Linux is not very deep). What I found generally handles only one column or all ... what I need is to put double quotes only in the first, fifth, and sixth columns.



For example:



2018-03-18 4:56:17,255.255.255.255,00,ssh,admin,123


To be



"2018-03-18 4:56:17",255.255.255.255,00,ssh,"admin","123"


Still, maybe I still change some column since I'm just starting in data mining and I do not know what the configuration will be like in a few weeks, so if you can do a brief syntax of logic, I'll be eternally grateful.







share|improve this question














I'm doing my course completion work where I need to manipulate some columns of a .csv file that has six hundred thousand lines. I have already searched in several forums on sed and awk and I did not find anything like it (and unfortunately my knowledge about Linux is not very deep). What I found generally handles only one column or all ... what I need is to put double quotes only in the first, fifth, and sixth columns.



For example:



2018-03-18 4:56:17,255.255.255.255,00,ssh,admin,123


To be



"2018-03-18 4:56:17",255.255.255.255,00,ssh,"admin","123"


Still, maybe I still change some column since I'm just starting in data mining and I do not know what the configuration will be like in a few weeks, so if you can do a brief syntax of logic, I'll be eternally grateful.









share|improve this question













share|improve this question




share|improve this question








edited Apr 12 at 1:04









Jeff Schaller

31.1k846105




31.1k846105










asked Apr 12 at 0:15









Felipe Duarte

62




62











  • What trouble did you have with extending a solution for one column?
    – muru
    Apr 12 at 1:02











  • What I found generally handles only one column or all please add those to question.. it will show your research effort..
    – Sundeep
    Apr 12 at 4:07










  • Is the reason that you need to quote these column that they may contain commas? This matters for most answers written so far.
    – Kusalananda
    Apr 12 at 6:28







  • 2




    To be more precise: If the reason that the fields needs to be quoted is that some of the data in those fields contain commas, then that would invalidate every answer written so far.
    – Kusalananda
    Apr 12 at 7:24










  • I agree with Kusalananda. I have to ask: why are you trying to do this? This smells like an XY problem to me.
    – glenn jackman
    Apr 12 at 14:09

















  • What trouble did you have with extending a solution for one column?
    – muru
    Apr 12 at 1:02











  • What I found generally handles only one column or all please add those to question.. it will show your research effort..
    – Sundeep
    Apr 12 at 4:07










  • Is the reason that you need to quote these column that they may contain commas? This matters for most answers written so far.
    – Kusalananda
    Apr 12 at 6:28







  • 2




    To be more precise: If the reason that the fields needs to be quoted is that some of the data in those fields contain commas, then that would invalidate every answer written so far.
    – Kusalananda
    Apr 12 at 7:24










  • I agree with Kusalananda. I have to ask: why are you trying to do this? This smells like an XY problem to me.
    – glenn jackman
    Apr 12 at 14:09
















What trouble did you have with extending a solution for one column?
– muru
Apr 12 at 1:02





What trouble did you have with extending a solution for one column?
– muru
Apr 12 at 1:02













What I found generally handles only one column or all please add those to question.. it will show your research effort..
– Sundeep
Apr 12 at 4:07




What I found generally handles only one column or all please add those to question.. it will show your research effort..
– Sundeep
Apr 12 at 4:07












Is the reason that you need to quote these column that they may contain commas? This matters for most answers written so far.
– Kusalananda
Apr 12 at 6:28





Is the reason that you need to quote these column that they may contain commas? This matters for most answers written so far.
– Kusalananda
Apr 12 at 6:28





2




2




To be more precise: If the reason that the fields needs to be quoted is that some of the data in those fields contain commas, then that would invalidate every answer written so far.
– Kusalananda
Apr 12 at 7:24




To be more precise: If the reason that the fields needs to be quoted is that some of the data in those fields contain commas, then that would invalidate every answer written so far.
– Kusalananda
Apr 12 at 7:24












I agree with Kusalananda. I have to ask: why are you trying to do this? This smells like an XY problem to me.
– glenn jackman
Apr 12 at 14:09





I agree with Kusalananda. I have to ask: why are you trying to do this? This smells like an XY problem to me.
– glenn jackman
Apr 12 at 14:09











4 Answers
4






active

oldest

votes

















up vote
4
down vote













Using csvtool which has a helpful format command:



csvtool format '"%1",%2,%3,%4,"%5","%6"n' file.csv 


Example:



echo "2018-03-18 4:56:17,255.255.255.255,00,ssh,admin,123" |
csvtool format '"%1",%2,%3,%4,"%5","%6"n' -


Output:



"2018-03-18 4:56:17",255.255.255.255,00,ssh,"admin","123"



csvtool also can call shell functions and external programs to parse lines from .CSV files. To do the same thing using printf, but print the "123" in hexadecimal, do:



echo "2018-03-18 4:56:17,255.255.255.255,00,ssh,admin,123" | 
csvtool call "printf '"%s",%s,%s,%s,"%s","%x"n'" -


Output:



"2018-03-18 4:56:17",255.255.255.255,00,ssh,"admin","7b"





share|improve this answer






















  • +1 this is cool.
    – Î±Ò“sнιη
    Apr 12 at 6:42

















up vote
2
down vote













I might approach it with awk this way:



  1. set the output field separator to what the input field separator is, which we assign as the comma -F,

  2. for each line, reassign the values of fields 1, 5, and 6 as being the original values but surrounded by double-quotes. The apparent quoting mess is because I used double-quotes to create the surrounding string, and because the only string I want to print is a double-quote, I have to escape it, so each double-quote that I want ends up being """.

  3. Once the fields have been updated, print the newly-combined string.

The script is:



awk -F, 'BEGIN OFS=FS $1="""$1"""; $5="""$5"""; $6="""$6"""; print ' < input.csv > output.csv


As you find that you need to quote more fields, simply do the same thing as with fields 1, 5, and 6 above.






share|improve this answer



























    up vote
    1
    down vote















    With perl



    $ perl -F, -lane 'map $_=qq("$_") @F[0,4,5]; print join ",", @F' ip.txt
    "2018-03-18 4:56:17",255.255.255.255,00,ssh,"admin","123"



    • -F, use , as input field delimiter, results available in @F array


    • map $_=qq("$_") @F[0,4,5] double quote required array elements. Index starts from 0. The qq operator is used here to avoid escaping double quotes, qq("$_") is same as ""$_""


    • print join ",", @F print the modified array with , as separator



    Another way to do with awk



    $ awk -v q='"' 'BEGINsplit("1 5 6",a); FS=OFS=","
    for(i in a) $a[i]=q $a[i] q 1' ip.txt
    "2018-03-18 4:56:17",255.255.255.255,00,ssh,"admin","123"



    • -v q='"' save double quote as value in q variable


    • split("1 5 6",a) save index to be changed as values in a array (default split on whitespace, FS is not yet changed)


    • FS=OFS="," change input/output delimiter to ,


    • for(i in a) $a[i]=q $a[i] q change required fields


    • 1 idiomatic way to print contents of $0





    share|improve this answer





























      up vote
      0
      down vote













      If you really want to use sed (which I don't recommend if awk or perl are available), then assuming that the sixth column is terminated by the line ending rather than a comma AND that fields do not themselves contain embedded (quoted) commas:



      sed -E -e 's/([^,]*),/"1",/1' -e 's/([^,]*),/"1",/5' -e 's/([^,]*)$/"1"/' file
      "2018-03-18 4:56:17",255.255.255.255,00,ssh,"admin","123"


      The general pattern is s/([^,]*),/"1",/n where n refers to the nth occurrence of zero or more non-, characters followed by a , and 1 is a reference to the captured pattern (between parentheses).






      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',
        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%2f437164%2fis-it-possible-to-put-double-quotes-in-just-a-few-columns-using-sed%23new-answer', 'question_page');

        );

        Post as a guest






























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        4
        down vote













        Using csvtool which has a helpful format command:



        csvtool format '"%1",%2,%3,%4,"%5","%6"n' file.csv 


        Example:



        echo "2018-03-18 4:56:17,255.255.255.255,00,ssh,admin,123" |
        csvtool format '"%1",%2,%3,%4,"%5","%6"n' -


        Output:



        "2018-03-18 4:56:17",255.255.255.255,00,ssh,"admin","123"



        csvtool also can call shell functions and external programs to parse lines from .CSV files. To do the same thing using printf, but print the "123" in hexadecimal, do:



        echo "2018-03-18 4:56:17,255.255.255.255,00,ssh,admin,123" | 
        csvtool call "printf '"%s",%s,%s,%s,"%s","%x"n'" -


        Output:



        "2018-03-18 4:56:17",255.255.255.255,00,ssh,"admin","7b"





        share|improve this answer






















        • +1 this is cool.
          – Î±Ò“sнιη
          Apr 12 at 6:42














        up vote
        4
        down vote













        Using csvtool which has a helpful format command:



        csvtool format '"%1",%2,%3,%4,"%5","%6"n' file.csv 


        Example:



        echo "2018-03-18 4:56:17,255.255.255.255,00,ssh,admin,123" |
        csvtool format '"%1",%2,%3,%4,"%5","%6"n' -


        Output:



        "2018-03-18 4:56:17",255.255.255.255,00,ssh,"admin","123"



        csvtool also can call shell functions and external programs to parse lines from .CSV files. To do the same thing using printf, but print the "123" in hexadecimal, do:



        echo "2018-03-18 4:56:17,255.255.255.255,00,ssh,admin,123" | 
        csvtool call "printf '"%s",%s,%s,%s,"%s","%x"n'" -


        Output:



        "2018-03-18 4:56:17",255.255.255.255,00,ssh,"admin","7b"





        share|improve this answer






















        • +1 this is cool.
          – Î±Ò“sнιη
          Apr 12 at 6:42












        up vote
        4
        down vote










        up vote
        4
        down vote









        Using csvtool which has a helpful format command:



        csvtool format '"%1",%2,%3,%4,"%5","%6"n' file.csv 


        Example:



        echo "2018-03-18 4:56:17,255.255.255.255,00,ssh,admin,123" |
        csvtool format '"%1",%2,%3,%4,"%5","%6"n' -


        Output:



        "2018-03-18 4:56:17",255.255.255.255,00,ssh,"admin","123"



        csvtool also can call shell functions and external programs to parse lines from .CSV files. To do the same thing using printf, but print the "123" in hexadecimal, do:



        echo "2018-03-18 4:56:17,255.255.255.255,00,ssh,admin,123" | 
        csvtool call "printf '"%s",%s,%s,%s,"%s","%x"n'" -


        Output:



        "2018-03-18 4:56:17",255.255.255.255,00,ssh,"admin","7b"





        share|improve this answer














        Using csvtool which has a helpful format command:



        csvtool format '"%1",%2,%3,%4,"%5","%6"n' file.csv 


        Example:



        echo "2018-03-18 4:56:17,255.255.255.255,00,ssh,admin,123" |
        csvtool format '"%1",%2,%3,%4,"%5","%6"n' -


        Output:



        "2018-03-18 4:56:17",255.255.255.255,00,ssh,"admin","123"



        csvtool also can call shell functions and external programs to parse lines from .CSV files. To do the same thing using printf, but print the "123" in hexadecimal, do:



        echo "2018-03-18 4:56:17,255.255.255.255,00,ssh,admin,123" | 
        csvtool call "printf '"%s",%s,%s,%s,"%s","%x"n'" -


        Output:



        "2018-03-18 4:56:17",255.255.255.255,00,ssh,"admin","7b"






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Apr 12 at 6:35

























        answered Apr 12 at 5:56









        agc

        4,0091935




        4,0091935











        • +1 this is cool.
          – Î±Ò“sнιη
          Apr 12 at 6:42
















        • +1 this is cool.
          – Î±Ò“sнιη
          Apr 12 at 6:42















        +1 this is cool.
        – Î±Ò“sнιη
        Apr 12 at 6:42




        +1 this is cool.
        – Î±Ò“sнιη
        Apr 12 at 6:42












        up vote
        2
        down vote













        I might approach it with awk this way:



        1. set the output field separator to what the input field separator is, which we assign as the comma -F,

        2. for each line, reassign the values of fields 1, 5, and 6 as being the original values but surrounded by double-quotes. The apparent quoting mess is because I used double-quotes to create the surrounding string, and because the only string I want to print is a double-quote, I have to escape it, so each double-quote that I want ends up being """.

        3. Once the fields have been updated, print the newly-combined string.

        The script is:



        awk -F, 'BEGIN OFS=FS $1="""$1"""; $5="""$5"""; $6="""$6"""; print ' < input.csv > output.csv


        As you find that you need to quote more fields, simply do the same thing as with fields 1, 5, and 6 above.






        share|improve this answer
























          up vote
          2
          down vote













          I might approach it with awk this way:



          1. set the output field separator to what the input field separator is, which we assign as the comma -F,

          2. for each line, reassign the values of fields 1, 5, and 6 as being the original values but surrounded by double-quotes. The apparent quoting mess is because I used double-quotes to create the surrounding string, and because the only string I want to print is a double-quote, I have to escape it, so each double-quote that I want ends up being """.

          3. Once the fields have been updated, print the newly-combined string.

          The script is:



          awk -F, 'BEGIN OFS=FS $1="""$1"""; $5="""$5"""; $6="""$6"""; print ' < input.csv > output.csv


          As you find that you need to quote more fields, simply do the same thing as with fields 1, 5, and 6 above.






          share|improve this answer






















            up vote
            2
            down vote










            up vote
            2
            down vote









            I might approach it with awk this way:



            1. set the output field separator to what the input field separator is, which we assign as the comma -F,

            2. for each line, reassign the values of fields 1, 5, and 6 as being the original values but surrounded by double-quotes. The apparent quoting mess is because I used double-quotes to create the surrounding string, and because the only string I want to print is a double-quote, I have to escape it, so each double-quote that I want ends up being """.

            3. Once the fields have been updated, print the newly-combined string.

            The script is:



            awk -F, 'BEGIN OFS=FS $1="""$1"""; $5="""$5"""; $6="""$6"""; print ' < input.csv > output.csv


            As you find that you need to quote more fields, simply do the same thing as with fields 1, 5, and 6 above.






            share|improve this answer












            I might approach it with awk this way:



            1. set the output field separator to what the input field separator is, which we assign as the comma -F,

            2. for each line, reassign the values of fields 1, 5, and 6 as being the original values but surrounded by double-quotes. The apparent quoting mess is because I used double-quotes to create the surrounding string, and because the only string I want to print is a double-quote, I have to escape it, so each double-quote that I want ends up being """.

            3. Once the fields have been updated, print the newly-combined string.

            The script is:



            awk -F, 'BEGIN OFS=FS $1="""$1"""; $5="""$5"""; $6="""$6"""; print ' < input.csv > output.csv


            As you find that you need to quote more fields, simply do the same thing as with fields 1, 5, and 6 above.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Apr 12 at 1:10









            Jeff Schaller

            31.1k846105




            31.1k846105




















                up vote
                1
                down vote















                With perl



                $ perl -F, -lane 'map $_=qq("$_") @F[0,4,5]; print join ",", @F' ip.txt
                "2018-03-18 4:56:17",255.255.255.255,00,ssh,"admin","123"



                • -F, use , as input field delimiter, results available in @F array


                • map $_=qq("$_") @F[0,4,5] double quote required array elements. Index starts from 0. The qq operator is used here to avoid escaping double quotes, qq("$_") is same as ""$_""


                • print join ",", @F print the modified array with , as separator



                Another way to do with awk



                $ awk -v q='"' 'BEGINsplit("1 5 6",a); FS=OFS=","
                for(i in a) $a[i]=q $a[i] q 1' ip.txt
                "2018-03-18 4:56:17",255.255.255.255,00,ssh,"admin","123"



                • -v q='"' save double quote as value in q variable


                • split("1 5 6",a) save index to be changed as values in a array (default split on whitespace, FS is not yet changed)


                • FS=OFS="," change input/output delimiter to ,


                • for(i in a) $a[i]=q $a[i] q change required fields


                • 1 idiomatic way to print contents of $0





                share|improve this answer


























                  up vote
                  1
                  down vote















                  With perl



                  $ perl -F, -lane 'map $_=qq("$_") @F[0,4,5]; print join ",", @F' ip.txt
                  "2018-03-18 4:56:17",255.255.255.255,00,ssh,"admin","123"



                  • -F, use , as input field delimiter, results available in @F array


                  • map $_=qq("$_") @F[0,4,5] double quote required array elements. Index starts from 0. The qq operator is used here to avoid escaping double quotes, qq("$_") is same as ""$_""


                  • print join ",", @F print the modified array with , as separator



                  Another way to do with awk



                  $ awk -v q='"' 'BEGINsplit("1 5 6",a); FS=OFS=","
                  for(i in a) $a[i]=q $a[i] q 1' ip.txt
                  "2018-03-18 4:56:17",255.255.255.255,00,ssh,"admin","123"



                  • -v q='"' save double quote as value in q variable


                  • split("1 5 6",a) save index to be changed as values in a array (default split on whitespace, FS is not yet changed)


                  • FS=OFS="," change input/output delimiter to ,


                  • for(i in a) $a[i]=q $a[i] q change required fields


                  • 1 idiomatic way to print contents of $0





                  share|improve this answer
























                    up vote
                    1
                    down vote










                    up vote
                    1
                    down vote











                    With perl



                    $ perl -F, -lane 'map $_=qq("$_") @F[0,4,5]; print join ",", @F' ip.txt
                    "2018-03-18 4:56:17",255.255.255.255,00,ssh,"admin","123"



                    • -F, use , as input field delimiter, results available in @F array


                    • map $_=qq("$_") @F[0,4,5] double quote required array elements. Index starts from 0. The qq operator is used here to avoid escaping double quotes, qq("$_") is same as ""$_""


                    • print join ",", @F print the modified array with , as separator



                    Another way to do with awk



                    $ awk -v q='"' 'BEGINsplit("1 5 6",a); FS=OFS=","
                    for(i in a) $a[i]=q $a[i] q 1' ip.txt
                    "2018-03-18 4:56:17",255.255.255.255,00,ssh,"admin","123"



                    • -v q='"' save double quote as value in q variable


                    • split("1 5 6",a) save index to be changed as values in a array (default split on whitespace, FS is not yet changed)


                    • FS=OFS="," change input/output delimiter to ,


                    • for(i in a) $a[i]=q $a[i] q change required fields


                    • 1 idiomatic way to print contents of $0





                    share|improve this answer
















                    With perl



                    $ perl -F, -lane 'map $_=qq("$_") @F[0,4,5]; print join ",", @F' ip.txt
                    "2018-03-18 4:56:17",255.255.255.255,00,ssh,"admin","123"



                    • -F, use , as input field delimiter, results available in @F array


                    • map $_=qq("$_") @F[0,4,5] double quote required array elements. Index starts from 0. The qq operator is used here to avoid escaping double quotes, qq("$_") is same as ""$_""


                    • print join ",", @F print the modified array with , as separator



                    Another way to do with awk



                    $ awk -v q='"' 'BEGINsplit("1 5 6",a); FS=OFS=","
                    for(i in a) $a[i]=q $a[i] q 1' ip.txt
                    "2018-03-18 4:56:17",255.255.255.255,00,ssh,"admin","123"



                    • -v q='"' save double quote as value in q variable


                    • split("1 5 6",a) save index to be changed as values in a array (default split on whitespace, FS is not yet changed)


                    • FS=OFS="," change input/output delimiter to ,


                    • for(i in a) $a[i]=q $a[i] q change required fields


                    • 1 idiomatic way to print contents of $0






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Apr 12 at 4:23

























                    answered Apr 12 at 4:15









                    Sundeep

                    6,9511826




                    6,9511826




















                        up vote
                        0
                        down vote













                        If you really want to use sed (which I don't recommend if awk or perl are available), then assuming that the sixth column is terminated by the line ending rather than a comma AND that fields do not themselves contain embedded (quoted) commas:



                        sed -E -e 's/([^,]*),/"1",/1' -e 's/([^,]*),/"1",/5' -e 's/([^,]*)$/"1"/' file
                        "2018-03-18 4:56:17",255.255.255.255,00,ssh,"admin","123"


                        The general pattern is s/([^,]*),/"1",/n where n refers to the nth occurrence of zero or more non-, characters followed by a , and 1 is a reference to the captured pattern (between parentheses).






                        share|improve this answer
























                          up vote
                          0
                          down vote













                          If you really want to use sed (which I don't recommend if awk or perl are available), then assuming that the sixth column is terminated by the line ending rather than a comma AND that fields do not themselves contain embedded (quoted) commas:



                          sed -E -e 's/([^,]*),/"1",/1' -e 's/([^,]*),/"1",/5' -e 's/([^,]*)$/"1"/' file
                          "2018-03-18 4:56:17",255.255.255.255,00,ssh,"admin","123"


                          The general pattern is s/([^,]*),/"1",/n where n refers to the nth occurrence of zero or more non-, characters followed by a , and 1 is a reference to the captured pattern (between parentheses).






                          share|improve this answer






















                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            If you really want to use sed (which I don't recommend if awk or perl are available), then assuming that the sixth column is terminated by the line ending rather than a comma AND that fields do not themselves contain embedded (quoted) commas:



                            sed -E -e 's/([^,]*),/"1",/1' -e 's/([^,]*),/"1",/5' -e 's/([^,]*)$/"1"/' file
                            "2018-03-18 4:56:17",255.255.255.255,00,ssh,"admin","123"


                            The general pattern is s/([^,]*),/"1",/n where n refers to the nth occurrence of zero or more non-, characters followed by a , and 1 is a reference to the captured pattern (between parentheses).






                            share|improve this answer












                            If you really want to use sed (which I don't recommend if awk or perl are available), then assuming that the sixth column is terminated by the line ending rather than a comma AND that fields do not themselves contain embedded (quoted) commas:



                            sed -E -e 's/([^,]*),/"1",/1' -e 's/([^,]*),/"1",/5' -e 's/([^,]*)$/"1"/' file
                            "2018-03-18 4:56:17",255.255.255.255,00,ssh,"admin","123"


                            The general pattern is s/([^,]*),/"1",/n where n refers to the nth occurrence of zero or more non-, characters followed by a , and 1 is a reference to the captured pattern (between parentheses).







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Apr 12 at 1:15









                            steeldriver

                            31.4k34978




                            31.4k34978






















                                 

                                draft saved


                                draft discarded


























                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f437164%2fis-it-possible-to-put-double-quotes-in-just-a-few-columns-using-sed%23new-answer', 'question_page');

                                );

                                Post as a guest













































































                                Popular posts from this blog

                                Peggy Mitchell

                                Palaiologos

                                The Forum (Inglewood, California)