This question already has answers here:
Removing all special characters from a string in Bash
(3 answers)
Closed 3 years ago.
I would like to remove all special characters contained in a string. I tried some sample script but it doesn't remove all special characters.
echo "SamPlE_#tExT%, reééééally ?" | sed -e 's/[^a-z^A-Z]//g'
Output : tExTreééééaôlly
Expected : tExTreally
The simplest would be to run the command with the C locale:
echo "SamPlE_#tExT%, reééééally ?" | LANG=C sed 's/[^a-zA-Z]//g'
Output:
SamPlEtExTreally
Related
This question already has answers here:
How to use variables in a command in sed?
(4 answers)
Closed 9 months ago.
The community reviewed whether to reopen this question 9 months ago and left it closed:
Original close reason(s) were not resolved
I have this string in my file
$vnet_address
And i have in my bash run time this variable which i want to replace my string with:
$vnet_address=10.1.0.0/16
I get an error when i run this command:
sed -i "/\$vnet_address/ s//$vnet_address/"
And i believe it is because of the slash character '/' in my variable.
How to go about it?
You can use
sed -i "s,\\\$vnet_address,${vnet_address}," file
Note:
The regex delimiter character is replaced with ,
The $ is properly escaped with a literal \ character
See the online demo:
#!/bin/bash
s='$vnet_address'
vnet_address=10.1.0.0/16
sed "s,\\\$vnet_address,${vnet_address}," <<< "$s"
# => 10.1.0.0/16
This question already has answers here:
Linux sed command - using variable with backslash
(2 answers)
Closed 1 year ago.
I'm trying to replace all \ with \\ in bash, I'm doing it like this but bash gets stuck in a never-ending loop. Where am I going wrong?
myVar="${myVar//\//\\\\}"
You can use sed for that:
echo "hello\world\hello\world" | sed 's/\\/\\\\/g'
Outputs:
hello\\world\\hello\\world
This question already has answers here:
Replacing some characters in a string with another character
(6 answers)
Closed 3 years ago.
I need to replace the special character which is not alphanumeric with a backslash in a string.
How do i do it in Bash? My version is 4.1
I can capture the special character the plus symbol using the following regex
([^[:alnum:]])
For example, applied to the string
Alan5+6imson
I can do
$ echo $orig_str |sed 's/([^[:alnum:]])/\\1/g'
Alan5+6imson
I need the output as
Alan5\+6imson
How can I replace it in Bash?
I tried the above regex but not sure how to perform a replacement.
Do i need to use some other tool or something like sed?
Would you please try:
echo "$orig_str" | sed 's/\([^[:alnum:]]\)/\\\1/g'
or:
echo "$orig_str" | sed 's/[^[:alnum:]]/\\&/g'
This question already has answers here:
Use sed to replace all backslashes with forward slashes
(9 answers)
Closed 3 years ago.
How do I replace the special character \ (Backslash) to - (Dash) in bash script.
I have tried sed but didn't work.
whoami
example - when I run whoami the output is DomainName\User
Current output - DomainName\User
Expected output - DomainName-User
I reckon the escape character is missing
root#:~# echo "google\account" | sed 's/\\/-/'
google-account
This question already has answers here:
Escape a string for a sed replace pattern
(17 answers)
Closed 1 year ago.
I want to substitute a variable SERVICE with a string $service which contains a backslash using sed. I did the following
sed "s/SERVICE/`printf '%q' "${service}"`/g"
Using this I am getting the substituted string as
b_a^c_b_\]Wdd[]X\[X\[W206C?2#,.\\,A#2AW!w6"|
where as I want
b_a^c_b_\]Wdd[]X\[X\[W206C?2#,.\,A#2AW!w6"|
Is there any other way to do it.
PS(The string $service has many different special characters)
You might as well not use sed at all but just bash like this instead:
while read -r; do
echo "${REPLY//SERVICE/$service}"
done