How to filter a string against the characters from a variable?
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
add a comment |
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
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
add a comment |
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
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
bash variable string
edited Dec 4 at 12:07
αғsнιη
16.5k102865
16.5k102865
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
There are a few things wrong with your commands.
string=#@$AAA%*
anda=#$@%*
: 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 usetr -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"
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 ofstring
anda
. 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
add a comment |
No need to run external commands when using recent shells' "parameter expansion":
echo ${string//["$a"]}
AAA
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
There are a few things wrong with your commands.
string=#@$AAA%*
anda=#$@%*
: 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 usetr -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"
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 ofstring
anda
. 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
add a comment |
There are a few things wrong with your commands.
string=#@$AAA%*
anda=#$@%*
: 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 usetr -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"
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 ofstring
anda
. 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
add a comment |
There are a few things wrong with your commands.
string=#@$AAA%*
anda=#$@%*
: 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 usetr -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"
There are a few things wrong with your commands.
string=#@$AAA%*
anda=#$@%*
: 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 usetr -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"
edited Dec 6 at 0:59
answered Dec 4 at 10:05
Sparhawk
9,25863991
9,25863991
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 ofstring
anda
. 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
add a comment |
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 ofstring
anda
. 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
add a comment |
No need to run external commands when using recent shells' "parameter expansion":
echo ${string//["$a"]}
AAA
add a comment |
No need to run external commands when using recent shells' "parameter expansion":
echo ${string//["$a"]}
AAA
add a comment |
No need to run external commands when using recent shells' "parameter expansion":
echo ${string//["$a"]}
AAA
No need to run external commands when using recent shells' "parameter expansion":
echo ${string//["$a"]}
AAA
edited Dec 4 at 12:06
answered Dec 4 at 11:28
RudiC
4,1291312
4,1291312
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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