Sed with variables [duplicate] - sed

This question already has answers here:
sed substitution with Bash variables
(6 answers)
Closed 1 year ago.
i have a config file with this format
http://link:port/username/password/1234
Im using this code to replace the Username1/Password1 with a different Username2/Password2
sed -i -e 's/\/Username1\/Password1/\/Username2\/Password2/g' /etc/config.cfg
Now i want to make this something like
$UsernameOLD = Username1
$PasswordOLD = Password1
$UsernameNEW = Username2
$PasswordNEW = Password2
sed -i -e 's/\/$UsernameOLD\/$PasswordOLD/\/$UsernameNEW\/$PasswordNEW/g' /etc/config.cfg
Could anyone help me getting this ready ?

I found the solution i had to use double quotes instead of '
The working command looks like this:
UsernameOLD=MyOldUsername
PasswordOLD=MyOldPassword
UsernameNEW=MyNewUsername
PasswordNEW=MyOldUsername
sed -i -e "s/\/$UsernameOLD\/$PasswordOLD/\/$UsernameNEW\/$PasswordNEW/" /etc/config.cfg

Related

Using sed from makefile to replace a string with a $ in it [duplicate]

This question already has answers here:
Write Dollar sign in sed command pattern inside makefile
(2 answers)
Closed 5 years ago.
I have a simple sed replacement for a text file:
sed -i 's/Temp_3/$t0/g' ./somefile.txt
When I use it in a bash script everything works fine.
However, if I try to use the same line from a makefile,
the $t is (probably) expanded into the empty string
(there is no variable called t in the makefile) and
Temp_3 is replaced with 0. What is the best way to solve this?
Thanks!
What happens is that make first does its substitutions,
in your case $t with nothing, then runs the command.
You can, or should ;), circumvent this problem by changing your command as follows: change $ with $$;
make will substitute the double $ sign with a single one.
sed -i 's/Temp_3/$$t0/g' ./somefile.txt

Trying to get the year out of a date with sed [duplicate]

This question already has answers here:
Why doesn't `\d` work in regular expressions in sed? [duplicate]
(3 answers)
Closed 5 years ago.
I'm using the following command to get the year out of a string using sed.
echo 1234-1-12 | sed -r 's/(\d{4})-\d{1,2}-\d{1,2}/\1/'
but somehow it returns the entire date instead of the year.
getting 1234-1-12
expecting 1234
Any ideas why it doesn't work?
Use the date command:
string="1234-1-12"
date -d "${string}" +%Y
Output:
1234
Simple grep approach:
echo "1234-1-12" | grep -o '^[^-]*'
The output:
1234

sed: -e expression #1, char 23: unknown option to `s' [duplicate]

This question already has an answer here:
sed fails with "unknown option to `s'" error [closed]
(1 answer)
Closed 7 years ago.
I'm trying to use sed to update a config file using a bash script. I have a similar sed command right above this one in the script that runs fine. I can't seem to figure out why this is breaking:
sed -i.bak \
-e s/"socketPath:'https://localhost:9091'"/"socketPath:'/socket'"/g \
$WEB_CONF
Any ideas?
The quotes and double quotes are causing problems.
You are using them in view of the slashes in the string.
In sed you can use another delimiter, such as a #.
sed -e 's#socketPath:https://localhost:9091#socketPath:/socket#g' \
$WEB_CONF
Escape your slashes in the pattern or use a different delimiter like this:
sed -i.bak \
-e s%"socketPath:'https://localhost:9091'"%"socketPath:'/socket'"%g \
$WEB_CONF
I am confused looking at the delimiters you have used so you can't blame sed for goofing up. When your dataset has / in them, it is often advised to use a different delimiters like # or _ (yes, sed supports various delimiters).
Also, your quoting looks abit off. The syntax should be:
sed -i.bak 's#substitute#replacement#g' "$WEB_CONF"
If your substitute and/or replacement has variables use " instead ' which will allow it to interpolate.

Sed (POSIX) coming from linux [duplicate]

This question already has answers here:
How to remove every other line with sed?
(5 answers)
Closed 8 years ago.
I am somewhat new to posix, and i can't use: sed '1~2p'
My goal is to skip every one line from line 1:
1
2
3
4
would become
1
3
I was wondering what is the posix equivalent of ~.
Code for sed:
sed -e n -e d file
or:
sed -e 'n;d' file
The simpler, portable solution would be:
awk 'NR%2' file
bash solution:
while read -r line; do
[ $((i++ % 2)) -eq 0 ] && echo "$line";
done < file

How to escape the forward slash in a sed command [duplicate]

This question already has answers here:
Using different delimiters in sed commands and range addresses
(3 answers)
Closed 1 year ago.
I'm writing a shell script with this command:
sed -e 's/OLD_ITEM/NEW_ITEM/g'
But I actually want to do something that includes a directory:
sed -e 's/FOLDER/OLD_ITEM/NEW_ITEM/g'
How do ignore the forward slash so that the entire line FOLDER/OLD_ITEM is read properly?
You don't have to use / as delimiter in sed regexps. You can use whatever character you like, as long as it doesn't appear in the regexp itself:
sed -e 's#FOLDER/OLD_ITEM#NEW_ITEM#g'
or
sed -e 's|FOLDER/OLD_ITEM|NEW_ITEM|g'
You need to escape the / as \/.
The escape (\) preceding a character tells the shell to interpret that character literally.
So use FOLDER\/OLD_ITEM
Escape it !
sed -e 's/FOLDER\/OLD_ITEM/NEW_ITEM/g'