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
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:
How to insert strings containing slashes with sed? [duplicate]
(11 answers)
Closed 3 years ago.
I am trying the below code to replace the string /IRM/I with E/IRM/I but am getting the file processed with no error and no transformation. I assume I'm using the cancel character incorrectly to allow the forward slash. Any help is much appreciated.
sed -i '/\/IRM\/IE\/IRM\/I/g'
A sed command needs to specify an operation (like s to replace), and that operation requires a sigil. You don't need to use a slash as that sigil.
printf '%s\n' 'This is a test: </IRM/I>' | \
sed -e 's#/IRM/I#E/IRM/I#g'
...correctly emits as output:
This is a test: <E/IRM/I>
Note that we added a s at the beginning of your sed expression, and followed it up with a # -- a sigil that isn't contained anywhere in the source or replacement strings, so you don't need to escape it as you would /.
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
This question already has answers here:
Use a variable's value in a sed command [duplicate]
(6 answers)
sed substitution with Bash variables
(6 answers)
Closed 4 years ago.
i am just trying to append a line from my bash script:
sed -i '$ a TEXT' "filename"
but when TEXT is a variable it does not work
sed -i '$ a $VARIABLE' "filename"
i have tested endless iterations with ',''," before/after $ and ${}, escape with \ etc., but i always get a literal $VARIABLE in my file
ps. please do not propose to use echo:)