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

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

问题描述

当同一行存在于另一个文件中时,如何删除该文件中的行有很多示例.我已经通读了它们,如果整行匹配,它们都会删除.例如:grep -vxF -f file1 file2

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

我所拥有的略有不同.我有一个来自我的网站和我的客户网站的 URL 列表.当域与另一个文件中的域匹配时,我想从该文件中删除行.

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

第二个文件可能是:

site2.com
www.site4.com

我希望输出为:

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

推荐答案

grep 标志太多.具体来说:-x 将使您无法获得所需的结果.

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

假设 file1 具有模式,而 file2 具有 URL,只需使用:

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

grep -v -f file1 file2

-x 标志会阻止你得到你想要的结果:使用 -x 意味着:只匹配整行,即只匹配一行,如果这条线正是,例如site2.com.

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.

来自man grep:

-x, --line-regexp

-x, --line-regexp

仅选择与整行完全匹配的匹配项.

Select only those matches that exactly match the whole line.

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

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