SED,awk.grep - 添加一行到配置节结束时,如果该行不存在 [英] sed,awk.grep - add a line to the end of a configuration section if the line doesn't already exist
本文介绍了SED,awk.grep - 添加一行到配置节结束时,如果该行不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用SED / AWK编辑一个文件。该文件是由一些配置,如:
I want to edit a file using sed/awk. The file is made up of several configuration sections, like this:
SECTION 1 BEGIN
some stuff
SECTION END
SECTION 2 BEGIN
some stuff
some more stuff
important line
SECTION END
我想重要的行
添加到第2部分
,如果它不存在,$结束p $ pferably作为命令单行。我一直在寻找的比fgrep / sed的组合<一个href=\"http://stackoverflow.com/questions/9118553/add-a-line-to-a-file-section-unless-it-exists?answertab=active#tab-top\">in这个问题,但我不太明白如何适应它我需要什么。
I want to add important line
to the end of SECTION 2
if it doesn't already exist, preferably as a command one liner. I've been looking at the fgrep/sed combo in this question, but I don't quite understand how to adapt it for what I need.
注:有可能在部分空行
非常感谢。
推荐答案
使用 AWK
:
awk '
$0 == "SECTION 2 BEGIN" { inSec2 = 1 }
inSec2 && $0 == "important line" { hasImportant = 1 }
inSec2 && $0 == "SECTION END" {
if (!hasImportant) { print "important line" }
inSec2 = 0
}
1'
这篇关于SED,awk.grep - 添加一行到配置节结束时,如果该行不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文