使用 sed 替换字符串之间的文本 [英] Using sed to replace text between strings

查看:64
本文介绍了使用 sed 替换字符串之间的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 sed 替换文件中的 a.mysql.com 用于以下行

I'm trying to replace a.mysql.com in a file using sed for the following line

    'ENGINE': 'a.mysql.com', # MySQL host

如何在不删除评论条目的情况下替换文本?

How can I replace the text without removing the comment entry?

我尝试了下面的方法,但没有用

I tried the below but it isn't working

sed -i -e "s/\('ENGINE': ').*\([^']*\)/\1new.mysql.com\2/g" file.py

推荐答案

关注 sed 可能对你有所帮助.

Following sed may help you in same.

sed "s/\(.*: \)\('.*'\)\(.*\)/\1'my_next_text'\3/"   Input_file

输出如下.

'ENGINE': 'my_next_text', # MySQL host

也用编辑过的 OP 输入文件对其进行了测试,如下所示.

Tested it with edited Input_file of OP too as follows.

cat Input_file
    'ENGINE': 'a.mysql.com', # MySQL host

sed "s/\(.*: \)\('.*'\)\(.*\)/\1'my_next_text'\3/" Input_file
    'ENGINE': 'my_next_text', # MySQL host

如果 OP 想要检查其中包含字符串 ENGINE 的行,则可以执行以下操作.

IN case OP wants to check for line which has string ENGINE in it then following could be done.

sed "/    'ENGINE/s/\(.*: \)\('.*'\)\(.*\)/\1'my_next_text'\3/"   Input_file

这篇关于使用 sed 替换字符串之间的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆