Reputation: 301
How can I replace a string in a file if line starts with another string using sed?
For example, replace this line:
connection = sqlite://keystone:[YOURPASSWORD]@[YOURIP]/keystone
With this line:
connection = mysql://keystone:[email protected]/keystone
Upvotes: 24
Views: 35946
Reputation: 522
If you want to change an entire line which starts with a pattern and ends with enything else, you can use command c
description Example
sed -i '/^connection = sqlite/c\connection = mysql://keystone:[email protected]/keystone' your_file
Upvotes: 5
Reputation: 15793
sed '/^string1/ { s,string2,string3, }' file
This will replace string2 with string3 on all the lines that start with string1.
Upvotes: 2
Reputation: 20980
Answer:
sed '/^start_string/s/search_string/replace_string/'
Information at http://www.gnu.org/software/sed/manual/sed.html#Addresses
Upvotes: 31