如何从grep保存匹配和不匹配 [英] How to save both matching and non-matching from grep

查看:122
本文介绍了如何从grep保存匹配和不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常使用grep,并且熟悉它返回匹配行(默认情况下)和不匹配行(使用-v参数)的功能.但是,我希望能够一次grep一个文件以分隔匹配和不匹配的行.

I use grep very often and am familiar with it's ability to return matching lines (by default) and non-matching lines (using the -v parameter). However, I want to be able to grep a file once to separate matching and non-matching lines.

如果这不可能,请告诉我.我意识到我可以在perl或awk中轻松地做到这一点,但是很好奇grep是否可以实现.

If this is not possible, please let me know. I realize I could do this easily in perl or awk, but am curious if it is possible with grep.

谢谢!

推荐答案

如果不是必须使用grep-这是基于模式的一次遍历拆分-找到模式>未找到file1模式> file2

If it does NOT have to be grep - this is a single pass split based on a pattern -- pattern found > file1 pattern not found > file2

awk '/pattern/ {print $0 > "file1"; next}{print $0 > "file2"}' inputfile

这篇关于如何从grep保存匹配和不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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