简体   繁体   English

如何在匹配/匹配后使用“sed”删除2行?

[英]How can I use “sed” to delete 2 lines after match/matches?

I have the following command : sed -i -e '/match1/,+2d' filex , which deletes 2 lines after finding the match "match1" in the file "file x". 我有以下命令: sed -i -e '/match1/,+2d' filex ,它在文件“file x”中找到匹配“match1”后删除2行。 I want to add several matches to it, like match1, match 2 .... 我想为它添加几个匹配,比如match1,match 2 ....

So it will delete 2 lines after finding any of the matches, how can I achieve this ? 因此在找到任何匹配后会删除2行,我该如何实现?

Two ways, depending upon the sed version and platform: 两种方式,取决于sed版本和平台:

sed -e '/match1/,+2d' -e '/match2/,+2d' < oldfile > newfile

or 要么

sed -e '/match1\|match2/,+2d' < oldfile > newfile

Not a sed user but it seems to me you could use : 不是sed用户,但在我看来你可以使用:

sed -i -e '/(match1|match2)/,+2d' filex

Otherwise you could, if that's possible for you to do, do : 否则你可以,如果你有可能这样做,那么:

sed -i -e '/match1/,+2d' filex && sed -i -e '/match2/,+2d' filex

EDIT: Looks like I had the right idea but ziu got it. 编辑:看起来我有正确的想法,但ziu得到了它。

If I understand you correctly, you want 如果我理解正确,你想要

sed -e '/match1/,+2d' input.txt

For example, create input with seq 10 | sed '3i match1' > input.txt 例如,使用seq 10 | sed '3i match1' > input.txt创建输入 seq 10 | sed '3i match1' > input.txt : seq 10 | sed '3i match1' > input.txt

1
2
match1
3
4
5
6
7
8
9
10

The output of sed -e '/match1/,+2d' input.txt would be: sed -e '/match1/,+2d' input.txt的输出将是:

1
2
5
6
7
8
9
10

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

相关问题 您可以使用sed删除匹配项,之后仅删除某些行吗? - Can you use sed to delete match and only certain lines after? 如何使用Sed删除之前的5行和模式匹配之后的6行? - How to delete 5 lines before and 6 lines after pattern match using Sed? 如何使用SED在文件的两个连续行中搜索两个不同的模式,并在模式匹配后打印下4行? - How can I search for two different patterns in two consecutive lines in a file using SED and print next 4 lines after pattern match? 如何在与 Sed 匹配后替换连续的 2 行 - How to replace 2 consecutive lines after Match with Sed 如何使用sed删除与模式匹配的行及其后的行? - How to delete the line that matches a pattern and the line after it with sed? 如何使用 grep 进行匹配但不打印匹配项? - How can I use grep to match but without printing the matches? 我需要使用 sed 查找匹配项并删除该匹配项之前的 2 行和之后的 3 行 - I need to find a match using sed and deletes 2 lines before this match and 3 lines after it 使用sed,如何删除一系列行中的所有字符串实例? - Using sed, how can I delete all instances of a string within a range of lines? 我如何排除 sed 的特定线路? - How can i except specific lines with sed? 如何使用sed命令删除没有备份文件的行? - How to use sed command to delete lines without backup file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM