sed-删除不包含模式的行 [英] sed- delete line that doesn't contain a pattern

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

问题描述

我在SO上找不到与该问题类似的问题,感到很惊讶.

I'm surprised that I can't find a question similar to this one on SO.

如何使用sed删除不包含特定模式的所有行.

How do I use sed to delete all lines that do not contain a specific pattern.

例如,我有这个文件:

cat kitty dog
giraffe panda
lion tiger

我想要一个sed命令,该命令在被调用时将删除所有不包含单词 cat 的行:

I want a sed command that, when called, will delete all lines that do not contain the word cat:

cat kitty dog

推荐答案

这可以做到:

sed -i '/cat/!d' file1.txt

要强制完全匹配:

sed -i '/\<cat\>/!d' file1.txt

sed -i '/\bcat\b/!d' file1.txt

其中 \< \> & \ b \ b 强制完全匹配.

where \<\> & \b\b force an exact match.

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

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