Skip to content
Advertisement

Can we replace one variable with multiple variables using sed

I have a config file where i need to replace the entire line with new values. It can be either a word or a URL. I am trying to write a script to replace only this particular parameter with new values.

I have tried using grep to extract the line using the parameter and divided the values separately and saved in two different variables. Now I am trying to replace the whole line with the parameter along with new value or url usind sed

JavaScript

The config file contains:

JavaScript

Here I am trying to replace the url of only abc1. though both have same value. I need to replace the entire url with something different url or a word.

Here I am trying to find the line which contains abc1 and this line url after = is saved in a different variable.

I tried to replace the url with new one using sed:

JavaScript

Seems like I am doing wrong at some syntax when using sed

trying to replace something like

JavaScript

Expecting an output like

JavaScript

Advertisement

Answer

The following script with comments:

JavaScript

produces the following output:

JavaScript

Notes:

  • I used the 0x01 byte as the separator for s command inside sed. So it should work with all printable characters.
  • Remember about quoting. All variables should be inside " double quotes, but all the rest would be best if inside ' single quotes.
  • I match the name= with ^, so from the beginning of the line. So that for example blablaabc1= doesn’t match.
  • I use ANSI-C Quoting from bash to generate the unredable 0x01 bytes to delimit sed command.

A little more readable version of the sed script could be this one, that just uses " for quoting and uses ~ as a separator:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement