How to filter a string against the characters from a variable?











up vote
2
down vote

favorite
1












I am trying to remove special characters (by passing them as a variable) from a string variable below command able to remove single characters not multiple?



string=#@$AAA%* 
a=#$@%* # Special characters which have to remove from variable
b=`echo $string|sed 's/${a}//g'`
echo $b









share|improve this question
























  • please also keep in mind to quote your variable that you should. read Why does my shell script choke on whitespace or other special characters?.
    – αғsнιη
    Dec 4 at 12:17

















up vote
2
down vote

favorite
1












I am trying to remove special characters (by passing them as a variable) from a string variable below command able to remove single characters not multiple?



string=#@$AAA%* 
a=#$@%* # Special characters which have to remove from variable
b=`echo $string|sed 's/${a}//g'`
echo $b









share|improve this question
























  • please also keep in mind to quote your variable that you should. read Why does my shell script choke on whitespace or other special characters?.
    – αғsнιη
    Dec 4 at 12:17















up vote
2
down vote

favorite
1









up vote
2
down vote

favorite
1






1





I am trying to remove special characters (by passing them as a variable) from a string variable below command able to remove single characters not multiple?



string=#@$AAA%* 
a=#$@%* # Special characters which have to remove from variable
b=`echo $string|sed 's/${a}//g'`
echo $b









share|improve this question















I am trying to remove special characters (by passing them as a variable) from a string variable below command able to remove single characters not multiple?



string=#@$AAA%* 
a=#$@%* # Special characters which have to remove from variable
b=`echo $string|sed 's/${a}//g'`
echo $b






bash variable string






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 4 at 12:07









αғsнιη

16.5k102765




16.5k102765










asked Dec 4 at 9:52









Harish a

233




233












  • please also keep in mind to quote your variable that you should. read Why does my shell script choke on whitespace or other special characters?.
    – αғsнιη
    Dec 4 at 12:17




















  • please also keep in mind to quote your variable that you should. read Why does my shell script choke on whitespace or other special characters?.
    – αғsнιη
    Dec 4 at 12:17


















please also keep in mind to quote your variable that you should. read Why does my shell script choke on whitespace or other special characters?.
– αғsнιη
Dec 4 at 12:17






please also keep in mind to quote your variable that you should. read Why does my shell script choke on whitespace or other special characters?.
– αғsнιη
Dec 4 at 12:17












2 Answers
2






active

oldest

votes

















up vote
2
down vote



accepted










There are a few things wrong with your commands.





  • string=#@$AAA%* and a=#$@%*: here the $AAA and $@ parts are interpreted as variables. You should single-quote the strings so that they are interpreted literally.

  • For the sed part, you are asking it to replace a literal string of #$@%* in order with nothing. If you want separate characters, you'd need to use a different format, e.g. the character class [#$@%*]. However, in this case, I'd just use tr -d instead, which is much simpler.

  • Also, $(…) is preferred over backticks.

  • I'd also use a here-string (<<<) instead of an echo, just to save a few cycles.

  • I noticed that Kusalananda edited my answer to add " quotes around variables. I deliberated over this too, but figured I wouldn't because it would be simpler, and not necessary for your specific question. However, I agree that this is the "right" way to do things. I've added " quotes around the other variables and the $(…) construct; in this case they are unnecessary, but also good practice.


Hence the final commands are



string='#@$AAA%*'
a='#$@%*'
b="$(<<<"$string" tr -d "$a")"
echo "$b"





share|improve this answer























  • You should've left it at @Kusalananda's edit. The extra quotes you added are absolutely useless -- unlike those around "$a" and "$b", they make no difference, no matter the value of string and a. Example: s='/b * .? * * b/'; a='/*'; b=$(<<<$s tr -d "$a"); echo "$b"
    – mosvy
    Dec 6 at 0:47












  • @mosvy Yes, good point… I guess my thinking was that Kusalananda's were also not necessary in this case either. Admittedly $b may well contain whitespace in another variant, but $a almost certainly would not (at least not in a way that would break). I agree that my quotes are even more useless, but I figured I'd use them for "good practice", in a general sense… which was likely Kusalananda's thinking? I've edited the question to point this out anyway.
    – Sparhawk
    Dec 6 at 0:59










  • the quotes around "$a" and "$b" are not just "good practice" (ie cargo cult). Try my example without either of them and you will see that they really make a difference.
    – mosvy
    Dec 6 at 1:02










  • @mosvy Ah yes okay. I forgot to use bash instead of my normal zsh. Yes, the * causes problems. Hopefully my edit includes this interpretation.
    – Sparhawk
    Dec 6 at 1:08


















up vote
6
down vote













No need to run external commands when using recent shells' "parameter expansion":



echo ${string//["$a"]}
AAA





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%2f485858%2fhow-to-filter-a-string-against-the-characters-from-a-variable%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








    up vote
    2
    down vote



    accepted










    There are a few things wrong with your commands.





    • string=#@$AAA%* and a=#$@%*: here the $AAA and $@ parts are interpreted as variables. You should single-quote the strings so that they are interpreted literally.

    • For the sed part, you are asking it to replace a literal string of #$@%* in order with nothing. If you want separate characters, you'd need to use a different format, e.g. the character class [#$@%*]. However, in this case, I'd just use tr -d instead, which is much simpler.

    • Also, $(…) is preferred over backticks.

    • I'd also use a here-string (<<<) instead of an echo, just to save a few cycles.

    • I noticed that Kusalananda edited my answer to add " quotes around variables. I deliberated over this too, but figured I wouldn't because it would be simpler, and not necessary for your specific question. However, I agree that this is the "right" way to do things. I've added " quotes around the other variables and the $(…) construct; in this case they are unnecessary, but also good practice.


    Hence the final commands are



    string='#@$AAA%*'
    a='#$@%*'
    b="$(<<<"$string" tr -d "$a")"
    echo "$b"





    share|improve this answer























    • You should've left it at @Kusalananda's edit. The extra quotes you added are absolutely useless -- unlike those around "$a" and "$b", they make no difference, no matter the value of string and a. Example: s='/b * .? * * b/'; a='/*'; b=$(<<<$s tr -d "$a"); echo "$b"
      – mosvy
      Dec 6 at 0:47












    • @mosvy Yes, good point… I guess my thinking was that Kusalananda's were also not necessary in this case either. Admittedly $b may well contain whitespace in another variant, but $a almost certainly would not (at least not in a way that would break). I agree that my quotes are even more useless, but I figured I'd use them for "good practice", in a general sense… which was likely Kusalananda's thinking? I've edited the question to point this out anyway.
      – Sparhawk
      Dec 6 at 0:59










    • the quotes around "$a" and "$b" are not just "good practice" (ie cargo cult). Try my example without either of them and you will see that they really make a difference.
      – mosvy
      Dec 6 at 1:02










    • @mosvy Ah yes okay. I forgot to use bash instead of my normal zsh. Yes, the * causes problems. Hopefully my edit includes this interpretation.
      – Sparhawk
      Dec 6 at 1:08















    up vote
    2
    down vote



    accepted










    There are a few things wrong with your commands.





    • string=#@$AAA%* and a=#$@%*: here the $AAA and $@ parts are interpreted as variables. You should single-quote the strings so that they are interpreted literally.

    • For the sed part, you are asking it to replace a literal string of #$@%* in order with nothing. If you want separate characters, you'd need to use a different format, e.g. the character class [#$@%*]. However, in this case, I'd just use tr -d instead, which is much simpler.

    • Also, $(…) is preferred over backticks.

    • I'd also use a here-string (<<<) instead of an echo, just to save a few cycles.

    • I noticed that Kusalananda edited my answer to add " quotes around variables. I deliberated over this too, but figured I wouldn't because it would be simpler, and not necessary for your specific question. However, I agree that this is the "right" way to do things. I've added " quotes around the other variables and the $(…) construct; in this case they are unnecessary, but also good practice.


    Hence the final commands are



    string='#@$AAA%*'
    a='#$@%*'
    b="$(<<<"$string" tr -d "$a")"
    echo "$b"





    share|improve this answer























    • You should've left it at @Kusalananda's edit. The extra quotes you added are absolutely useless -- unlike those around "$a" and "$b", they make no difference, no matter the value of string and a. Example: s='/b * .? * * b/'; a='/*'; b=$(<<<$s tr -d "$a"); echo "$b"
      – mosvy
      Dec 6 at 0:47












    • @mosvy Yes, good point… I guess my thinking was that Kusalananda's were also not necessary in this case either. Admittedly $b may well contain whitespace in another variant, but $a almost certainly would not (at least not in a way that would break). I agree that my quotes are even more useless, but I figured I'd use them for "good practice", in a general sense… which was likely Kusalananda's thinking? I've edited the question to point this out anyway.
      – Sparhawk
      Dec 6 at 0:59










    • the quotes around "$a" and "$b" are not just "good practice" (ie cargo cult). Try my example without either of them and you will see that they really make a difference.
      – mosvy
      Dec 6 at 1:02










    • @mosvy Ah yes okay. I forgot to use bash instead of my normal zsh. Yes, the * causes problems. Hopefully my edit includes this interpretation.
      – Sparhawk
      Dec 6 at 1:08













    up vote
    2
    down vote



    accepted







    up vote
    2
    down vote



    accepted






    There are a few things wrong with your commands.





    • string=#@$AAA%* and a=#$@%*: here the $AAA and $@ parts are interpreted as variables. You should single-quote the strings so that they are interpreted literally.

    • For the sed part, you are asking it to replace a literal string of #$@%* in order with nothing. If you want separate characters, you'd need to use a different format, e.g. the character class [#$@%*]. However, in this case, I'd just use tr -d instead, which is much simpler.

    • Also, $(…) is preferred over backticks.

    • I'd also use a here-string (<<<) instead of an echo, just to save a few cycles.

    • I noticed that Kusalananda edited my answer to add " quotes around variables. I deliberated over this too, but figured I wouldn't because it would be simpler, and not necessary for your specific question. However, I agree that this is the "right" way to do things. I've added " quotes around the other variables and the $(…) construct; in this case they are unnecessary, but also good practice.


    Hence the final commands are



    string='#@$AAA%*'
    a='#$@%*'
    b="$(<<<"$string" tr -d "$a")"
    echo "$b"





    share|improve this answer














    There are a few things wrong with your commands.





    • string=#@$AAA%* and a=#$@%*: here the $AAA and $@ parts are interpreted as variables. You should single-quote the strings so that they are interpreted literally.

    • For the sed part, you are asking it to replace a literal string of #$@%* in order with nothing. If you want separate characters, you'd need to use a different format, e.g. the character class [#$@%*]. However, in this case, I'd just use tr -d instead, which is much simpler.

    • Also, $(…) is preferred over backticks.

    • I'd also use a here-string (<<<) instead of an echo, just to save a few cycles.

    • I noticed that Kusalananda edited my answer to add " quotes around variables. I deliberated over this too, but figured I wouldn't because it would be simpler, and not necessary for your specific question. However, I agree that this is the "right" way to do things. I've added " quotes around the other variables and the $(…) construct; in this case they are unnecessary, but also good practice.


    Hence the final commands are



    string='#@$AAA%*'
    a='#$@%*'
    b="$(<<<"$string" tr -d "$a")"
    echo "$b"






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Dec 6 at 0:59

























    answered Dec 4 at 10:05









    Sparhawk

    9,20863890




    9,20863890












    • You should've left it at @Kusalananda's edit. The extra quotes you added are absolutely useless -- unlike those around "$a" and "$b", they make no difference, no matter the value of string and a. Example: s='/b * .? * * b/'; a='/*'; b=$(<<<$s tr -d "$a"); echo "$b"
      – mosvy
      Dec 6 at 0:47












    • @mosvy Yes, good point… I guess my thinking was that Kusalananda's were also not necessary in this case either. Admittedly $b may well contain whitespace in another variant, but $a almost certainly would not (at least not in a way that would break). I agree that my quotes are even more useless, but I figured I'd use them for "good practice", in a general sense… which was likely Kusalananda's thinking? I've edited the question to point this out anyway.
      – Sparhawk
      Dec 6 at 0:59










    • the quotes around "$a" and "$b" are not just "good practice" (ie cargo cult). Try my example without either of them and you will see that they really make a difference.
      – mosvy
      Dec 6 at 1:02










    • @mosvy Ah yes okay. I forgot to use bash instead of my normal zsh. Yes, the * causes problems. Hopefully my edit includes this interpretation.
      – Sparhawk
      Dec 6 at 1:08


















    • You should've left it at @Kusalananda's edit. The extra quotes you added are absolutely useless -- unlike those around "$a" and "$b", they make no difference, no matter the value of string and a. Example: s='/b * .? * * b/'; a='/*'; b=$(<<<$s tr -d "$a"); echo "$b"
      – mosvy
      Dec 6 at 0:47












    • @mosvy Yes, good point… I guess my thinking was that Kusalananda's were also not necessary in this case either. Admittedly $b may well contain whitespace in another variant, but $a almost certainly would not (at least not in a way that would break). I agree that my quotes are even more useless, but I figured I'd use them for "good practice", in a general sense… which was likely Kusalananda's thinking? I've edited the question to point this out anyway.
      – Sparhawk
      Dec 6 at 0:59










    • the quotes around "$a" and "$b" are not just "good practice" (ie cargo cult). Try my example without either of them and you will see that they really make a difference.
      – mosvy
      Dec 6 at 1:02










    • @mosvy Ah yes okay. I forgot to use bash instead of my normal zsh. Yes, the * causes problems. Hopefully my edit includes this interpretation.
      – Sparhawk
      Dec 6 at 1:08
















    You should've left it at @Kusalananda's edit. The extra quotes you added are absolutely useless -- unlike those around "$a" and "$b", they make no difference, no matter the value of string and a. Example: s='/b * .? * * b/'; a='/*'; b=$(<<<$s tr -d "$a"); echo "$b"
    – mosvy
    Dec 6 at 0:47






    You should've left it at @Kusalananda's edit. The extra quotes you added are absolutely useless -- unlike those around "$a" and "$b", they make no difference, no matter the value of string and a. Example: s='/b * .? * * b/'; a='/*'; b=$(<<<$s tr -d "$a"); echo "$b"
    – mosvy
    Dec 6 at 0:47














    @mosvy Yes, good point… I guess my thinking was that Kusalananda's were also not necessary in this case either. Admittedly $b may well contain whitespace in another variant, but $a almost certainly would not (at least not in a way that would break). I agree that my quotes are even more useless, but I figured I'd use them for "good practice", in a general sense… which was likely Kusalananda's thinking? I've edited the question to point this out anyway.
    – Sparhawk
    Dec 6 at 0:59




    @mosvy Yes, good point… I guess my thinking was that Kusalananda's were also not necessary in this case either. Admittedly $b may well contain whitespace in another variant, but $a almost certainly would not (at least not in a way that would break). I agree that my quotes are even more useless, but I figured I'd use them for "good practice", in a general sense… which was likely Kusalananda's thinking? I've edited the question to point this out anyway.
    – Sparhawk
    Dec 6 at 0:59












    the quotes around "$a" and "$b" are not just "good practice" (ie cargo cult). Try my example without either of them and you will see that they really make a difference.
    – mosvy
    Dec 6 at 1:02




    the quotes around "$a" and "$b" are not just "good practice" (ie cargo cult). Try my example without either of them and you will see that they really make a difference.
    – mosvy
    Dec 6 at 1:02












    @mosvy Ah yes okay. I forgot to use bash instead of my normal zsh. Yes, the * causes problems. Hopefully my edit includes this interpretation.
    – Sparhawk
    Dec 6 at 1:08




    @mosvy Ah yes okay. I forgot to use bash instead of my normal zsh. Yes, the * causes problems. Hopefully my edit includes this interpretation.
    – Sparhawk
    Dec 6 at 1:08












    up vote
    6
    down vote













    No need to run external commands when using recent shells' "parameter expansion":



    echo ${string//["$a"]}
    AAA





    share|improve this answer



























      up vote
      6
      down vote













      No need to run external commands when using recent shells' "parameter expansion":



      echo ${string//["$a"]}
      AAA





      share|improve this answer

























        up vote
        6
        down vote










        up vote
        6
        down vote









        No need to run external commands when using recent shells' "parameter expansion":



        echo ${string//["$a"]}
        AAA





        share|improve this answer














        No need to run external commands when using recent shells' "parameter expansion":



        echo ${string//["$a"]}
        AAA






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Dec 4 at 12:06

























        answered Dec 4 at 11:28









        RudiC

        3,9541312




        3,9541312






























            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f485858%2fhow-to-filter-a-string-against-the-characters-from-a-variable%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

            Quarter-circle Tiles

            build a pushdown automaton that recognizes the reverse language of a given pushdown automaton?

            Mont Emei