我怎么能代替使用bash在文件中,这个字符串? [英] How can I replace this string inside a file using bash?

查看:145
本文介绍了我怎么能代替使用bash在文件中,这个字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想编写一个bash脚本,在一个文本文件替换某些字符串,但我越来越挂了如何更换为变量的价值里面的内容。

Would like to write a bash script that replaces some strings in a text file, however I am getting hung up on how to replace the contents inside a 'value' for a variable.

假设我希望到一个文本文件中设置可能的值:

Suppose the value I wish to set inside a text file could be :

TCP_IN =123,35,995

..其中 123,35,995 可以是任何字符串(不只是用逗号的数字。

.. where 123,35,995 could be any string (not just numbers with commas.

我怎么能取代这个密钥对搜索 TCP_IN =*,并设置里面,其中 * 是价值,从bash脚本内?

How could I replace this keypair searching for TCP_IN = "*" and set the value inside where * is, from within a bash script ?

推荐答案

您可以使用

sed -E 's/(TCP_IN *= *")[^"]+/\1MyNewVal/g'

查看 IDEONE演示

正则表达式匹配和捕获到组1 TCP_IN 随后与零个或多个空格,后面有一个 = 符号,随后再次与零个或多个空格,然后 [^] + 之外的其他1个或更多字符匹配 [^ ...] 是一个*否定匹配所有字符,但这些焦炭类中定义的字符类)。

The regex matches and captures into Group 1 TCP_IN followed with zero or more spaces, followed with a = symbol, followed with again zero or more spaces, and then [^"]+ matches 1 or more characters other than a " ([^...] is a *negated character class that matches all characters but those defined inside the char class).

这篇关于我怎么能代替使用bash在文件中,这个字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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