分页: 1 / 2
求助:文本文件匹配删除
发表于 : 2012-02-08 22:08
由 nyfair
有个文本文件里面有很多这样的东西
ifneq ($(TEST) 1)
...
...
...
endif
有没有什么能把所有从ifneq开始到下个endif为止所有行都删除的办法
虽然可以直接改TEST,但是留着那砣东西太碍眼了
Re: 求助:文本文件匹配删除
发表于 : 2012-02-08 22:23
由 josephyoung
代码: 全选
awk '/ ?ifneq ?\(/{while($0 !~ /endif/)getline;getline}1'
Re: 求助:文本文件匹配删除
发表于 : 2012-02-08 22:27
由 josephyoung
如果有嵌套的,就麻烦一点

有嵌套的也搞出来了
代码: 全选
awk '/ ?ifneq ?\(/{T=0;while($0 !~ /endif/ || T!=1){if($0 ~ /endif/)T--;if($0 ~ / ?ifneq ?\(/)T++;getline;}getline}1'
Re: 求助:文本文件匹配删除
发表于 : 2012-02-08 22:28
由 nyfair
嗯,没有嵌套的
非常感谢
Re: 求助:文本文件匹配删除
发表于 : 2012-02-09 5:57
由 fnan
sed '/^ifneq (/,/^endif$/d'
#加上 -i 就直接改文本。
Re: 求助:文本文件匹配删除
发表于 : 2012-02-12 19:01
由 tusooa
这貌似不能处理嵌套的吧。
Re: 求助:文本文件匹配删除
发表于 : 2012-02-12 19:03
由 eexpress
这要求简单,sed awk基本够了。
Re: 求助:文本文件匹配删除
发表于 : 2012-02-12 19:09
由 josephyoung
tusooa 写了:这貌似不能处理嵌套的吧。
我3楼的那个代码可以处理嵌套的
Re: 求助:文本文件匹配删除
发表于 : 2012-02-12 20:44
由 fnan
josephyoung 写了:tusooa 写了:这貌似不能处理嵌套的吧。
我3楼的那个代码可以处理嵌套的
kose-1@kose-1-desktop:~$ cat file
ifneq ($(TEST) 1)
...
...
...
endif
kose-1@kose-1-desktop:~$ awk '/ ?ifneq ?\(/{while($0 !~ /endif/)getline;getline}1' file
endif
#先改改bug。
Re: 求助:文本文件匹配删除
发表于 : 2012-02-13 8:39
由 fnan
kose-1@kose-1-desktop:~$ cat file
..."1*ifneq"...
ifneq ($(TEST) 1)
..."ifneq"...
ifneq ($(TEST) 1)
...
..."endif"...
endif
..."endif"...
endif
..."2*endif"...
..."3*ifneq"...
ifneq ($(TEST) 1)
..."ifneq"...
ifneq ($(TEST) 1)
...
..."endif"...
ifneq ($(TEST) 1)
...
..."endif"...
endif
ifneq ($(TEST) 1)
...
..."endif"...
endif
endif
..."endif"...
endif
..."4*endif"...
kose-1@kose-1-desktop:~$ sed -nr '1h;2,$H;${x;s/(^|\n)ifneq \([^\n]*/\x1a/g;s/\nendif(\n|$)/\x1b\1/g;s/(\x1a[^\x1b]+)+(\x1b[^\x1a]+)*\x1b//g;p}' file
..."1*ifneq"...
..."2*endif"...
..."3*ifneq"...
..."4*endif"...
#用sed玩嵌套(不考虑tab)
Re: 求助:文本文件匹配删除
发表于 : 2012-02-13 20:20
由 josephyoung
fnan 写了:josephyoung 写了:tusooa 写了:这貌似不能处理嵌套的吧。
我3楼的那个代码可以处理嵌套的
kose-1@kose-1-desktop:~$ cat file
ifneq ($(TEST) 1)
...
...
...
endif
kose-1@kose-1-desktop:~$ awk '/ ?ifneq ?\(/{while($0 !~ /endif/)getline;getline}1' file
endif
#先改改bug。
谢谢指正
代码: 全选
awk '/ ?ifneq ?\(/{T=0;while($0 !~ /endif/ || T!=1){if($0 ~ /endif/)T--;if($0 ~ / ?ifneq ?\(/)T++;getline;}next}1' file
将最后的getline改为next应该没问题了
Re: 求助:文本文件匹配删除
发表于 : 2012-02-13 21:11
由 josephyoung
fnan 写了:sed -nr '1h;2,$H;${x;s/(^|\n)ifneq \([^\n]*/\x1a/g;s/\nendif(\n|$)/\x1b\1/g;s/(\x1a[^\x1b]+)+(\x1b[^\x1a]+)*\x1b//g;p}' file
#用sed玩嵌套(不考虑tab)
帅呆了

特别是这个正则/(\x1a[^\x1b]+)+(\x1b[^\x1a]+)*\x1b/,学习了
Re: 求助:文本文件匹配删除
发表于 : 2012-02-13 22:51
由 fanhe
不喜欢shell了,脚本现在都直接写 python
Re: 求助:文本文件匹配删除
发表于 : 2012-02-13 23:50
由 josephyoung
fanhe 写了:不喜欢shell了,脚本现在都直接写 python

这个问题,带嵌套的,能写个python玩玩么?我也在学python中
Re: 求助:文本文件匹配删除
发表于 : 2012-02-15 21:22
由 fnan
josephyoung 写了:fnan 写了:sed -nr '1h;2,$H;${x;s/(^|\n)ifneq \([^\n]*/\x1a/g;s/\nendif(\n|$)/\x1b\1/g;s/(\x1a[^\x1b]+)+(\x1b[^\x1a]+)*\x1b//g;p}' file
#用sed玩嵌套(不考虑tab)
帅呆了

特别是这个正则/(\x1a[^\x1b]+)+(\x1b[^\x1a]+)*\x1b/,学习了
#嵌套再加复杂一点就显出bug了,还是老老实实用循环可靠,效率低一些。
kose3@kose3-desktop:~$ cat file2
..."1*ifneq"...
ifneq ($(TEST) 1)
..."ifneq"...
ifneq ($(TEST) 1)
...
..."endif"...
endif
..."endif"...
endif
..."2*endif"...
..."3*ifneq"...
ifneq ($(TEST) 1)
..."ifneq"...
ifneq ($(TEST) 1)
...
..."endif"...
ifneq ($(TEST) 1)
...
..."endif"...
endif
..."xxx"...
ifneq ($(TEST) 1)
...
..."endif"...
endif
endif
..."endif"...
endif
..."4*endif"...
kose3@kose3-desktop:~$ sed -nr '1h;2,$H;${x;s/(^|\n)ifneq \([^\n]*/\x1a/g;s/\nendif/\x1b/g; :A;s/\x1a[^\x1b\x1a]+\x1b//g;tA;p}' file2
..."1*ifneq"...
..."2*endif"...
..."3*ifneq"...
..."4*endif"...