在空行之后修剪文件
我有一个包含多个空行的文本文件,我试图返回其中两个空行之间的所有行,
所以如果我有一个如下所示的文件:
____________________________
1########################
2##########################
3
4########################
5##########################
6#######################
7
8#########################
9##########################
10#######################
11####################
12########################
13#########################
14
15##########################
----------------------------
我想抓取第 8-13 行。不幸的是,它可能并不总是 8-13,因为它可能是 9-20 或 7-8,但它总是在第二个和第三个断线之间。
我知道如何修剪字符并拉出奇异的线条,但我不知道如何修剪整个部分。
任何帮助将不胜感激,即使您只是向我指出一个教程。
提前致谢。
I have a text file that has multiple blank lines and Im trying to return all the lines between two of them specifically
so if I have a file that looks like this:
____________________________
1########################
2##########################
3
4########################
5##########################
6#######################
7
8#########################
9##########################
10#######################
11####################
12########################
13#########################
14
15##########################
----------------------------
I would like to grab lines 8-13. Unfortunately, it might not always be 8-13 as it could be 9-20 or 7-8, but it will however always be between the 2nd and 3rd line break.
I know how to trim characters and pull out singular lines, but I have no idea how to trim entire sections.
Any help would be appreciated, even if you just point me to a tutorial.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里的基本思想是将整个内容作为一个字符串,在双换行符处将其分成几组,然后引用您想要的组(在您的情况下,是第三个组)。
编辑:
回答您评论中的问题 -
Environment.NewLine 是指定换行符的更动态的方式。假设您在 Windows 上运行 - 您也可以使用 VbCrLf。这个想法是,如果您要在 Linux 上编译相同的代码,Environment.NewLine 将生成一个 Lf。您可以在此处查看更多信息:http://en.wikipedia.org/wiki/Newline
我使用Environment.NewLine & 的原因Environment.NewLine 是因为您想要在有两个换行符的地方中断信息(一个在段落最后一行的末尾,一个在下一段之前的空行)
The basic idea here is to get the entire thing as a string, split it into groups at the double line breaks, and then reference the group you want (in your case, the third one).
Edit:
In response to the question in your comment -
Environment.NewLine is a more dynamic way of specifying a line break. Assuming you're running on windows - you could use VbCrLf as well. The idea is that if you were to compile the same code on Linux, it Environment.NewLine would generate a Lf instead. You can see here for more information: http://en.wikipedia.org/wiki/Newline
The reason I used
Environment.NewLine & Environment.NewLine
is because you want to break your information where there are two line breaks (one at the end of the last line of a paragraph, and one for the blank line before the next paragraph)我最终做的是修剪最后一部分并搜索第一部分中我需要的内容(我知道我没有在问题中包含搜索部分,但我只是想找出一种方法来缩小搜索结果范围它会产生重复的结果)。我发布此内容是为了防止其他人偶然发现此内容并寻求一些答案。
其工作原理是,它会遍历正则表达式的每一行,直到到达断点“Total PSS by OOM adjustment:”中的第一个单词。
What I ended up doing was trimming the last part and searching for what I needed in the first part (I know I didnt include the searching part in the question, but I was just trying to figure out a way to narrow down the search results as it would have had repeated results). Im posting this incase anyone else stumbles upon this looking for some answers.
How this works is that it looks through each line for the regex until it reaches first word in the breakpoint "Total PSS by OOM adjustment:"