Answers: <p>You can obtain old parameters from the properties file before using <strong>sed</strong>, e.g.</p>
  
  <pre><code># get old parameters
  $ export oldUsername=$(grep mysql.username $FILE | cut -d'=' -f2)
  $ export oldPass=$(grep mysql.pass $FILE | cut -d'=' -f2)
  
  # set new parameters
  $ export newUsername=NEW_USERNAME
  $ export newPass=NEW_PASS
  
  # change parameters
  $ sed -i "s/$oldUsername/$newUsername/g" $FILE
  $ sed -i "s/$oldPass/$newPass/g" $FILE
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2019-03-15T11:33:28.510Z
Answers: 2019-03-15T11:33:28.510Z
Answers: Pavel Stroitelev
Answers: <p>You should regenerate this file from a template using your configuration management system of choice.</p>
  
  <p>You could also write a shell script that uses a <a href="http://tldp.org/LDP/abs/html/here-docs.html" rel="nofollow noreferrer">heredoc</a> to do the same thing.  But since we're in DevOps-land you really should be using your configuration management system for these things.</p>
  
Answers: CC BY-SA 4.0
Answers: 2019-03-15T14:10:08.103Z
Answers: 2019-03-15T14:10:08.103Z
Answers: chicks
Answers: <p>You can use below. </p>
  
  <pre><code>[user@xyz ~]$ hh='mysql.username=USERNAME';echo $hh | awk -F"=" '{OFS=FS}{ $2="Mango"; print }'
  mysql.username=Mango
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2019-03-17T02:21:31.487Z
Answers: It would be nice if you could break down each section of the command. This would help people less experienced in bash and possibly help them fix similar problems in the future based on what they learn here.
Answers: Levi
Answers: CC BY-SA 4.0
Answers: 2019-03-15T11:42:24.493Z
Answers: 2019-03-15T11:42:24.493Z
Answers: again
Answers: <p>Maybe, this is something you can try - in sed they are known as back-references:</p>
  
  <p>$1 - is the value passed for username</p>
  
  <p>$2 - is the value passed for password</p>
  
  <p>\1 captures the back reference within \( and \)</p>
  
  <pre><code>sed -i "s/\(mysql\.username=\).*/\1$1" propertiesfile
  sed -i "s/\(mysql\.pass=\).*/\1$2" propertiesfile
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2019-03-18T03:46:32.257Z
Answers: 2019-03-18T03:46:32.257Z
Answers: rebelution
Body: <p>I have a properties file that looks like this:</p>
  
  <pre><code>mysql.username=USERNAME
  mysql.pass=PASS
  </code></pre>
  
  <p>I need to change <code>USERNAME</code> and <code>PASS</code> with variable values passed to shell script. I cannot use <code>sed</code> since that will work fine the first time to replace USERNAME and PASS with "values" but once they are replaced, sed will not find the variable names in the file the second time the script runs and thus an issue. </p>
  
  <p>How can this be handled? </p>
  
Tags: <shell-script><bash>
Title: shell script to pass values properties file in java
UserDisplayName: systemdebt
ThreadText: # shell script to pass values properties file in java
  
  (Asked by: username_0 on 2019-03-15)
  
  I have a properties file that looks like this:
  
  ```
  mysql.username=USERNAME
  mysql.pass=PASS
  
  ```
  
  I need to change `USERNAME` and `PASS` with variable values passed to shell script. I cannot use `sed` since that will work fine the first time to replace USERNAME and PASS with "values" but once they are replaced, sed will not find the variable names in the file the second time the script runs and thus an issue.
  
  How can this be handled?
  
  **Tags:** shell-script, bash
  
  
  ---
  
  ## 4 answers
  
  ### Answer by username_1 on 2019-03-15. Score: 1 | Accepted answer
  
  You can obtain old parameters from the properties file before using **sed**, e.g.
  
  ```
  # get old parameters
  $ export oldUsername=$(grep mysql.username $FILE | cut -d'=' -f2)
  $ export oldPass=$(grep mysql.pass $FILE | cut -d'=' -f2)
  
  # set new parameters
  $ export newUsername=NEW_USERNAME
  $ export newPass=NEW_PASS
  
  # change parameters
  $ sed -i "s/$oldUsername/$newUsername/g" $FILE
  $ sed -i "s/$oldPass/$newPass/g" $FILE
  
  ```
  
  
  ---
  
  ### Answer by username_2 on 2019-03-15. Score: 1
  
  You should regenerate this file from a template using your configuration management system of choice.
  
  You could also write a shell script that uses a [heredoc](http://tldp.org/LDP/abs/html/here-docs.html) to do the same thing. But since we're in DevOps-land you really should be using your configuration management system for these things.
  
  
  ---
  
  ### Answer by username_3 on 2019-03-15. Score: 1
  
  You can use below.
  
  ```
  [user@xyz ~]$ hh='mysql.username=USERNAME';echo $hh | awk -F"=" '{OFS=FS}{ $2="Mango"; print }'
  mysql.username=Mango
  
  ```
  
  > **Comments:**
  > **username_4**: It would be nice if you could break down each section of the command. This would help people less experienced in bash and possibly help them fix similar problems in the future based on what they learn here.  
  
  ---
  
  ### Answer by username_5 on 2019-03-18. Score: 0
  
  Maybe, this is something you can try - in sed they are known as back-references:
  
  $1 - is the value passed for username
  
  $2 - is the value passed for password
  
  \1 captures the back reference within \( and \)
  
  ```
  sed -i "s/\(mysql\.username=\).*/\1$1" propertiesfile
  sed -i "s/\(mysql\.pass=\).*/\1$2" propertiesfile
  
  ```
  
  
  ---
  
  