linux - 如何检查文本文件的文件夹中是否有重复的 URL

标签 linux macos shell duplicates uniq

我有一个包含 *.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

问题是:

  1. 这只会检查完全相同的行 - markdown URL 会被忽略并且关联的评论会丢失
  2. 我不想输出干净的文本文件,但只需要提示哪些 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/

相关文章:

c - 替代轮询功能以检查 FIFO 中的新数据

xcode - OSX 上的 gcc 在哪里?我已经安装了 Xcode

shell - 如何使用 time 命令但仅对脚本的一部分进行计时

mysql - 从 shell 查询 MySQL 数据库

linux - FD_ISSET 在 FD_SET 之后返回 0

linux - Linux 上缓存和缓冲内存之间的区别

java - MacBook Air 的 Java/J2EE/RoR 开发工作?

macos - 在 Mac 中,如何在脚本 (sh/bash/applescript) 中确定当前是否通过 Apple Remote Desktop 运行?

linux - 如何在 mac OS 中将主机 ip 地址传递给 docker?

linux:如何在 LSF 作业系统中重定向一行 bsub 中的运行命令?