<p>Negation to the rescue:</p>

<pre><code>sed -i "s/server.address=[^\ ].*\ /server.address=${SERVER_ADDRESS} /g"
</code></pre>

<p>This is telling to search zero or more characters (<code>.*</code>) NOT matching whitespace (<code>[^\ ]</code>) up to the first whitespace.</p>

<p>A good pattern is not to use real values to be replaced (<code>10.111.11.11:3197</code>) but to use a mark like <code>###SERVER_ADDRESS###</code>. If something goes wrong you can easily post-check with</p>

<pre><code>grep "###" &amp;&amp; exit -1
</code></pre>

<p>This can avoid continuing with dangerous values (mixing dev/pre/pro values)</p>

CC BY-SA 4.0
2019-03-17T19:34:21.880Z
2019-04-02T04:06:24.447Z
2019-04-02T04:06:24.447Z
earizon
<p>You can do this using <strong>perl</strong></p>

<p>For Example</p>

<pre><code>perl -pi -e "s/server.address=localhost/server.address=${SERVER_ADDRESS}/g" file_path
</code></pre>

<p>This command change server address value localhost to vaariable value.</p>

<p>Hope this works</p>

CC BY-SA 4.0
2019-04-02T05:34:08.087Z
2019-04-02T05:34:08.087Z
Subhash
<p>My shell script looks like this:</p>

<pre><code>FAILED_REQUEST_DIRECTORY=/bla/bla2 \
java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=1234 \
     -jar /xyz/abc-service-1.0-SNAPSHOT.jar \
     --server.port=1111 \
     --server.address=127.0.0.1 \
     --kafka.bootstrap.servers=10.111.11.11:3197,10.112.12.111:3197,10.123.44.25:3197 \
     --topic.pushcrew.encryptionKey=abc-def-egh \
     --spring.application.name=name-sevice \
     --fb.verification.token=token \
     --management.trace.include.payload=true \
     --topic.pushcrew.hits=activity-logs \
     --max.block.ms=1000 \
     --pager.duty.api.key=apikey \
     --dsn=dsn
</code></pre>

<p>If I have to replace values of keys in the command, for ex: replacing value of key <code>--server.address</code> with the value to shell script through a variable, how would I do it?</p>

<p>I understand it would look like following if I had to replace value for key --server.address:</p>

<pre><code>sed -i "s#--server.address=.* #$SERVER_ADDRESS#g;
</code></pre>

<p>But how do I maintain the space that has to be there after the replacement and how do I make sure that part int the key only after <code>=</code>(equals to) and before  <code></code> (space) gets replaced.</p>

<shell-script><bash>
replacing values in shell script using sed
systemdebt
# replacing values in shell script using sed

(Asked by: username_0 on 2019-03-17)

My shell script looks like this:
FAILED_REQUEST_DIRECTORY=/bla/bla2 \
java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=1234 \
     -jar /xyz/abc-service-1.0-SNAPSHOT.jar \
     --server.port=1111 \
     --server.address=127.0.0.1 \
     --kafka.bootstrap.servers=10.111.11.11:3197,10.112.12.111:3197,10.123.44.25:3197 \
     --topic.pushcrew.encryptionKey=abc-def-egh \
     --spring.application.name=name-sevice \
     --fb.verification.token=token \
     --management.trace.include.payload=true \
     --topic.pushcrew.hits=activity-logs \
     --max.block.ms=1000 \
     --pager.duty.api.key=apikey \
     --dsn=dsn

If I have to replace values of keys in the command, for ex: replacing value of key --server.address with the value to shell script through a variable, how would I do it?
I understand it would look like following if I had to replace value for key --server.address:
sed -i "s#--server.address=.* #$SERVER_ADDRESS#g;

But how do I maintain the space that has to be there after the replacement and how do I make sure that part int the key only after =(equals to) and before   (space) gets replaced.

**Tags:** shell-script, bash


---

## 2 answers

### Answer by username_1 on 2019-03-17. Score: 2 | Accepted answer

Negation to the rescue:
sed -i "s/server.address=[^\ ].*\ /server.address=${SERVER_ADDRESS} /g"

This is telling to search zero or more characters (.*) NOT matching whitespace ([^\ ]) up to the first whitespace.
A good pattern is not to use real values to be replaced (10.111.11.11:3197) but to use a mark like ###SERVER_ADDRESS###. If something goes wrong you can easily post-check with
grep "###" && exit -1

This can avoid continuing with dangerous values (mixing dev/pre/pro values)


---

### Answer by username_2 on 2019-04-02. Score: 1

You can do this using perl
For Example
perl -pi -e "s/server.address=localhost/server.address=${SERVER_ADDRESS}/g" file_path

This command change server address value localhost to vaariable value.
Hope this works


---

