Regular expression to remove all punctuation except commas in regex awk












0















I have a variable



 local= "[  'service center','New'  ]"


I have used the following awk code to remove the square braces and single quotes,



 local=gensub(/[[]']+/, "", "g", local);


Expected O/P is



local ="  service center,New  "


The code I wrote is not working










share|improve this question




















  • 1





    Please provide some more context. I think you might better of using a json parser instead of trying to remove characters from your string. And please fix your code as it's not valid.

    – RoVo
    Feb 1 at 14:53











  • Can you see my updated question

    – mittu
    Feb 1 at 14:58











  • Still no context. And if it's all inside awk, you might want to remove the shell tag from the question.

    – RoVo
    Feb 1 at 15:08













  • What exactly is not working. The AWK substitution local=gensub(/[[]']+/, "", "g", local); produces exactly the expected value. The expression /[^,[:punct:][:space:]]/ you removed with your edit does not match "([:punct:] or [:space:]) and not ," but "not ([:punct:] or [:space:] or ,)".

    – Bodo
    Feb 1 at 15:09






  • 1





    @mittu Works for me. I think you should edit your question to include a complete, piece of awk code that exhibits the issue so any readers can repeat it.

    – ilkkachu
    Feb 1 at 15:36
















0















I have a variable



 local= "[  'service center','New'  ]"


I have used the following awk code to remove the square braces and single quotes,



 local=gensub(/[[]']+/, "", "g", local);


Expected O/P is



local ="  service center,New  "


The code I wrote is not working










share|improve this question




















  • 1





    Please provide some more context. I think you might better of using a json parser instead of trying to remove characters from your string. And please fix your code as it's not valid.

    – RoVo
    Feb 1 at 14:53











  • Can you see my updated question

    – mittu
    Feb 1 at 14:58











  • Still no context. And if it's all inside awk, you might want to remove the shell tag from the question.

    – RoVo
    Feb 1 at 15:08













  • What exactly is not working. The AWK substitution local=gensub(/[[]']+/, "", "g", local); produces exactly the expected value. The expression /[^,[:punct:][:space:]]/ you removed with your edit does not match "([:punct:] or [:space:]) and not ," but "not ([:punct:] or [:space:] or ,)".

    – Bodo
    Feb 1 at 15:09






  • 1





    @mittu Works for me. I think you should edit your question to include a complete, piece of awk code that exhibits the issue so any readers can repeat it.

    – ilkkachu
    Feb 1 at 15:36














0












0








0








I have a variable



 local= "[  'service center','New'  ]"


I have used the following awk code to remove the square braces and single quotes,



 local=gensub(/[[]']+/, "", "g", local);


Expected O/P is



local ="  service center,New  "


The code I wrote is not working










share|improve this question
















I have a variable



 local= "[  'service center','New'  ]"


I have used the following awk code to remove the square braces and single quotes,



 local=gensub(/[[]']+/, "", "g", local);


Expected O/P is



local ="  service center,New  "


The code I wrote is not working







shell awk regular-expression






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 1 at 14:58







mittu

















asked Feb 1 at 14:01









mittumittu

475




475








  • 1





    Please provide some more context. I think you might better of using a json parser instead of trying to remove characters from your string. And please fix your code as it's not valid.

    – RoVo
    Feb 1 at 14:53











  • Can you see my updated question

    – mittu
    Feb 1 at 14:58











  • Still no context. And if it's all inside awk, you might want to remove the shell tag from the question.

    – RoVo
    Feb 1 at 15:08













  • What exactly is not working. The AWK substitution local=gensub(/[[]']+/, "", "g", local); produces exactly the expected value. The expression /[^,[:punct:][:space:]]/ you removed with your edit does not match "([:punct:] or [:space:]) and not ," but "not ([:punct:] or [:space:] or ,)".

    – Bodo
    Feb 1 at 15:09






  • 1





    @mittu Works for me. I think you should edit your question to include a complete, piece of awk code that exhibits the issue so any readers can repeat it.

    – ilkkachu
    Feb 1 at 15:36














  • 1





    Please provide some more context. I think you might better of using a json parser instead of trying to remove characters from your string. And please fix your code as it's not valid.

    – RoVo
    Feb 1 at 14:53











  • Can you see my updated question

    – mittu
    Feb 1 at 14:58











  • Still no context. And if it's all inside awk, you might want to remove the shell tag from the question.

    – RoVo
    Feb 1 at 15:08













  • What exactly is not working. The AWK substitution local=gensub(/[[]']+/, "", "g", local); produces exactly the expected value. The expression /[^,[:punct:][:space:]]/ you removed with your edit does not match "([:punct:] or [:space:]) and not ," but "not ([:punct:] or [:space:] or ,)".

    – Bodo
    Feb 1 at 15:09






  • 1





    @mittu Works for me. I think you should edit your question to include a complete, piece of awk code that exhibits the issue so any readers can repeat it.

    – ilkkachu
    Feb 1 at 15:36








1




1





Please provide some more context. I think you might better of using a json parser instead of trying to remove characters from your string. And please fix your code as it's not valid.

– RoVo
Feb 1 at 14:53





Please provide some more context. I think you might better of using a json parser instead of trying to remove characters from your string. And please fix your code as it's not valid.

– RoVo
Feb 1 at 14:53













Can you see my updated question

– mittu
Feb 1 at 14:58





Can you see my updated question

– mittu
Feb 1 at 14:58













Still no context. And if it's all inside awk, you might want to remove the shell tag from the question.

– RoVo
Feb 1 at 15:08







Still no context. And if it's all inside awk, you might want to remove the shell tag from the question.

– RoVo
Feb 1 at 15:08















What exactly is not working. The AWK substitution local=gensub(/[[]']+/, "", "g", local); produces exactly the expected value. The expression /[^,[:punct:][:space:]]/ you removed with your edit does not match "([:punct:] or [:space:]) and not ," but "not ([:punct:] or [:space:] or ,)".

– Bodo
Feb 1 at 15:09





What exactly is not working. The AWK substitution local=gensub(/[[]']+/, "", "g", local); produces exactly the expected value. The expression /[^,[:punct:][:space:]]/ you removed with your edit does not match "([:punct:] or [:space:]) and not ," but "not ([:punct:] or [:space:] or ,)".

– Bodo
Feb 1 at 15:09




1




1





@mittu Works for me. I think you should edit your question to include a complete, piece of awk code that exhibits the issue so any readers can repeat it.

– ilkkachu
Feb 1 at 15:36





@mittu Works for me. I think you should edit your question to include a complete, piece of awk code that exhibits the issue so any readers can repeat it.

– ilkkachu
Feb 1 at 15:36










2 Answers
2






active

oldest

votes


















1














You can replace commas with a non-punctuation character, remove all remaining puctuation, then restore the commas:



awk -v  local="[  'service center','New'  ]" '
BEGIN {
gsub(/,/, SUBSEP, local)
gsub(/[[:punct:]]/, "", local)
gsub(SUBSEP, ",", local)
printf("local="%s"n", local)
}'




local="  service center,New  "


Here, I'm using the builtin SUBSEP variable, described in the GNU awk manual thusly:




The default value of SUBSEP is the string "34", which contains a nonprinting character that is unlikely to appear in an awk program or in most input data







share|improve this answer
























  • Thank you so much

    – mittu
    Feb 3 at 2:27



















1














Please find below command and it worked fine



cat filename
local= "[ 'service center','New' ]"

sed "s/[^a-zA-Z,= " ]//g" filename
output: local= " service center,New "


Command: sed "s/[^a-zA-Z,= " ]//g" filename





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%2f498139%2fregular-expression-to-remove-all-punctuation-except-commas-in-regex-awk%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    You can replace commas with a non-punctuation character, remove all remaining puctuation, then restore the commas:



    awk -v  local="[  'service center','New'  ]" '
    BEGIN {
    gsub(/,/, SUBSEP, local)
    gsub(/[[:punct:]]/, "", local)
    gsub(SUBSEP, ",", local)
    printf("local="%s"n", local)
    }'




    local="  service center,New  "


    Here, I'm using the builtin SUBSEP variable, described in the GNU awk manual thusly:




    The default value of SUBSEP is the string "34", which contains a nonprinting character that is unlikely to appear in an awk program or in most input data







    share|improve this answer
























    • Thank you so much

      – mittu
      Feb 3 at 2:27
















    1














    You can replace commas with a non-punctuation character, remove all remaining puctuation, then restore the commas:



    awk -v  local="[  'service center','New'  ]" '
    BEGIN {
    gsub(/,/, SUBSEP, local)
    gsub(/[[:punct:]]/, "", local)
    gsub(SUBSEP, ",", local)
    printf("local="%s"n", local)
    }'




    local="  service center,New  "


    Here, I'm using the builtin SUBSEP variable, described in the GNU awk manual thusly:




    The default value of SUBSEP is the string "34", which contains a nonprinting character that is unlikely to appear in an awk program or in most input data







    share|improve this answer
























    • Thank you so much

      – mittu
      Feb 3 at 2:27














    1












    1








    1







    You can replace commas with a non-punctuation character, remove all remaining puctuation, then restore the commas:



    awk -v  local="[  'service center','New'  ]" '
    BEGIN {
    gsub(/,/, SUBSEP, local)
    gsub(/[[:punct:]]/, "", local)
    gsub(SUBSEP, ",", local)
    printf("local="%s"n", local)
    }'




    local="  service center,New  "


    Here, I'm using the builtin SUBSEP variable, described in the GNU awk manual thusly:




    The default value of SUBSEP is the string "34", which contains a nonprinting character that is unlikely to appear in an awk program or in most input data







    share|improve this answer













    You can replace commas with a non-punctuation character, remove all remaining puctuation, then restore the commas:



    awk -v  local="[  'service center','New'  ]" '
    BEGIN {
    gsub(/,/, SUBSEP, local)
    gsub(/[[:punct:]]/, "", local)
    gsub(SUBSEP, ",", local)
    printf("local="%s"n", local)
    }'




    local="  service center,New  "


    Here, I'm using the builtin SUBSEP variable, described in the GNU awk manual thusly:




    The default value of SUBSEP is the string "34", which contains a nonprinting character that is unlikely to appear in an awk program or in most input data








    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Feb 1 at 20:29









    glenn jackmanglenn jackman

    51.5k572111




    51.5k572111













    • Thank you so much

      – mittu
      Feb 3 at 2:27



















    • Thank you so much

      – mittu
      Feb 3 at 2:27

















    Thank you so much

    – mittu
    Feb 3 at 2:27





    Thank you so much

    – mittu
    Feb 3 at 2:27













    1














    Please find below command and it worked fine



    cat filename
    local= "[ 'service center','New' ]"

    sed "s/[^a-zA-Z,= " ]//g" filename
    output: local= " service center,New "


    Command: sed "s/[^a-zA-Z,= " ]//g" filename





    share|improve this answer




























      1














      Please find below command and it worked fine



      cat filename
      local= "[ 'service center','New' ]"

      sed "s/[^a-zA-Z,= " ]//g" filename
      output: local= " service center,New "


      Command: sed "s/[^a-zA-Z,= " ]//g" filename





      share|improve this answer


























        1












        1








        1







        Please find below command and it worked fine



        cat filename
        local= "[ 'service center','New' ]"

        sed "s/[^a-zA-Z,= " ]//g" filename
        output: local= " service center,New "


        Command: sed "s/[^a-zA-Z,= " ]//g" filename





        share|improve this answer













        Please find below command and it worked fine



        cat filename
        local= "[ 'service center','New' ]"

        sed "s/[^a-zA-Z,= " ]//g" filename
        output: local= " service center,New "


        Command: sed "s/[^a-zA-Z,= " ]//g" filename






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Feb 1 at 18:07









        Praveen Kumar BSPraveen Kumar BS

        1,474138




        1,474138






























            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%2f498139%2fregular-expression-to-remove-all-punctuation-except-commas-in-regex-awk%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 make a Squid Proxy server?

            Is this a new Fibonacci Identity?

            19世紀