【发布时间】:2014-09-24 22:34:36
【问题描述】:
我有两个文件: 文件 1
http://www.hello.com http://neo.com/peace/development.html, www.japan.com, http://example.com/abc/abc.html
http://news.net http://lolz.com/country/list.html,www.telecom.net, www.highlands.net, www.software.com
http://example2.com http://earth.net, http://abc.gov.cn/department/1.html
文件 2:
www.neo.com/1/2/3/names.html
http://abc.gov.cn/script.aspx
http://example.com/abc/abc.html
file 2 是用于 file1 中 column2 的部分匹配的搜索 url。如果它有部分匹配,它必须返回第 1 列 url 和文件 1 的第 2 列中的部分匹配 url,如下所示:
期望的输出:
http://www.hello.com http://neo.com/peace/development.html, http://example.com/abc/abc.html
http://news.net
http://example2.com http://abc.gov.cn/department/1.html
我尝试了这个脚本,它可以在第 2 列为我提供完全匹配的 url 模式,如下所示:
awk -F '[ \t,]' '
FNR == NR {
a[$1]
next
}
{ o = $1
c = 0
for(i = 2; i <= NF; i++)
if($i in a)
o = o (c++ ? ", " : "\t") $i
print o
}' file2 file1
输出是:
http://www.hello.com http://example.com/abc/abc.html
http://news.net
http://example2.com
有什么建议可以解决这个问题吗?
【问题讨论】:
-
我认为
grep -f file2 file1应该适用于大多数情况,除了在没有匹配项时返回 ing column1 -
当
http://abc.gov.cn/department/1.html没有出现在file 2中时,为什么它会出现在您的输出中。我想我不明白你在这里的目的。你不是用file 2搜索file 1,如果找到一个匹配则返回整行,如果没有找到匹配则只返回第一列? -
@skamazin 如果与 File2 中的 URL 部分匹配,则应打印来自 File1 列 2 的 URL。
http://abc.gov.cn/department/1.html被打印,因为文件 2 中与http://abc.gov.cn/部分匹配。这就是我的解释方式。 -
好的,所以它只需要匹配 URL 的第一部分......这相当困难......