文本文件的格式如下:
Section 4 Area B Unit 20
stuff i don't need...
stuff i don't need...
45990 - Title of Project that I want to save
line of text I need to keep
line of text I need to keep
2010-11 this line (starting with 2010) is not needed
stuff i don't need
Section 589 Area C Unit 1005
stuff i don't need...
stuff i don't need...
45990 - Title of Project that I want to save
line of text I need to keep
line of text I need to keep
2010-11 this line (starting with 2010) is not needed
stuff i don't need
这些部分重复了数百个。 “我不需要的东西”线实际上大约是30左右。我需要保持“Section ...”行,“Title ...”行和“我需要保留的文本行”之间的关联。所以我希望首先将文本文档向下(行方向)破坏我需要的东西,然后再进行操作(字符方式)。所以我写了这个:
g!/\Section\s\d*\sArea\s\h\sUnit\s\d*\n\|^\s\{3}\zs\d*\s-\_.*\ze2010-11/d
删除后,我得到“Section ..”行和“Title ...”行,但从来没有“Title ..”行下面的后续行。随后的线路从4到8行不等,但“2010-11”线是一致的,总是我不再想要的。
你可以看到我尝试使用zs和ze来选择我不想删除的内容。我认为选择是有效的,因为如果我将命令更改为“2011-12”则没有匹配,并且命令的(OR)一半不返回结果。
我认为错误可能是光标位置(?),但我不确定,我修复它的努力失败了。
任何人都可以看到我的错误吗?
谢谢!
答案 0 :(得分:1)
:g
找到匹配模式开头的每一行,!
将恢复选择,命令将应用于这些行。
像g/^Section.../normal! j2dd3jd}
那样吗?
如果没有,你可以在正常情况下使用搜索标题行! 您可能需要将其括在“exec”中,但编写函数可能要简单得多。
你真的需要使用vim吗?对我来说Perl的工作似乎很像。
答案 1 :(得分:1)
有很多方法可以做,我确定。我认为这个命令序列应该有效(忽略以双引号开头的注释行):
" global delete of line below 'Section' to line before 'Title'
g/^\s*Section/+1;/Title/-1delete
" global delete from date line to line before 'Section'
g/^\s*\d\d\d\d-\d\d/;/^\s*Section/-1delete
" go to top line of buffer
gg
" delete last chunk, from final date to last line
/^\s*\d\d\d\d-\d\d/;$delete
答案 2 :(得分:1)
给它一个旋转。
:silent! g/^Section/+ , /^\s\+\d\+ -/- d
:g/^\s\+2010/ , -/\nSection\|\%$/ d