regex - 在 Vi 编辑器中替换多行

标签 regex linux unix vi

这是文件中的几行文本。我需要使用 vi 编辑器删除某些文本 block 。

极光(复数:auroras 或 aurorae)是天空中的自然光显示,特别是在高纬度(北极和南极)地区,由高能带电粒子与高海拔大气(热层)中的原子碰撞引起.

Most aurorae occur in a band known as the auroral zone[2][2] which is typically 3° to 6° in latitudinal extent and at all local times or longitudes.

The auroral zone is typically 10° to 20° from the magnetic pole defined by the axis of the Earth's magnetic dipole. During a geomagnetic storm, the auroral zone will expand to lower latitudes. The diffuse aurora is a featureless glow in the sky which may not be visible to the naked eye even on a dark night and defines the extent of the auroral zone.

我有一个像上面这样的输入文件。在此文件中,我必须删除某些出现的文本 block ,如下所示。

Most aurorae occur in a band known as the auroral zone[2][2] which is typically 3° to 6° in latitudinal extent and at all local times or longitudes.

所以,我正在使用以下不起作用的命令:

:g/^Most/,/auroral/,/longitudes./d

我正在删除以 Most 开头的行,中间是 auroral,末尾是 longitudes。

最佳答案

你可以做什么有明显的限制,但在上下文中,你可以使用:

:g/^while/.,/^}/d

删除 while 循环,其中 while 位于一行的开头,直到一行开头的右大括号。


Can you please add some more to this?

:g/^while/ 部分全局搜索以 while 开头的行。接下来是为每个匹配行执行的 ex 命令。命令为.,/^}/d,表示从当前行(.)开始到下一行以右大括号(/^}/) 执行删除 (d)。您也可以使用向后搜索或相对运动(?^{?.-3.+10)之类的东西。

很难从评论的困惑外观中准确判断出您的想法(这不是您的错 - 评论不会保留有用的格式。)

--------------
Stack Over Flow
c**p
c**p
c**p
cool
c**p
c**p
c**p
------------

I need to replace from Stack Over Flow to ------------ [...]

这很简单,我看不出 cool 对它有什么影响:

:g/^Stack Over Flow/.,/^-------/d

这与我原来的答案同构。

关于regex - 在 Vi 编辑器中替换多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7857491/

相关文章:

regex - 正则表达式从字符串中提取前3个单词

linux - Bash,如何对每一行应用一个算术表达式?

c - 将数据与用户输入分开。

linux - 为什么 grep 给出搜索模式错误,即 grep : <some pattern>: No such file or directory

c++ - 是否有用于 C++ 的 LINQ 库?

javascript - 空格导致正则表达式捕获地址时出现问题

python : Split string every three words

java - 将数字提取到字符串数组中

linux - Bash 脚本日期 : Day before 15th and last day of month but it has to be a week day

linux - Linux 上的 inotifywait 是否允许在超时期限内收集事件?