如果行以另一个字符串开头,则替换文件中的字符串

时间:2022-09-20 22:26:47

How can I replace a string in a file if line starts with another string using sed?

如果使用sed使用另一个字符串开始行,如何替换文件中的字符串?

For example, replace this line:

例如,替换此行:

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

With this line:

有了这条线:

connection = mysql://keystone:password@10.1.1.10/keystone

4 个解决方案

#1


14  

Answer:

回答:

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

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

有关http://www.gnu.org/software/sed/manual/sed.html#Addresses的信息

#2


5  

You can do simply this :

你可以做到这一点:

sed -ri 's/sqlite/mysql/g' YOURFILE

#3


2  

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

如果要更改以模式开头并以其他方式结束的整行,可以使用命令c description示例

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

#4


1  

sed '/^string1/ { s,string2,string3, }' file

This will replace string2 with string3 on all the lines that start with string1.

这将在以string1开头的所有行上用string3替换string2。

#1


14  

Answer:

回答:

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

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

有关http://www.gnu.org/software/sed/manual/sed.html#Addresses的信息

#2


5  

You can do simply this :

你可以做到这一点:

sed -ri 's/sqlite/mysql/g' YOURFILE

#3


2  

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

如果要更改以模式开头并以其他方式结束的整行,可以使用命令c description示例

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

#4


1  

sed '/^string1/ { s,string2,string3, }' file

This will replace string2 with string3 on all the lines that start with string1.

这将在以string1开头的所有行上用string3替换string2。