Rams Rams - 3 months ago 8
Bash Question

SED find a line with a regular expression and replace part of it

Case 1

I have a file with text as below

search_text_us_server=value.env_1.path_to_file
search_text_uk_server=value.env_1.path_to_file
search_text_jp_server=value.env_1.path_to_file
some_other_key=value.env_1.another_path


Now I want a sed command to find the lines which match the regular expression search_text_{any}_server and then replace
env_1
with
env_2
.

I tried with the following.

^search_text_[a-z_]\*_server.*$
to find the lines. But now I was struck on how to replace.

PS : I am not an expert in shell

Answer

Your regex is close. You can use:

sed -E 's/^(search_text_[a-z_]*_server=.*)env_1\./\1env_2\./' file

search_text_us_server=value.env_2.path_to_file
search_text_uk_server=value.env_2.path_to_file
search_text_jp_server=value.env_2.path_to_file
some_other_key=value.env_1.another_path
Comments