猛砸,Linux的,需要根据从另一个文件匹配的内容从一个文件中删除行 [英] Bash, Linux, Need to remove lines from one file based on matching content from another file

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

问题描述

有关于如何当另一个文件存在相同的行删除一个文件行的例子很多。我已经通过他们读,他们都删除,如果全系列匹配。类似的例子:的grep -vxF -f文件1文件2

我有略有不同。我从我的网站和我的客户网站的URL列表。我希望当域在另一个文件中的域相匹配,以消除从该文件中的行。

因此​​,第一个文件可能如下:

  http://www.site1.com/some/path
http://www.site2.com/some/path
http://www.site3.com/some/path
http://www.site4.com/some/path

第二个文件可以是:

  site2.com
www.site4.com

我想输出是:

  http://www.site1.com/some/path
http://www.site3.com/some/path


解决方案

您有太多的的grep 标志。具体做法是: -x 将让你从得到你想要的结果。

假设的文件1 的有图案,和文件2 的有网址,只需使用:

 的grep -v -f文件1文件2

-x 标志将让你找不到你想要的结果:使用 -x 表示:比赛只对整条生产线,即只如果行是完全匹配的线路,例如:的 site2.com

的grep人


  

-x,--line,正则表达式


  
  

选择只有那些比赛,整个线路完全匹配。


There are many examples on how to remove lines in one file when that same line exists in another file. I have read through them and they all remove if the full line matches. Examples like: grep -vxF -f file1 file2

What I have is slightly different. I have a list of URLs from my websites and my clients websites. I want to remove lines from that file when the domain matches a domain in another file.

So the first file might look like:

http://www.site1.com/some/path
http://www.site2.com/some/path
http://www.site3.com/some/path
http://www.site4.com/some/path

The second file could be:

site2.com
www.site4.com

I would like the output to be:

http://www.site1.com/some/path
http://www.site3.com/some/path

解决方案

You have too many grep flags. Specifically: -x will keep you from getting your desired results.

Assuming that file1 has the patterns, and file2 has the URLs, just use:

grep -v -f file1 file2

The -x flag will keep you from getting the results that you want: using -x means: match only against the entire line, i.e. only match a line if the line is exactly, e.g. site2.com.

From the man grep:

-x, --line-regexp

Select only those matches that exactly match the whole line.

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

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