按特定行范围内的模式删除行 [英] Delete lines by pattern in specific range of lines

查看:37
本文介绍了按特定行范围内的模式删除行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 sed 通过正则表达式模式从文件中删除行,就像在这个问题 Delete lines in包含特定字符串的文本文件,但仅在行范围内(而不是在整个文件中).我想从某个行号开始直到文件末尾.

I want to remove lines from file by regex pattern using sed just like in this question Delete lines in a text file that containing a specific string, but only inside a range of lines (not in the whole file). I want to do it starting from some line number till the end of file.

这是我结合 tail 完成的:

This is how I've done it in combination with tail:

  • tail -n +731 file|sed '/some_pattern/d' >>文件
  • 从上一步手动删除file中编辑过的范围
  • tail -n +731 file|sed '/some_pattern/d' >> file
  • manually remove edited range in file from previous step

仅使用 sed 是否有更短的方法?
类似于 sed -i '731,1000/some_pattern/d' 文件?

Is there a shorter way to do it with sed only?
Something like sed -i '731,1000/some_pattern/d' file?

推荐答案

你可以使用这个sed,

sed -i.bak '731,1000{/some_pattern/d}' yourfile

测试:

$ cat a
1
2
3
13
23
4
5

$ sed '2,4{/3/d}' a
1
2
23
4
5 

这篇关于按特定行范围内的模式删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆