A
A
Alexey2022-02-01 23:38:03
linux
Alexey, 2022-02-01 23:38:03

How to replace a string with double quotes and a variable in sed?

Hello dear experts.
It is required using sed to replace the line, and even with a variable that could not beat for an hour, it just does not change :)

The code is something like this:

#!/bin/bash
IP=`wget -qO- someurl.com`
GMT=`wget -qO- someurl.com?$IP`
OLD="TimeZone="600""
REP="TimeZone="$GMT""
sed -i "s/$OLD/$REP/" test.txt


The attempt was like this:
sed -i "s|TimeZone=\x22600\x22|TimeZone=\x22$GMT\x22|g" test.txt


Content of test.txt : Tried at least this way, but in general you should always change the value even if it's not 600 , but any Help, please)
<TimeConfig TimeMode="NTP" TimeZone="600">

TimeZone=

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Saboteur, 2022-02-02
@edlirius

#!/bin/bash
IP=$(wget -qO- someurl.com)
GMT=(wget -qO- someurl.com?$IP)
OLD="TimeZone=\"600\""
REP="TimeZone=\"$GMT\""
sed -i "s/$OLD/$REP/" test.txt

sed  "s/TimeZone=\"[^\"]*\"/TimeZone=\"$GMT\"/g" test.txt

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question