2014-11-24 60 views
0

我必须用一堆数据txt文件(在一种情况下,每行上有一个字符串,在其他文件中末尾有换行符结束只是简单的字符串,用空格但没有明确的方式来分解数据)。但文件组由超链接组成。这些数据当前不存在于表格中 - 我不确定将其导入适当的列/数据字段的最佳方式)。如何在两个txt文件中找到一个匹配的字符串

我需要做的就是拉出两个文件中出现的数据“字符串”。

例子文件1:

http://-28uw.c.cr http://www.2xik.com http://www.365cnblog.cn http://www.blogactif.net  http://www.blogactifs.com http://www.blogalbums.com http://www.blogallerys.com ... etc. 

示例文件2:

http://en.wikipedia.org 
http://stayyoungafter50.com 
http://108.160.146.227 
http://10tv.com 
http://110mb.com 

我不关心WWW,HTTP或HTTPS所有我想要做的就是找到这两个文件中匹配10tv.com 。或cnn.com,就是这样。

有什么建议吗?

+0

你的问题很模糊,所以很难给出任何价值的答案。 我建议的是用你选择的语言编写一个小程序,读取2个文件。 当它处理第一个文件时 - 将这些单词添加到字典对象或等价物。希望你可以使用像任何空白逻辑将被视为分隔符(新行,空格,制表符)。当你存储它时,你会修剪你不关心的东西,比如http://和https://等等。 然后你读第二个文件 - 检查(修剪的)单词是否在字典中。如果它们是,将它们插入到数据库中。 – bkr 2014-11-24 19:38:46

+0

作为附加说明 - 我不相信-28uw.c.cr是一个有效的主机名。主机名标签不应以连字符开头或结尾 - http://en.wikipedia.org/wiki/Hostname。 – bkr 2014-11-24 19:45:31

+0

@bkr感谢您的建议,我不负责这些主机名的有效性是我提供的文件。最终我使用sql /数据库,所以我想用sql的soe形式来比较数据。但我不确定什么是最好的。 – YelizavetaYR 2014-11-24 19:50:55

回答

0

以下是我想接近它(但使用PowerShell的,而不是SQL):

clear 
pushd c:\myPath\myFolder\ 

#read in the contents of the files 
$file1 = get-content("file1.txt") 
$file2 = get-content("file2.txt") 

#loop through each row of the whitespace separated file 
$file1 = $file1 | %{ 
    #for each line, split on whitespace characters, returning the results back in a single column 
    $_ -split "\s" | %{$_} 
} 
#compare the two files for matching data & output this info 
compare-object $file1 $file2 -IncludeEqual -ExcludeDifferent | ft -AutoSize 

popd 

注:忽略协议,只需从使用类似的技术我们对空间的分割字符串中删除;即正则表达式,这次用替换而不是拆分。

clear 
pushd c:\temp 

$file1 = get-content("file1.txt") 
$file2 = get-content("file2.txt") 

$file1 = $file1 | %{ 
    $_ -split "\s" | %{ 
     $_ -replace ".*://(.*)",'$1' 
    } 
} 

$file2 = $file2 | %{ 
    $_ -replace ".*://(.*)",'$1' 
} 

compare-object $file1 $file2 -IncludeEqual -ExcludeDifferent | ft -AutoSize 

然而,你应该更喜欢SQL解决方案,试试这个(MS SQL服务器):

create table f1(url nvarchar(1024)) 
create table f2(url nvarchar(1024)) 

BULK INSERT f1 
FROM 'C:\myPath\myFolder\file1.txt' 
WITH (ROWTERMINATOR =' ', FIRSTROW = 1) 

BULK INSERT f2 
FROM 'C:\myPath\myFolder\file2.txt' 
WITH (FIRSTROW = 1) 
go 

delete from f1 where coalesce(rtrim(url),'') = '' 
delete from f2 where coalesce(rtrim(url),'') = '' 

select x.url, x.x, y.y 
from 
(
    select SUBSTRING(url,patindex('%://%',url)+3, len(url)) x 
    , url 
    from f1 
) x 
inner join 
(
    select SUBSTRING(url,patindex('%://%',url)+3, len(url)) y 
    , url 
    from f2 
) y 
on y.y = x.x 
+1

这看起来不错。我会玩这个。谢谢。 – YelizavetaYR 2014-11-24 21:08:28

相关问题