linux中使用awk查看删除java,关于linux:使用sed或awk从文件中删除功能

我想删除功能'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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值