从一个文件中删除另一个文件中的行 [英] Deleting lines from one file which are in another file

查看:25
本文介绍了从一个文件中删除另一个文件中的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件 f1:

line1
line2
line3
line4
..
..

我想删除另一个文件 f2 中的所有行:

I want to delete all the lines which are in another file f2:

line2
line8
..
..

我用 catsed 尝试了一些东西,这甚至不是我想要的.我该怎么做?

I tried something with cat and sed, which wasn't even close to what I intended. How can I do this?

推荐答案

grep -v -x -f f2 f1 应该可以解决问题.

说明:

  • -v 选择不匹配的行
  • -x 只匹配整行
  • -f f2f2
  • 获取模式
  • -v to select non-matching lines
  • -x to match whole lines only
  • -f f2 to get patterns from f2

可以改为使用 grep -Ffgrep 来匹配来自 f2 而不是 固定字符串>patterns(如果您想以所见即所得"的方式删除行,而不是将 f2 中的行视为正则表达式模式).

One can instead use grep -F or fgrep to match fixed strings from f2 rather than patterns (in case you want remove the lines in a "what you see if what you get" manner rather than treating the lines in f2 as regex patterns).

这篇关于从一个文件中删除另一个文件中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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