我有一个包含 *.txt
文件的文件夹。我想定期检查这些文件是否有重复的 URL。
实际上,我在这些文件中保存了我的书签,总是至少有两行,例如:
www.domain.com
Quite a popular domain name
碰巧,我用另一个描述保存了相同的 URL,例如:
www.domain.com
I should buy this domain
Whenever I happen to have enough money for this
所有条目都由单个空行分隔。有时 URL 是 Markdown 格式:
[domain.com](www.domain.com)
我将如何抓取文件夹中的重复 URL?
到目前为止我找到的唯一解决方案是 cat
结合它的 uniq
管道:
cat folder/* |sort|uniq|less > dupefree.txt
问题是:
- 这只会检查完全相同的行 - markdown URL 会被忽略并且关联的评论会丢失
- 我不想输出干净的文本文件,但只需要提示哪些 URL 是重复的
如何进行正确的重复检查?
最佳答案
这是我根据你的描述制作的源文件
cat file
www.domain.com
Quite a popular domain name
www.domain.com
I should buy this domain
Whenever I happen to have enough money for this
All entries are separated by single blank lines. And sometimes the URLs are in markdown format:
[domain.com](www.domain.com)
How would I crawl the folder for duplicate URLs?
使用awk导出重复域名:
awk 'BEGIN{FS="\n";RS=""}
{ if ($1~/\[/) { split($1,a,"[)(]"); domain[a[2]]++}
else {domain[$1]++}
}
END{ for (i in domain)
if (domain[i]>1) print "Duplicate domain found: ",i
}' file
Duplicate domain found: www.domain.com
关于linux - 如何检查文本文件的文件夹中是否有重复的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22986231/