简体   繁体   English

多线 sed 替换【暂定】

[英]Multi-line sed replacement [on hold]

I've been struggling with sed and regexp while dealing with something a little tricky.在处理一些棘手的事情时,我一直在努力使用sed和正则表达式。 I want to replace strings in a configuration file, which looks something similar to我想替换配置文件中的字符串,它看起来类似于

configuration {
     key 0   value0
     key 1   value1
     key 2   value2
}

I want to be able to replace the content inside the brackets.我希望能够替换括号内的内容。 There are multiple configuration{} occurrence in the file, however I only ever want to capture the first one.文件中有多个configuration{} ,但我只想捕获第一个。 Note that I also know the key names in advance.请注意,我也事先知道键名。

I thought of two implementation solution, the first one is the delete everything inside the brackets and to input my data.我想到了两个实现解决方案,第一个是删除括号内的所有内容并输入我的数据。 The second one is to capture each line inside the brackets and replace them with my data.第二个是捕获括号内的每一行并用我的数据替换它们。

My main issue is that I'm really struggling with the multi-line.我的主要问题是我真的在多线问题上苦苦挣扎。 I've tried a lot of things, but nothing close, and couldn't find help I understand online.我已经尝试了很多东西,但没有关闭,并且无法在网上找到我理解的帮助。 If someone had a detailed solution, that would be really great.如果有人有详细的解决方案,那就太好了。

Also, I'm open to other solutions that would not be sed , as long as they can work on a bash script!另外,我对其他不是sed的解决方案持开放态度,只要它们可以在 bash 脚本上工作!

Thanks in advance!提前致谢!

sed cannot capture over newlines, it can only insert them in the new pattern. sed 无法捕获换行符,它只能将它们插入到新模式中。 You need to use awk for this.为此,您需要使用 awk。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM