我想删除功能'engine" map" {..." foobar" ...}'。
我尝试了很多方法,这很困难,因为它的末尾有空行和'}',分隔符不起作用
mainfunc {
var ="baz"
engine"map" {
func {
var0 ="foo"
border = { 1, 1, 1, 1 }
var1 ="bar"
}
}
}
mainfunc {
var ="baz"
engine"map" {
func {
var0 ="foo"
border = { 1, 1, 1, 1 }
var1 ="foobar"
}
}
}
... # more functions like 'mainfunc'
我试过了
sed '/engine/,/^\s\s}$/d' file
但是删除了每个引擎功能,我只需要一个包含" foobar"的功能,也许一个模式匹配所有内容,甚至换行符,直到foobar这样的东西:
sed '/engine(.*)foobar/,/^\s\s}$/d' file
这是可能的?
尝试:
sed '/engine/{:a;N;/foobar/{N;N;d};/ }/b;ba}' filename
要么:
awk '/engine/{c=1}c{b=b?b"
"$0:$0;if(/{/)a++;if(/}/)a--;if(!a){if(b!~/foobar/)print b;c=0;b="";next}}!c' filename
伙计,你摇滚! 谢谢!
当您匹配engine"map"时,我会简单地计算开/闭括号的数量,无法说这是否仅适用于gawk
awk '
/^[ \t]*engine"map"/ {
ship=1; # ship is used as a boolean
b=0 # The factor between open / close brackets
}
ship {
b += split($0, tmp,"{"); # Count numbers of { in line
b -= split($0, tmp,"}"); # Count numbers of } in line
# If open / close brackets are equal the function ends
if(b==0) {
ship = 0;
}
# Ship the rest (printing)
next;
}
1 # Print line
' file
拆分返回匹配数:split(string, array [, fieldsep [, seps ] ]):
Divide
string into pieces defined by fieldpat
and store the pieces in array and the separator strings in the
seps array. The first piece is stored in
array[1], the second piece in array[2], and so
forth. The third argument, fieldpat, is
a regexp describing the fields in string (just as FPAT is
a regexp describing the fields in input records).
It may be either a regexp constant or a string.
If fieldpat is omitted, the value of FPAT is used.
patsplit() returns the number of elements created.