Shell grep 取差集、交集、并集

  • 使用常见的 grep 命令实现
  • 可以用 man grep 查看grep使用说明
Matcher Selection
       -F, --fixed-strings, --fixed-regexp
              Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched.  (-F is specified by POSIX, --fixed-regexp is an obsoleted alias, please do not use it in new scripts.)
Matching Control
       -f FILE, --file=FILE
              Obtain patterns from FILE, one per line.  The empty file contains zero patterns, and therefore matches nothing.  (-f is specified by POSIX.)
       -v, --invert-match
              Invert the sense of matching, to select non-matching lines.  (-v is specified by POSIX.)

a差b

grep -F -v -f b.file a.file
cat a.file | grep -F -v -f b.file 

a交b

grep -F -f a.file b.file
cat b.file | grep -F -f a.file 

a并b

cat a.file b.file | sort | uniq
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容