Replace line in file if line starts from another line

How to replace a line in a file if a line starts from another line with sed?

For example, replace this line:

connection = sqlite://keystone:[YOURPASSWORD]@[YOURIP]/keystone 

Using this line:

 connection = mysql://keystone: password@10.1.1.10 /keystone 
+7
source share
4 answers

Answer:

 sed '/^start_string/s/search_string/replace_string/' 

Information at http://www.gnu.org/software/sed/manual/sed.html#Addresses

+13
source

You can do it simply:

 sed -ri 's/sqlite/mysql/g' YOURFILE 
+5
source
 sed '/^string1/ { s,string2,string3, }' file 

This will replace string2 with string3 on all lines starting with line1.

+1
source

If you want to change the entire line that starts with the template and ends with enything else, you can use the c description command Example

 sed -i '/^connection = sqlite/c\connection = mysql://keystone: password@10.1.1.10 /keystone' your_file 
+1
source

All Articles