文本文件的格式如下:
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行左右。我需要保持“部门”的关联...“行,“标题...”行和“我需要保留的文本行”相互关联。因此,我希望首先将文本文档分解成我需要的东西,然后再对其进行进一步的操作(按字符)。所以我写了这个:
g!/\Section\s\d*\sArea\s\h\sUnit\s\d*\n\|^\s\{3}\zs\d*\s-\_.*\ze2010-11/d删除后,我得到"Section..“行和“标题...”行,但决不是"Title..“下面的后续行。线路。随后的代码行从4行到8行不等,但"2010-11“行是一致的,而且总是我不想要的。
您可以看到,我尝试使用zs和ze来选择我不想删除的内容。我认为选择是有效的,因为如果我将命令更改为"2011-12“,则没有匹配,并且命令的(OR)一半不返回结果。
我认为故障可能是光标位置(?),但我不确定,我的努力已经失败了。
有人能看到我的错误吗?
谢谢!
发布于 2011-12-16 19:12:11
试一试吧。
:silent! g/^Section/+ , /^\s\+\d\+ -/- d
:g/^\s\+2010/ , -/\nSection\|\%$/ d发布于 2011-12-16 04:30:44
:g找到与模式开头匹配的每一行,!将恢复选择,并将命令应用于这些行。
像g/^Section.../normal! j2dd3jd}这样的东西会这样做吗?
如果没有,你可以在normal中搜索标题行!您可能需要将其包含在"exec“中,但编写函数可能要简单得多。
你真的需要使用vim吗?在我看来,这是Perl的工作。
发布于 2011-12-16 11:37:25
我相信有很多方法可以做到这一点。我认为这个命令序列应该有效(忽略以双引号开头的注释行):
" 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 https://stackoverflow.com/questions/8523754
复制相似问题