0

I have elasticsearch.yml file.

I need to update path.data and path.logs value based on user input.

I wrote .sh script to take path as argument and replace string but it replaces with empty value.

Under script am using this

sed -i.bak -e 's/# path.data:.*/  path.data: $2 /' "$1"
sed -i.bak -e 's/# path.logs:.*/  path.logs: $3 /' "$1"

Example : ./updateconfig.sh /usr/local/elasticsearch/config/elasticsearch.yml /var/lib/elasticsearch /var/log/elasticsearch

I tried with double quotes (") too but no luck.

Can you please help me on this.

Update

Adding example and error when trying with different options

Example :

sed -i.bak -e "s/# path.data:.*/  path.data: $2 /" "$1"
sed -i.bak -e 's># path.data:.*>  path.data: $2 >' "$1"
sed -i.bak -e "'s># path.data:.*>  path.data: $2 >'" "$1"

Errors:

sed: -e expression #1, char 32: unknown option to s' sed: -e expression #1, char 1: unknown command:''

Sunil Agarwal
  • 4,097
  • 5
  • 44
  • 80
  • You need to use double quotes, please show the example of your attempt at using them. – 123 Jun 16 '16 at 10:24
  • if i use double quotes, I get an error sed: -e expression #1, char 32: unknown option to `s' example : sed -i.bak -e "s/# path.data:.*/ path.data: $2 /" "$1" – Sunil Agarwal Jun 16 '16 at 10:28
  • 2
    Use a different delimiter – 123 Jun 16 '16 at 10:28
  • Tried with different delimeter but no luck. Again error sed: -e expression #1, char 1: unknown command: `'' – Sunil Agarwal Jun 16 '16 at 10:45
  • 2
    Please show an example of you using a different delimiter – 123 Jun 16 '16 at 10:47
  • This pretty much collects all the common `sed` errors in one question, so singling out a single duplicate is precarious. Quoting is explained in http://stackoverflow.com/questions/3306007/replace-a-string-in-shell-script-using-a-variable and using the wrong delimiter is in http://stackoverflow.com/questions/27787536/how-to-pass-a-variable-containing-slashes-to-sed – tripleee Jun 16 '16 at 11:23

1 Answers1

1

Was mangling somewhat with sed from the examples over here, and I was able to make these commands work, if the variables names are known in prior:-

After receiving the arguments in prior: $1,$2 and $3

sed -i -e "s|^path.data.*|path.data:$2$1|g" -e "s|^path.logs.*|path.logs:$3$1|g" file

Assuming my sample file is dummy like below but having actual variable names:-

path.data:/l/m/n
path.logs:/d/e/f

With the script, below am able to do in-place substitution of paths as needed in OP:-

#!/bin/bash

sed -i -e "s|^path.data.*|path.data:$2$1|g" -e "s|^path.logs.*|path.logs:$3$1|g" file

On running the script:-

$ ./script.sh /usr/local/elasticsearch/config/elasticsearch.yml /var/lib/elasticsearch /var/log/elasticsearch

On verifying the contents of the original file now:-

path.data:/var/lib/elasticsearch/usr/local/elasticsearch/config/elasticsearch.yml
path.logs:/var/log/elasticsearch/usr/local/elasticsearch/config/elasticsearch.yml

Thanks to tripleee's suggestion, I am able to do in-place substitution now!

Community
  • 1
  • 1
Inian
  • 80,270
  • 14
  • 142
  • 161
  • 1
    Just pass multiple `-e` options. See e.g. http://stackoverflow.com/questions/7657647/combining-2-sed-commands – tripleee Jun 16 '16 at 11:17