删除除匹配模式行最佳实践之外的所有行 (sed) [英] Remove all lines except matching pattern line best practice (sed)

查看:54
本文介绍了删除除匹配模式行最佳实践之外的所有行 (sed)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想删除除包含匹配模式的行之外的所有行.

I want to remove all lines except the line(s) containing matching pattern.

我是这样做的:

sed -n 's/matchingpattern/matchingpattern/p' file.txt

但我只是好奇,因为我将匹配模式重命名为匹配模式本身.这里看起来很浪费.

But I'm just curious because I rename matching pattern to the matching pattern itself. Looks like a waste here.

有没有更好的方法来做到这一点?

Is there a better way to do this?

推荐答案

sed '/pattern/!d' file.txt

但是你在这里重新发明了grep.

But you're reinventing grep here.

这篇关于删除除匹配模式行最佳实践之外的所有行 (sed)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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