简体   繁体   English

使用 sed 删除一系列行上的模式

[英]use sed to delete patterns on a range of lines

I have a large text file that I would like to divide into segments and use sed to delete certain patterns in place.我有一个大文本文件,我想将其分成多个段并使用 sed 删除某些模式。 I would like to do this in a single command line using a pipe.我想使用管道在单个命令行中执行此操作。 For example:例如:

sed -n 1,10p <text file> | sed -i 's/<pattern to remove>//'

The code above attempts to take the first 10 lines of the text file and remove the patterns from the 10 lines in place.上面的代码尝试获取文本文件的前 10 行,并从原位的 10 行中删除模式。 The resulting text file should have the first 10 lines modified.生成的文本文件应修改前 10 行。 The code above doesn't work because the second command after the pipe requires a input file.上面的代码不起作用,因为管道后的第二个命令需要输入文件。 Please help!请帮忙!

Something like就像是

sed -i '1,10s/pattern//' foo.txt

though for in place editing of a file I prefer ed or perl instead of relying on a non standard sed extension like -i .虽然对于文件的就地编辑,我更喜欢edperl而不是依赖像-i这样的非标准sed扩展名。

这似乎可以满足您的要求......

sed -ni '1,10s/pattern//p' file

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM