删除所有行,在/ pattern /之后开始两行(Delete all lines, starting two lines after /pattern/)

编程入门 行业动态 更新时间:2024-10-22 10:52:38
删除所有行,在/ pattern /之后开始两行(Delete all lines, starting two lines after /pattern/)

想象一下,我有一个文件如下文件:

drink eat XXX pizza blunzn sushi

我想删除文件中的所有行,从模式XXX之后的第三行开始,因此结果应如下所示:

drink eat XXX pizza blunzn

删除XXX后的所有行很简单:

sed -e '/XXX/q' -i data.txt

但是,我发现在删除模式后很难跳过固定数量的行。

到目前为止我想出的最好的是:

sed -e '/XXX/ { N; N; q }' -i data.txt

有没有比添加n * N更优雅的东西(想象一下,我想跳过50行)?

Imagine I have a file the following file:

drink eat XXX pizza blunzn sushi

I would like to remove all lines from the file, starting with the third line after the pattern XXX, so the result should look like:

drink eat XXX pizza blunzn

Removing all lines after XXX is simple enough:

sed -e '/XXX/q' -i data.txt

However, I find it hard to skip a fixed number of lines after the pattern from deletion.

The best I came up with so far is:

sed -e '/XXX/ { N; N; q }' -i data.txt

Is there something more elegant, than adding n * N (imagine, I would like to skip 50 lines)??

最满意答案

这可能适合你(GNU sed):

sed '/pattern/{:a;N;s/\n/&/2;Ta;q}' file

在遇到所需的模式时,循环所需的行然后退出。

对于遵循所需模式的50行,使用:

sed '/pattern/{:a;N;s/\n/&/50;Ta;q}' file

This might work for you (GNU sed):

sed '/pattern/{:a;N;s/\n/&/2;Ta;q}' file

On encountering the required pattern, loop the required lines then quit.

For fifty lines following the required pattern, use:

sed '/pattern/{:a;N;s/\n/&/50;Ta;q}' file

更多推荐

本文发布于:2023-04-29 12:12:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1336317.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:两行   pattern   Delete   starting   lines

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!