相对于包含模式的行,删除前面的 n1 行和后面的 n2 行
sed -e '/XXXX/,+4d' fv.out
我必须在文件中找到特定模式并同时删除其上方 5 行和下方 4 行。我发现上面的行删除了包含图案的行及其下面的四行。
sed -e '/XXXX/,~5d' fv.out
在 sed 手册中,给出 ~ 代表模式后面的行。但当我尝试时,删除的是模式后面的行。
那么,如何同时删除包含该模式的行上方 5 行和下方 4 行呢?
sed -e '/XXXX/,+4d' fv.out
I have to find a particular pattern in a file and delete 5 lines above and 4 lines below it simultaneously. I found out that the line above removes the line containing the pattern and four lines below it.
sed -e '/XXXX/,~5d' fv.out
In sed manual it was given that ~ represents the lines which is followed by the pattern. But when i tried it, it was the lines following the pattern that was deleted.
So, how do I delete 5 lines above and 4 lines below a line containing the pattern simultaneously?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用
sed
的一种方法,假设模式彼此不够接近:script.sed
的内容:运行如下:
One way using
sed
, assuming that the patterns are not close enough each other:Content of
script.sed
:Run like:
使用awk的解决方案:
更新:
这是解释的脚本:
lines
中的最后5行使用旋转索引(NR% 5;NR 是记录编号;在本例中为行)。$0 ~ "XXXX
;$0
是当前记录:在本例中是一行;并且~
作为扩展正则表达式匹配运算符),我重置了读取的行数,并注意到我有 5 行要删除(包括当前行),我的脚本原始版本如下,但我最终将其优化为上述版本:
< code>awk 是一个很棒的工具我强烈建议您在网上找到一个教程并阅读它:
awk
可与扩展正则表达式 (ERE) 配合使用。 。它们的语法与sed
中使用的标准正则表达式 (RE) 略有不同,但是使用 RE 可以完成的所有操作都可以使用埃雷。A solution using
awk
:Update:
This is the script explained:
lines
using rotatory indexes (NR%5; NR is the record number; in this case lines).$0 ~ "XXXX
;$0
being the current record: in this case a line; and~
being the Extended Regular Expression match operator), I reset the number of lines read and note that I have 5 lines to delete (including the current line).My original version of the script was the following, but I ended up optimizing it to the above version:
awk
is a great tool ! I strongly recommend that you find a tutorial on the net and read it. One important thing:awk
works with Extended Regular Expressions (ERE). Their syntax is a little different from Standard Regular Expression (RE) used insed
, but all that can be done with RE can be done with ERE.这个想法是读取 5 行而不打印它们。如果找到该图案,请删除未打印的行和下面的 4 行。如果没有找到该模式,请记住当前行并打印第一个未打印的行。最后,打印未打印的内容。
当然,这仅当您的模式在文件中出现一次时才有效。如果你有很多,你需要在找到你的模式后阅读 5 行新的行,如果你在这些行中再次有你的模式,事情就会变得复杂。在这种情况下,我认为 sed 不是正确的工具。
The idea is to read 5 lines without printing them. If you find the pattern, delete the unprinted lines and the 4 lines bellow. If you do not find the pattern, remember the current line and print the 1st unprinted line. At the end, print what is unprinted.
Of course, this only works if you have one occurrence of your pattern in the file. If you have many, you need to read 5 new lines after finding your pattern, and it gets complicated if you again have your pattern in those lines. In this case, I think sed is not the right tool.
这可能对你有用:
或者这个:
一个更有效的 sed 解决方案:
This might work for you:
or this:
a more efficient sed solution:
如果您愿意将结果输出到文件而不是 stdout,vim 可以非常有效地完成它:
或者
就地编辑文件。
If you are happy to output the result to a file instead of stdout,
vim
can do it quite efficiently:or
to edit the file in-place.