gpt4 book ai didi

bash - 根据域名使 URL 唯一

转载 作者:行者123 更新时间:2023-12-03 23:58:05 25 4
gpt4 key购买 nike

我有一个名为 urls.list 的 URL 列表:

https://target.com/?first=one
https://target.com/something/?first=one
http://target.com/dir/?first=summer
https://fake.com/?first=spring
https://example.com/about/?third=three
https://example.com/?third=three

并且我想根据它们的域(如 https://target.com)使它们独一无二,这意味着每个域及其协议(protocol)打印一次,并且避免下一个 URL。所以结果是:

https://target.com/?first=one
http://target.com/dir/?first=summer
https://fake.com/?first=spring
https://example.com/about/?third=three

这是我尝试做的:

cat urls.list | cut -d"/" -f1-3 | awk '!a[$0]++' >> host_unique.del

for urls in $(cat urls.list); do

for hosts in $(cat host_unique.del); do
if [[ $hosts == *"$urls"* ]]; then
echo "$hosts"
fi
done
done

最佳答案

这个 awk 可能会做你想做的事。

awk -F'/' '!seen[$1,$3]++' urls.list

bash 替代方案在处理大量数据/文件时会非常慢,但就是这样。

使用 mapfile 又名 readarray 这是一个 bash4+ 功能,关联数组。加上更多 bash 功能。

#!/usr/bin/env bash

declare -A uniq
mapfile -t urls < urls.list

for uniq_url in "${urls[@]}"; do
IFS='/' read -ra url <<< "$uniq_url"
if ((!uniq["${url[0]}","${url[2]}"]++)); then
printf '%s\n' "$uniq_url"
fi
done

关于bash - 根据域名使 URL 唯一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67667026/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com