配置文件看起来像这样(我必须经历数百个):
define host {
host_name db.xxx.yyy
address 10.5.220.10
use yyy-server
parents router.xxx.yyy
}
define host {
host_name drac.vs2.xxx.yyy
address 10.5.220.48
use yyy-drac
parents router.xxx.yyy
}
define service {
use disk-opt-service
host_name db.xxx.yyy
}
我的工作是删除包含db.xxx.yyy的所有块 所以结果看起来就像这样(删除了顶部和底部块,因为它们在块中都包含db.xxx.yyy):
define host {
host_name drac.vs2.xxx.yyy
address 10.5.220.48
use yyy-drac
parents router.xxx.yyy
}
答案 0 :(得分:1)
sed solution:
Regex fomrula = new Regex(@"^[a-zA-Z0-9_\[])(,\-.'");
<title>Replace with text here on server</title>
<meta name="description" content="Replace with text here on server">
- treat the input as a set of lines, each terminated by a zero byte (the ASCII ‘NUL’ character) instead of a newlineThe output:
Process_Request(file, content)
{
if (file == index.html)
{
content.replace("<title>xxx</title>", "<title>This is what I want title to be</title>"));
...same replace for description
}
return content to client;
}
答案 1 :(得分:0)
Awk One-Liner:
awk -v RS='}\n' '/db.xxx/{next} {print $0,RS}' file
输出:
define host {
host_name drac.vs2.xxx.yyy
address 10.5.220.48
use yyy-drac
parents router.xxx.yyy
}
答案 2 :(得分:0)
每当你在输入中有值命名映射的名称时,最强大且易于扩展的解决方案是首先创建一个数组(下面为ASP.NET Identity
)以将这些名称保存到值映射,然后只需通过它们访问值名称,例如:
f[]
这将适用于任何UNIX系统上的任何awk,因为它在特定字段上进行字符串比较而不是整个块上的正则表达式比较,所以它不会被您正在寻找的值所迷惑用于显示错误字段和/或正则表达式元字符和/或部分匹配,显然您可以轻松更改它以查找字段组合或您需要测试的任何其他内容。它也只能一次读取一个块进入内存,因此无论输入文件有多大,它都能正常工作。
$ cat tst.awk
/define.*{/ { inBlock=1 }
inBlock {
f[$1] = $2
block = block $0 ORS
if ( /}/ ) {
if ( f["host_name"] != "db.xxx.yyy" ) {
printf "%s", block
}
inBlock = block = ""
delete f
}
next
}
{ print }
$ cat file
When chapman billies leave the street,
And drouthy neibors, neibors, meet;
As market days are wearing late,
And folk begin to tak the gate,
While we sit bousing at the nappy,
An' getting fou and unco happy,
define host {
host_name db.xxx.yyy
address 10.5.220.10
use yyy-server
parents router.xxx.yyy
}
define host {
host_name drac.vs2.xxx.yyy
address 10.5.220.48
use yyy-drac
parents router.xxx.yyy
}
define service {
use disk-opt-service
host_name db.xxx.yyy
}
We think na on the lang Scots miles,
The mosses, waters, slaps and stiles,
That lie between us and our hame,
Where sits our sulky, sullen dame,
Gathering her brows like gathering storm,
Nursing her wrath to keep it warm.
- Tam O'Shanter by Robert Burns
答案 3 :(得分:0)
这可能适合你(GNU sed):
sed '/^\s*define/{:a;N;/^\s*}/M!ba;/db\.xxx\.yyy/d}' file
匹配以define
开头的行,并在其中添加更多行,直到匹配以}
开头的行。如果这些行包含字符串db.xxx.yyy
,请删除它们。