如何使 sed 删除与替换不匹配的行
我基本上想这样做:
cat file | grep '<expression>' | sed 's/<expression>/<replacement>/g'
而不必将表达式写两次:
cat file | sed 's/<expression>/<replacement>/g'
有没有办法告诉 sed 不要打印与替换命令中的正则表达式不匹配的行?
I basically want to do this:
cat file | grep '<expression>' | sed 's/<expression>/<replacement>/g'
without having to write the expression twice:
cat file | sed 's/<expression>/<replacement>/g'
Is there a way to tell sed not to print lines that does not match the regular expression in the substitute command?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
假设您有一个文件,其中包含要替换的文本。
如果您想将 A 更改为 a,那么理想情况下我们会执行以下操作 -
但如果您不希望获得不受替换影响的行,那么您可以使用 n 和 p 的组合,如下所示 -
Say you have a file which contains text you want to substitute.
If you want to change A to a then ideally we do the following -
But if you don't wish to get lines that are not affected with the substitution then you can use the combination of n and p like follows -
这可能对你有用:
或者
This might work for you:
Or
怎么样:
将从输入中删除匹配的模式。当然,这与你想要的相反,但也许你可以制作一个相反的正则表达式?
请注意,我并不完全确定语法,前段时间只使用过几次。
How about:
Will delete matching patterns from the input. Of course, this is opposite of what you want, but maybe you can make an opposite regular expression?
Please not that I'm not completely sure of the syntax, only used it a couple of times some time ago.