删除与模式匹配的第一行 [英] Delete the first line that matches a pattern

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

问题描述

如何使用 sed 只删除包含特定模式的第一行?

How can I use sed to delete only the first line that contains a certain pattern?

例如,我想从该文档中删除与FAA匹配的第一行:

For example, I want to remove the first line matching FAA from this document:

1. foo bar quuz
2. foo FAA bar (this should go)
3. quuz quuz FAA (this should remain)
4. quuz FAA bar (this should also remain)

结果应该是

1. foo bar quuz
3. quuz quuz FAA (this should remain)
4. quuz FAA bar (this should also remain)

POSIX sed 的解决方案将不胜感激,GNU sed 是可以的.

A solution for POSIX sed would be greatly appreciated, GNU sed is OK.

推荐答案

使用 GNU sed,您可以使用以下习惯用法:

With GNU sed you can use the following idiom:

sed '0,/FAA/{/FAA/d}' input.txt

它将删除操作应用于从文件开头到第一次出现 FAA 的范围,并仅删除该行.

It applies the delete action to a range from the beginning of the file to the first occurrence of FAA and removes only that line.

这篇关于删除与模式匹配的第一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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