-1

I am working on a script to read variables from different log files using a grep command and after reading the result, I want to add that variable into a txt file in front a predefixed text.

This is what i am doing

echo "whats your environmentprefix"

based on userinput, i am doing this

read $environmentprefix

Now this $environmentprefix value needs to be replaced in a config.txt file

where text is present as input.queue=_mq.test

I want to add this variable in front of this , so that it looks like

input.queue=$environmentprefix_mq.test

I have tried all sed commands but unable to get the desired output

David Buck
  • 3,752
  • 35
  • 31
  • 35
  • 1
    Does this answer your question? [How to use variable in sed command in shell](https://stackoverflow.com/questions/49691071/how-to-use-variable-in-sed-command-in-shell) – oguz ismail Nov 25 '19 at 00:14
  • 1
    `sed -i "s/input.queue=_mq.test/input.queue=${environmentprefix}_mq.test/" filename`? – David C. Rankin Nov 25 '19 at 00:15
  • Thanks , but that didnt woked, the result is coming as input.queue=_mq.test only the variable is not getting appended. Am i doing correct, that i am asking for a user input and then doing read $environmentprefix and using that $environmentprefix into the above sed command – VIR1983 Nov 25 '19 at 00:24
  • sample code posted should work. Be sure you copy/paste the variable name from your declaration, ie. `var=something; sed -i "s/input=xxx/input=${var}_mq.test/" file` And you seem to understand the difference between a single-quoted `sed` script versus a dbl-quoted version. Good luck. – shellter Nov 25 '19 at 00:58

1 Answers1

0

Please try the below code. It should work :

read -i "Whats your environment prefix : " environmentprefix
sed -i "s/input.queue *= *.*/input.queue=${environmentprefix}_mq.test/g" file

Regards!

Matias Barrios
  • 4,674
  • 3
  • 22
  • 49